-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
119 lines (69 loc) · 2.84 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 28 16:15:08 2020
@author: Guru
"""
import streamlit as st
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import numpy as np
st.title("Streamlit and Machine Learning")
st.write("""
# Explore Different Classifiers
Which one is the best?
"""
)
dataset_name = st.selectbox("Select Dataset", ("Iris", "Breast Cancer", "Wine"))
classifier_name = st.selectbox("Select Classifier", ("KNN", "SVM", "Random Forest"))
def get_dataset(dataset_name):
if dataset_name == "Iris":
data = datasets.load_iris()
elif dataset_name == "Breast Cancer":
data datasets.load_breast_cancer()
elif dataset_name == "Wine":
data = datasets.load_wine()
X = data.data
y = data.target
return X,y
X, y = get_dataset(dataset_name)
st.write("Shape of data is ", X.shape)
st.write("Number of Classes is", len(np.unique(y)))
def add_parameter_ui(clf_name):
params = dict()
if clf_name == "KNN":
K = st.sidebar.slider("K", 1, 15)
params["K"] = K
elif clf_name == "SVM":
C = st.sidebar.slider("C", 0.01, 10.0)
params["C"] = C
elif clf_name == "Random Forest":
MaxDepth = st.sidebar.slider("Max Depth", 2, 15)
NEstimators = st.sidebar.slider("Number of Estimators", 1, 100)
params["MaxDepth"] = MaxDepth
params["NEstimators"] = NEstimators
return params
P = add_parameter_ui(classifier_name)
def get_classifier(clf_name, params):
if clf_name == "KNN":
clf = KNeighborsClassifier(n_neighbors = P["K"], )
elif clf_name == "SVM":
clf = SVC(C=P["C"])
elif clf_name == "Random Forest":
clf = RandomForestClassifier(n_estimators = P["NEstimators"], max_depth=P["MaxDepth"],
)
return clf
clf = get_classifier(classifier_name, P)
st.write("""
# Classification
"""
)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
acc = accuracy_score(y_test, y_predict)
st.write(f"Classifer = {classifier_name}")
st.write(f"Classifer Accuracy= {acc}")