Skip to content

Commit 2dc2e4a

Browse files
Initial commit
0 parents  commit 2dc2e4a

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed

.devcontainer/devcontainer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "Python 3",
3+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
4+
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
5+
"customizations": {
6+
"codespaces": {
7+
"openFiles": [
8+
"README.md",
9+
"app.py"
10+
]
11+
},
12+
"vscode": {
13+
"settings": {},
14+
"extensions": [
15+
"ms-python.python",
16+
"ms-python.vscode-pylance"
17+
]
18+
}
19+
},
20+
"updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y <packages.txt; [ -f requirements.txt ] && pip3 install --user -r requirements.txt; pip3 install --user streamlit; echo '✅ Packages installed and Requirements met'",
21+
"postAttachCommand": {
22+
"server": "streamlit run app.py --server.enableCORS false --server.enableXsrfProtection false"
23+
},
24+
"portsAttributes": {
25+
"8501": {
26+
"label": "Application",
27+
"onAutoForward": "openPreview"
28+
}
29+
},
30+
"forwardPorts": [
31+
8501
32+
]
33+
}

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# My Streamlit App – Explore ML Models on Popular Datasets
2+
3+
Welcome to my interactive **Machine Learning Model Explorer** built using **Streamlit**. This app allows users to:
4+
5+
- Select different datasets
6+
- Choose between popular ML models (SVM, KNN, Random Forest)
7+
- Tune hyperparameters through the sidebar
8+
- Visualize results in 2D using PCA
9+
- View model accuracy scores instantly
10+
11+
---
12+
13+
## Features
14+
15+
### Select from popular datasets:
16+
- Iris
17+
- Breast Cancer
18+
- Wine
19+
20+
### Choose and tune classifiers:
21+
- **K-Nearest Neighbors (KNN)** – Adjust the number of neighbors
22+
- **Support Vector Machine (SVM)** – Control the regularization parameter `C`
23+
- **Random Forest** – Set `n_estimators` and `max_depth`
24+
25+
### Real-time insights:
26+
- Shows dataset shape and number of classes
27+
- Displays accuracy of the selected model
28+
- Visualizes data in 2D using **PCA** with color-coded target labels
29+
30+
---
31+
32+
## Tech Stack
33+
34+
- **Python**
35+
- **Streamlit**
36+
- **Scikit-learn**
37+
- **Matplotlib**
38+
- **NumPy**
39+
40+
---
41+
42+
## App Preview
43+
44+
![App Screenshot](app_screenshort.PNG)
45+
46+
---
47+
48+
## How to Run This App Locally
49+
50+
```bash
51+
# 1. Clone the repository
52+
git clone https://github.com/YourUsername/My-Streamlit-App.git
53+
54+
# 2. Navigate into the project directory
55+
cd My-Streamlit-App
56+
57+
# 3. Install the dependencies
58+
pip install -r requirements.txt
59+
60+
# 4. Run the Streamlit app
61+
streamlit run app.py

app.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# import libraries
2+
import streamlit as st
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
from sklearn import datasets
6+
from sklearn.model_selection import train_test_split
7+
from sklearn.decomposition import PCA
8+
from sklearn.svm import SVC
9+
from sklearn.neighbors import KNeighborsClassifier
10+
from sklearn.ensemble import RandomForestClassifier
11+
from sklearn.metrics import accuracy_score
12+
13+
# app heading
14+
st.write("""
15+
# Explore different ML Models and Datasets
16+
Let's see which one is best from these?
17+
""")
18+
19+
# make sidebar
20+
dataset_name = st.sidebar.selectbox(
21+
'Select Dataset',
22+
("Iris", "Breast Cancer", "Wine")
23+
)
24+
25+
# classifier selection
26+
classifier_name = st.sidebar.selectbox(
27+
"Select Classifier",
28+
("KNN", "SVM", "Random Forest")
29+
)
30+
31+
# import datasets
32+
def get_dataset(dataset_name):
33+
data = None
34+
if dataset_name == "Iris":
35+
data = datasets.load_iris()
36+
elif dataset_name == "Wine":
37+
data = datasets.load_wine()
38+
else:
39+
data = datasets.load_breast_cancer()
40+
x = data.data
41+
y = data.target
42+
return x, y
43+
44+
# get selected dataset
45+
X, y = get_dataset(dataset_name)
46+
47+
# dataset info
48+
st.write("Shape of dataset:", X.shape)
49+
st.write("Number of classes:", len(np.unique(y)))
50+
51+
# classifier parameters from UI
52+
def add_parameter_ui(classifier_name):
53+
params = dict()
54+
if classifier_name == "SVM":
55+
C = st.sidebar.slider('C', 0.01, 10.0, 1.0)
56+
params['C'] = C
57+
elif classifier_name == "KNN":
58+
K = st.sidebar.slider("K", 1, 15)
59+
params['K'] = K
60+
else:
61+
max_depth = st.sidebar.slider('max_depth', 2, 15)
62+
params['max_depth'] = max_depth
63+
n_estimators = st.sidebar.slider('n_estimators', 1, 100)
64+
params['n_estimators'] = n_estimators
65+
return params
66+
67+
# get classifier params from UI
68+
params = add_parameter_ui(classifier_name)
69+
70+
# create classifier based on selected model
71+
def get_classifier(classifier_name, params):
72+
clf = None
73+
if classifier_name == "SVM":
74+
clf = SVC(C=params['C'])
75+
elif classifier_name == "KNN":
76+
clf = KNeighborsClassifier(n_neighbors=params['K'])
77+
else:
78+
clf = RandomForestClassifier(
79+
n_estimators=params['n_estimators'],
80+
max_depth=params['max_depth'],
81+
random_state=1234
82+
)
83+
return clf
84+
85+
# get the classifier
86+
clf = get_classifier(classifier_name, params)
87+
88+
# split data into training and testing
89+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234)
90+
91+
# train the model
92+
clf.fit(X_train, y_train)
93+
y_pred = clf.predict(X_test)
94+
95+
# show results
96+
acc = accuracy_score(y_test, y_pred)
97+
st.write(f'Classifier = {classifier_name}')
98+
st.write(f'Accuracy = {acc:.2f}')
99+
100+
# plot PCA 2D
101+
pca = PCA(2)
102+
X_projected = pca.fit_transform(X)
103+
104+
x1 = X_projected[:, 0]
105+
x2 = X_projected[:, 1]
106+
107+
fig = plt.figure()
108+
plt.scatter(x1, x2, c=y, alpha=0.8, cmap='viridis')
109+
plt.xlabel('Principal Component 1')
110+
plt.ylabel('Principal Component 2')
111+
plt.colorbar()
112+
st.pyplot(fig)

app_screenshort.PNG

120 KB
Loading

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
matplotlib==3.5.3
2+
numpy==1.22.4
3+
scikit_learn==0.24.2
4+
streamlit==1.40.1
5+
click==8.1.8

0 commit comments

Comments
 (0)