Team Ai
Apppublic

Siddureddy27/Visualization_Using_Different_Algorithms

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py86 linesDownload Raw Back to root
1import streamlit as st2import pandas as pd3import numpy as np4import seaborn as sns5import matplotlib.pyplot as plt6from mlxtend.plotting import plot_decision_regions7from sklearn.neighbors import KNeighborsClassifier8from sklearn.linear_model import LogisticRegression9from sklearn.naive_bayes import GaussianNB10from sklearn.metrics import accuracy_score, f1_score11from sklearn.datasets import make_classification,make_circles,make_blobs,make_moons12from sklearn.model_selection import train_test_split,learning_curve13 14st.image("Inno logo.png",width=500)15 16st.title("DB & LC Using Different Algorithms")17st.sidebar.header("Model Selector")18 19data_type = st.sidebar.selectbox("Select Dataset Type", ["Moons","Blobs","Classification","Circles"], index=0)20noise = st.sidebar.slider("Noise Level", 0.0, 1.0, 0.2)21 22# Always Show Number of Neighbors23n_neighbors = st.sidebar.slider("Number of Neighbors", 1, 15, 5)24 25# Algorithm Selection26st.sidebar.subheader("Select Algorithm")27algorithm_selected = st.sidebar.selectbox("Choose an Algorithm", ["KNN", "Logistic Regression","Naive Bayes"])28 29# Show KNN-related parameters only if KNN is selected30if algorithm_selected == "KNN":31    knn_type = st.sidebar.selectbox("KNN Weight Type", ["uniform", "distance"])32    knn_metric = st.sidebar.selectbox("Select KNN Metric", ["euclidean", "manhattan", "minkowski"])33 34# Generate dataset35if data_type == "Blobs":36    X, y = make_blobs(n_samples=2000, centers=2, cluster_std=noise, random_state=27)37elif data_type == "Moons":38    X, y = make_moons(n_samples=2000, noise=noise, random_state=27)39elif data_type == "Circles":40    X, y = make_circles(n_samples=2000, noise=noise, factor=0.5, random_state=27)41else:42    X, y = make_classification(n_samples=2000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=27)43 44# Train-test split and scaling45X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=27)46 47# Train and evaluate selected model48if algorithm_selected == "KNN":49    model = KNeighborsClassifier(n_neighbors=n_neighbors, weights=knn_type, metric=knn_metric)50elif algorithm_selected == "Logistic Regression":51    model = LogisticRegression()52elif algorithm_selected == "Naive Bayes":53    model = GaussianNB()54 55# Train Model56model.fit(X_train, y_train)57y_pred = model.predict(X_test)58 59# Evaluation Metrics60st.write(f"### {algorithm_selected} Evaluation Metrics")61accuracy = accuracy_score(y_test, y_pred)62f1 = f1_score(y_test, y_pred, average='weighted')63 64st.write(f"**Accuracy:** {accuracy:.2f}")65st.write(f"**F1 Score:** {f1:.2f}")66 67# Decision Boundary68st.write(f"### {algorithm_selected} Decision Boundary")69fig, ax = plt.subplots()70plot_decision_regions(X_test, y_test, clf=model, legend=2)71plt.legend(loc='upper right')72st.pyplot(fig)73 74# Learning Curve75st.write(f"### {algorithm_selected} Learning Curve")76train_sizes, train_scores, test_scores = learning_curve(model, X_train, y_train, cv=5, train_sizes=np.linspace(0.1, 1.0, 10))77train_mean = np.mean(train_scores, axis=1)78test_mean = np.mean(test_scores, axis=1)79 80fig, ax = plt.subplots()81ax.plot(train_sizes, train_mean, label='Train Accuracy', marker='o')82ax.plot(train_sizes, test_mean, label='Test Accuracy', marker='s')83ax.set_xlabel("Training Size")84ax.set_ylabel("Accuracy")85ax.legend()86st.pyplot(fig)