CoolFace
Apppublic

indhu-0120/Machine-Learning-Algorithms

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py96 linesDownload Raw Back to root
1import streamlit as st2import numpy as np3import matplotlib.pyplot as plt4from sklearn.datasets import make_classification, make_circles, make_blobs, make_moons5from sklearn.model_selection import train_test_split, learning_curve6from sklearn.neighbors import KNeighborsClassifier7from sklearn.naive_bayes import GaussianNB8from sklearn.linear_model import LogisticRegression9from sklearn.tree import DecisionTreeClassifier10from sklearn.metrics import accuracy_score, f1_score11from mlxtend.plotting import plot_decision_regions12 13# Display image14st.image("inno.jpg", width=500)15 16# Streamlit app title17st.markdown("<h1 style='color:#17becf;'>Model Boundary Representation</h1>", unsafe_allow_html=True)18 19# Select dataset20data = st.sidebar.selectbox('Type of data ', ('Classification', 'Circles', 'Blobs', 'Moons'))21 22if data == 'Classification':23    X, y = make_classification(n_samples=200, n_features=2, n_redundant=0, random_state=27)24elif data == 'Circles':25    X, y = make_circles(n_samples=200, factor=0.5, noise=0.05)26elif data == 'Blobs':27    X, y = make_blobs(n_samples=200, centers=2, n_features=2, cluster_std=1.0, random_state=27)28elif data == 'Moons':29    X, y = make_moons(n_samples=200, noise=0.1, random_state=42)30 31# Split dataset32X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)33 34def plot_decision_surface(X, y, model, title):35    plt.figure(figsize=(6,4))36    plot_decision_regions(X, y, clf=model, colors="#8c564b")37    plt.title(title)38    st.pyplot(plt.gcf(), clear_figure=True)39 40# Select classifier41classifier_name = st.sidebar.selectbox('Select Classifier', ('KNN', 'Naive Bayes', 'Logistic Regression', 'DecisionTreeClassifier'))42 43if classifier_name == 'KNN':44    n_neighbors = st.sidebar.slider('Number of Neighbors (k)', 1, 15, 3)45    weights = st.sidebar.radio('Weight Function', ('uniform', 'distance'))46    algorithm = st.sidebar.selectbox('Algorithm', ('auto', 'ball_tree', 'kd_tree', 'brute'))47    48    model = KNeighborsClassifier(n_neighbors=n_neighbors, weights=weights, algorithm=algorithm)49    50elif classifier_name == 'Naive Bayes':51    model = GaussianNB()52    53elif classifier_name == 'DecisionTreeClassifier':54    model = DecisionTreeClassifier()55    56else:57    model = LogisticRegression()58 59# Train model60model.fit(X_train, y_train)61 62# Make predictions63y_pred = model.predict(X_test)64 65# Compute accuracy & F1-score66accuracy = accuracy_score(y_test, y_pred)67f1 = f1_score(y_test, y_pred)68 69# Display metrics in Streamlit70st.subheader("Model Performance")71st.write(f"*Accuracy:* {accuracy:.2f}")72st.write(f"*F1-score:* {f1:.2f}")73 74# Plot decision boundary75plot_decision_surface(X, y, model, f'{classifier_name} Decision Surface')76 77# Plot Learning Curve78def plot_learning_curve(model, X, y):79    train_sizes, train_scores, test_scores = learning_curve(model, X, y, cv=5, scoring='accuracy', train_sizes=np.linspace(0.1, 1.0, 10))80    81    train_mean = np.mean(train_scores, axis=1)82    test_mean = np.mean(test_scores, axis=1)83    84    plt.figure(figsize=(6,4))85    plt.plot(train_sizes, train_mean, 'o-', label="Training Accuracy")86    plt.plot(train_sizes, test_mean, 'o-', label="Validation Accuracy")87    88    plt.xlabel("Training Samples")89    plt.ylabel("Accuracy")90    plt.title(f"Learning Curve: {classifier_name}")91    plt.legend()92    st.pyplot(plt.gcf(), clear_figure=True)93 94# Display Learning Curve95st.subheader("Learning Curve")96plot_learning_curve(model, X, y)