| | import streamlit as st |
| | import numpy as np |
| | import matplotlib.pyplot as plt |
| | from sklearn.datasets import make_classification, make_circles, make_blobs, make_moons |
| | from sklearn.model_selection import train_test_split, learning_curve |
| | from sklearn.neighbors import KNeighborsClassifier |
| | from sklearn.naive_bayes import GaussianNB |
| | from sklearn.linear_model import LogisticRegression |
| | from sklearn.tree import DecisionTreeClassifier |
| | from sklearn.metrics import accuracy_score, f1_score |
| | from mlxtend.plotting import plot_decision_regions |
| |
|
| | |
| | st.image("inno.jpg", width=500) |
| |
|
| | |
| | st.markdown("<h1 style='color:#17becf;'>ML Models Decision surface and learning curve</h1>", unsafe_allow_html=True) |
| |
|
| | |
| | data = st.sidebar.selectbox('Type of data ', ('Classification', 'Circles', 'Blobs', 'Moons')) |
| |
|
| | if data == 'Classification': |
| | X, y = make_classification(n_samples=200, n_features=2, n_redundant=0, random_state=27) |
| | elif data == 'Circles': |
| | X, y = make_circles(n_samples=200, factor=0.5, noise=0.05) |
| | elif data == 'Blobs': |
| | X, y = make_blobs(n_samples=200, centers=2, n_features=2, cluster_std=1.0, random_state=27) |
| | elif data == 'Moons': |
| | X, y = make_moons(n_samples=200, noise=0.1, random_state=42) |
| |
|
| | |
| | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) |
| |
|
| | def plot_decision_surface(X, y, model, title): |
| | plt.figure(figsize=(6,4)) |
| | plot_decision_regions(X, y, clf=model) |
| | plt.title(title) |
| | st.pyplot(plt.gcf(), clear_figure=True) |
| |
|
| | |
| | classifier_name = st.sidebar.selectbox('Select Classifier', ('KNN', 'Naive Bayes', 'Logistic Regression', 'DecisionTreeClassifier')) |
| |
|
| | if classifier_name == 'KNN': |
| | n_neighbors = st.sidebar.slider('Number of Neighbors (k)', 1, 15, 3) |
| | weights = st.sidebar.radio('Weight Function', ('uniform', 'distance')) |
| | algorithm = st.sidebar.selectbox('Algorithm', ('auto', 'ball_tree', 'kd_tree', 'brute')) |
| | |
| | model = KNeighborsClassifier(n_neighbors=n_neighbors, weights=weights, algorithm=algorithm) |
| | |
| | elif classifier_name == 'Naive Bayes': |
| | model = GaussianNB() |
| | |
| | elif classifier_name == 'DecisionTreeClassifier': |
| | model = DecisionTreeClassifier() |
| | |
| | else: |
| | model = LogisticRegression() |
| |
|
| | |
| | model.fit(X_train, y_train) |
| |
|
| | |
| | y_pred = model.predict(X_test) |
| |
|
| | |
| | accuracy = accuracy_score(y_test, y_pred) |
| | f1 = f1_score(y_test, y_pred) |
| |
|
| | |
| | st.subheader("Model Performance") |
| | st.write(f"*Accuracy:* {accuracy:.2f}") |
| | st.write(f"*F1-score:* {f1:.2f}") |
| |
|
| | |
| | plot_decision_surface(X, y, model, f'{classifier_name} Decision Surface') |
| |
|
| | |
| | def plot_learning_curve(model, X, y): |
| | train_sizes, train_scores, test_scores = learning_curve(model, X, y, cv=5, scoring='accuracy', train_sizes=np.linspace(0.1, 1.0, 10)) |
| | |
| | train_mean = np.mean(train_scores, axis=1) |
| | test_mean = np.mean(test_scores, axis=1) |
| | |
| | plt.figure(figsize=(6,4)) |
| | plt.plot(train_sizes, train_mean, 'o-', label="Training Accuracy") |
| | plt.plot(train_sizes, test_mean, 'o-', label="Validation Accuracy") |
| | |
| | plt.xlabel("Training Samples") |
| | plt.ylabel("Accuracy") |
| | plt.title(f"Learning Curve: {classifier_name}") |
| | plt.legend() |
| | st.pyplot(plt.gcf(), clear_figure=True) |
| |
|
| | |
| | st.subheader("Learning Curve") |
| | plot_learning_curve(model, X, y) |