Team Ai
Apppublic

dthota10/Different_Machine_Learning_Algorithms

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py96 linesDownload Raw Back to root
1import streamlit as st
2import numpy as np
3import matplotlib.pyplot as plt
4from sklearn.datasets import make_classification, make_circles, make_blobs, make_moons
5from sklearn.model_selection import train_test_split, learning_curve
6from sklearn.neighbors import KNeighborsClassifier
7from sklearn.naive_bayes import GaussianNB
8from sklearn.linear_model import LogisticRegression
9from sklearn.tree import DecisionTreeClassifier
10from sklearn.metrics import accuracy_score, f1_score
11from mlxtend.plotting import plot_decision_regions
12
13# Display image
14st.image("inno.jpg", width=600)
15
16# Streamlit app title
17st.markdown("<h1 style='color:#2ca02c;'>Visualizing Classification Boundaries</h1>", unsafe_allow_html=True)
18
19# Select dataset
20data = st.sidebar.selectbox('Type of data ', ('Classification', 'Circles', 'Blobs', 'Moons'))
21
22if data == 'Classification':
23    X, y = make_classification(n_samples=300, n_features=2, n_redundant=0, random_state=42)
24elif data == 'Circles':
25    X, y = make_circles(n_samples=300, factor=0.5, noise=0.05)
26elif data == 'Blobs':
27    X, y = make_blobs(n_samples=300, centers=2, n_features=2, cluster_std=1.0, random_state=42)
28elif data == 'Moons':
29    X, y = make_moons(n_samples=300, noise=0.1, random_state=42)
30
31# Split dataset
32X_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="#98df8a")
37    plt.title(title)
38    st.pyplot(plt.gcf(), clear_figure=True)
39
40# Select classifier
41classifier_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 model
60model.fit(X_train, y_train)
61
62# Make predictions
63y_pred = model.predict(X_test)
64
65# Compute accuracy & F1-score
66accuracy = accuracy_score(y_test, y_pred)
67f1 = f1_score(y_test, y_pred)
68
69# Display metrics in Streamlit
70st.subheader("Model Performance")
71st.write(f"Accuracy: {accuracy:.2f}")
72st.write(f"F1-score: {f1:.2f}")
73
74# Plot decision boundary
75plot_decision_surface(X, y, model, f'{classifier_name} Decision Surface')
76
77# Plot Learning Curve
78def 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 Curve
95st.subheader("Learning Curve")
96plot_learning_curve(model, X, y)