Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,118 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import numpy as np
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
-
from sklearn.datasets import make_classification, make_circles, make_blobs, make_moons
|
| 5 |
-
from sklearn.model_selection import train_test_split, learning_curve
|
| 6 |
-
from sklearn.neighbors import KNeighborsClassifier
|
| 7 |
-
from sklearn.naive_bayes import GaussianNB
|
| 8 |
-
from sklearn.linear_model import LogisticRegression
|
| 9 |
-
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 11 |
-
from sklearn.svm import SVC
|
| 12 |
-
from sklearn.metrics import accuracy_score, f1_score
|
| 13 |
-
from mlxtend.plotting import plot_decision_regions
|
| 14 |
-
|
| 15 |
-
# Display image
|
| 16 |
-
# st.markdown("<div style='text-align: center;'>"
|
| 17 |
-
# "<img src='inno.jpg' width='700' height='400'>"
|
| 18 |
-
# "</div>",
|
| 19 |
-
# unsafe_allow_html=True)
|
| 20 |
-
from PIL import Image
|
| 21 |
-
|
| 22 |
-
image = Image.open("Inno.jpg")
|
| 23 |
-
image = image.resize((800, 500)) # Resize image before displaying
|
| 24 |
-
st.image(
|
| 25 |
-
|
| 26 |
-
# Streamlit app title with color
|
| 27 |
-
st.markdown("<h1 style='text-align: center; color: #FF5733;'>Boundary Surfaces Visualization</h1>", unsafe_allow_html=True)
|
| 28 |
-
|
| 29 |
-
# Select dataset
|
| 30 |
-
data = st.sidebar.selectbox('Select Dataset', ('Classification', 'Circles', 'Blobs', 'Moons'))
|
| 31 |
-
|
| 32 |
-
if data == 'Classification':
|
| 33 |
-
X, y = make_classification(n_samples=100, n_features=2, n_redundant=0, random_state=27)
|
| 34 |
-
elif data == 'Circles':
|
| 35 |
-
X, y = make_circles(n_samples=100, factor=0.5, noise=0.05)
|
| 36 |
-
elif data == 'Blobs':
|
| 37 |
-
X, y = make_blobs(n_samples=250, centers=2, n_features=2, cluster_std=1.0, random_state=27)
|
| 38 |
-
elif data == 'Moons':
|
| 39 |
-
X, y = make_moons(n_samples=250, noise=0.1, random_state=27)
|
| 40 |
-
|
| 41 |
-
# Split dataset
|
| 42 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=27)
|
| 43 |
-
|
| 44 |
-
def plot_decision_surface(X, y, model, title):
|
| 45 |
-
plt.figure(figsize=(6,4))
|
| 46 |
-
plot_decision_regions(X, y, clf=model, colors="#7f7f7f,#bcbd22,#17becf")
|
| 47 |
-
plt.title(title)
|
| 48 |
-
st.pyplot(plt.gcf(), clear_figure=True)
|
| 49 |
-
|
| 50 |
-
# Select classifier
|
| 51 |
-
classifier_name = st.sidebar.selectbox('Select Classifier',
|
| 52 |
-
('KNN', 'Naive Bayes', 'Logistic Regression', 'Decision Tree', 'Random Forest', 'SVM'))
|
| 53 |
-
|
| 54 |
-
# Initialize model based on user selection
|
| 55 |
-
if classifier_name == 'KNN':
|
| 56 |
-
n_neighbors = st.sidebar.slider('Number of Neighbors (k)', 1, 15, 3)
|
| 57 |
-
weights = st.sidebar.radio('Weight Function', ('uniform', 'distance'))
|
| 58 |
-
algorithm = st.sidebar.selectbox('Algorithm', ('auto', 'ball_tree', 'kd_tree', 'brute'))
|
| 59 |
-
p = st.sidebar.slider("Distance Parameter (p)", 1, 5, 2)
|
| 60 |
-
model = KNeighborsClassifier(n_neighbors=n_neighbors, weights=weights, algorithm=algorithm,p=p)
|
| 61 |
-
|
| 62 |
-
elif classifier_name == 'Naive Bayes':
|
| 63 |
-
model = GaussianNB()
|
| 64 |
-
|
| 65 |
-
elif classifier_name == 'Logistic Regression':
|
| 66 |
-
model = LogisticRegression()
|
| 67 |
-
|
| 68 |
-
elif classifier_name == 'Decision Tree':
|
| 69 |
-
model = DecisionTreeClassifier()
|
| 70 |
-
|
| 71 |
-
elif classifier_name == 'Random Forest':
|
| 72 |
-
n_estimators = st.sidebar.slider('Number of Trees', 10, 200, 100)
|
| 73 |
-
model = RandomForestClassifier(n_estimators=n_estimators)
|
| 74 |
-
|
| 75 |
-
elif classifier_name == 'SVM':
|
| 76 |
-
kernel = st.sidebar.selectbox('Kernel Type', ('linear', 'poly', 'rbf', 'sigmoid'))
|
| 77 |
-
C = st.sidebar.slider('Regularization (C)', 0.01, 10.0, 1.0)
|
| 78 |
-
model = SVC(kernel=kernel, C=C)
|
| 79 |
-
|
| 80 |
-
# Train model
|
| 81 |
-
model.fit(X_train, y_train)
|
| 82 |
-
|
| 83 |
-
# Make predictions
|
| 84 |
-
y_pred = model.predict(X_test)
|
| 85 |
-
|
| 86 |
-
# Compute accuracy & F1-score
|
| 87 |
-
accuracy = accuracy_score(y_test, y_pred)
|
| 88 |
-
f1 = f1_score(y_test, y_pred)
|
| 89 |
-
|
| 90 |
-
# Display metrics in Streamlit
|
| 91 |
-
st.markdown("<h3 style='color: #4CAF50;'>π Model Performance</h3>", unsafe_allow_html=True)
|
| 92 |
-
st.write(f"β
**Accuracy:** {accuracy:.2f}")
|
| 93 |
-
st.write(f"π **F1-score:** {f1:.2f}")
|
| 94 |
-
|
| 95 |
-
# Plot decision boundary
|
| 96 |
-
st.subheader("π Decision Boundary")
|
| 97 |
-
plot_decision_surface(X, y, model, f'{classifier_name} Decision Surface')
|
| 98 |
-
|
| 99 |
-
# Plot Learning Curve
|
| 100 |
-
def plot_learning_curve(model, X, y):
|
| 101 |
-
train_sizes, train_scores, test_scores = learning_curve(model, X, y, cv=5, scoring='accuracy', train_sizes=np.linspace(0.1, 1.0, 10))
|
| 102 |
-
|
| 103 |
-
train_mean = np.mean(train_scores, axis=1)
|
| 104 |
-
test_mean = np.mean(test_scores, axis=1)
|
| 105 |
-
|
| 106 |
-
plt.figure(figsize=(6,4))
|
| 107 |
-
plt.plot(train_sizes, train_mean, 'o-', label="Training Accuracy", color="blue")
|
| 108 |
-
plt.plot(train_sizes, test_mean, 'o-', label="Validation Accuracy", color="red")
|
| 109 |
-
|
| 110 |
-
plt.xlabel("Training Samples")
|
| 111 |
-
plt.ylabel("Accuracy")
|
| 112 |
-
plt.title(f"Learning Curve: {classifier_name}")
|
| 113 |
-
plt.legend()
|
| 114 |
-
st.pyplot(plt.gcf(), clear_figure=True)
|
| 115 |
-
|
| 116 |
-
# Display Learning Curve
|
| 117 |
-
st.subheader("π Learning Curve")
|
| 118 |
-
plot_learning_curve(model, X, y)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from sklearn.datasets import make_classification, make_circles, make_blobs, make_moons
|
| 5 |
+
from sklearn.model_selection import train_test_split, learning_curve
|
| 6 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 7 |
+
from sklearn.naive_bayes import GaussianNB
|
| 8 |
+
from sklearn.linear_model import LogisticRegression
|
| 9 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 11 |
+
from sklearn.svm import SVC
|
| 12 |
+
from sklearn.metrics import accuracy_score, f1_score
|
| 13 |
+
from mlxtend.plotting import plot_decision_regions
|
| 14 |
+
|
| 15 |
+
# Display image
|
| 16 |
+
# st.markdown("<div style='text-align: center;'>"
|
| 17 |
+
# "<img src='inno.jpg' width='700' height='400'>"
|
| 18 |
+
# "</div>",
|
| 19 |
+
# unsafe_allow_html=True)
|
| 20 |
+
from PIL import Image
|
| 21 |
+
|
| 22 |
+
# image = Image.open("Inno.jpg")
|
| 23 |
+
# image = image.resize((800, 500)) # Resize image before displaying
|
| 24 |
+
st.image("innomatics-footer-logo.webp", use_container_width=True, width=300)
|
| 25 |
+
|
| 26 |
+
# Streamlit app title with color
|
| 27 |
+
st.markdown("<h1 style='text-align: center; color: #FF5733;'>Boundary Surfaces Visualization</h1>", unsafe_allow_html=True)
|
| 28 |
+
|
| 29 |
+
# Select dataset
|
| 30 |
+
data = st.sidebar.selectbox('Select Dataset', ('Classification', 'Circles', 'Blobs', 'Moons'))
|
| 31 |
+
|
| 32 |
+
if data == 'Classification':
|
| 33 |
+
X, y = make_classification(n_samples=100, n_features=2, n_redundant=0, random_state=27)
|
| 34 |
+
elif data == 'Circles':
|
| 35 |
+
X, y = make_circles(n_samples=100, factor=0.5, noise=0.05)
|
| 36 |
+
elif data == 'Blobs':
|
| 37 |
+
X, y = make_blobs(n_samples=250, centers=2, n_features=2, cluster_std=1.0, random_state=27)
|
| 38 |
+
elif data == 'Moons':
|
| 39 |
+
X, y = make_moons(n_samples=250, noise=0.1, random_state=27)
|
| 40 |
+
|
| 41 |
+
# Split dataset
|
| 42 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=27)
|
| 43 |
+
|
| 44 |
+
def plot_decision_surface(X, y, model, title):
|
| 45 |
+
plt.figure(figsize=(6,4))
|
| 46 |
+
plot_decision_regions(X, y, clf=model, colors="#7f7f7f,#bcbd22,#17becf")
|
| 47 |
+
plt.title(title)
|
| 48 |
+
st.pyplot(plt.gcf(), clear_figure=True)
|
| 49 |
+
|
| 50 |
+
# Select classifier
|
| 51 |
+
classifier_name = st.sidebar.selectbox('Select Classifier',
|
| 52 |
+
('KNN', 'Naive Bayes', 'Logistic Regression', 'Decision Tree', 'Random Forest', 'SVM'))
|
| 53 |
+
|
| 54 |
+
# Initialize model based on user selection
|
| 55 |
+
if classifier_name == 'KNN':
|
| 56 |
+
n_neighbors = st.sidebar.slider('Number of Neighbors (k)', 1, 15, 3)
|
| 57 |
+
weights = st.sidebar.radio('Weight Function', ('uniform', 'distance'))
|
| 58 |
+
algorithm = st.sidebar.selectbox('Algorithm', ('auto', 'ball_tree', 'kd_tree', 'brute'))
|
| 59 |
+
p = st.sidebar.slider("Distance Parameter (p)", 1, 5, 2)
|
| 60 |
+
model = KNeighborsClassifier(n_neighbors=n_neighbors, weights=weights, algorithm=algorithm,p=p)
|
| 61 |
+
|
| 62 |
+
elif classifier_name == 'Naive Bayes':
|
| 63 |
+
model = GaussianNB()
|
| 64 |
+
|
| 65 |
+
elif classifier_name == 'Logistic Regression':
|
| 66 |
+
model = LogisticRegression()
|
| 67 |
+
|
| 68 |
+
elif classifier_name == 'Decision Tree':
|
| 69 |
+
model = DecisionTreeClassifier()
|
| 70 |
+
|
| 71 |
+
elif classifier_name == 'Random Forest':
|
| 72 |
+
n_estimators = st.sidebar.slider('Number of Trees', 10, 200, 100)
|
| 73 |
+
model = RandomForestClassifier(n_estimators=n_estimators)
|
| 74 |
+
|
| 75 |
+
elif classifier_name == 'SVM':
|
| 76 |
+
kernel = st.sidebar.selectbox('Kernel Type', ('linear', 'poly', 'rbf', 'sigmoid'))
|
| 77 |
+
C = st.sidebar.slider('Regularization (C)', 0.01, 10.0, 1.0)
|
| 78 |
+
model = SVC(kernel=kernel, C=C)
|
| 79 |
+
|
| 80 |
+
# Train model
|
| 81 |
+
model.fit(X_train, y_train)
|
| 82 |
+
|
| 83 |
+
# Make predictions
|
| 84 |
+
y_pred = model.predict(X_test)
|
| 85 |
+
|
| 86 |
+
# Compute accuracy & F1-score
|
| 87 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 88 |
+
f1 = f1_score(y_test, y_pred)
|
| 89 |
+
|
| 90 |
+
# Display metrics in Streamlit
|
| 91 |
+
st.markdown("<h3 style='color: #4CAF50;'>π Model Performance</h3>", unsafe_allow_html=True)
|
| 92 |
+
st.write(f"β
**Accuracy:** {accuracy:.2f}")
|
| 93 |
+
st.write(f"π **F1-score:** {f1:.2f}")
|
| 94 |
+
|
| 95 |
+
# Plot decision boundary
|
| 96 |
+
st.subheader("π Decision Boundary")
|
| 97 |
+
plot_decision_surface(X, y, model, f'{classifier_name} Decision Surface')
|
| 98 |
+
|
| 99 |
+
# Plot Learning Curve
|
| 100 |
+
def plot_learning_curve(model, X, y):
|
| 101 |
+
train_sizes, train_scores, test_scores = learning_curve(model, X, y, cv=5, scoring='accuracy', train_sizes=np.linspace(0.1, 1.0, 10))
|
| 102 |
+
|
| 103 |
+
train_mean = np.mean(train_scores, axis=1)
|
| 104 |
+
test_mean = np.mean(test_scores, axis=1)
|
| 105 |
+
|
| 106 |
+
plt.figure(figsize=(6,4))
|
| 107 |
+
plt.plot(train_sizes, train_mean, 'o-', label="Training Accuracy", color="blue")
|
| 108 |
+
plt.plot(train_sizes, test_mean, 'o-', label="Validation Accuracy", color="red")
|
| 109 |
+
|
| 110 |
+
plt.xlabel("Training Samples")
|
| 111 |
+
plt.ylabel("Accuracy")
|
| 112 |
+
plt.title(f"Learning Curve: {classifier_name}")
|
| 113 |
+
plt.legend()
|
| 114 |
+
st.pyplot(plt.gcf(), clear_figure=True)
|
| 115 |
+
|
| 116 |
+
# Display Learning Curve
|
| 117 |
+
st.subheader("π Learning Curve")
|
| 118 |
+
plot_learning_curve(model, X, y)
|