-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1.py
111 lines (75 loc) · 2.37 KB
/
exercise1.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
import streamlit as st
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from PIL import Image
# #Set title
st.title('Data Science App')
image = Image.open('ML.jpg')
st.image(image,use_column_width=True)
#set subtitle
st.write("""
# A simple Data App With Streamlit
""")
st.write("""
### Let's Explore different classifiers and datasets
""")
dataset_name=st.sidebar.selectbox("Select dataset",("Breast Cancer","Iris","Wine"))
classifier_name=st.sidebar.selectbox("Select classifiers",("SVM","KNN"))
def get_dataset(name):
data=None
if name=="Iris":
data=datasets.load_iris()
elif name=="Wine":
data=datasets.load_wine()
else:
data=datasets.load_breast_cancer()
x=data.data
y=data.target
return x,y
x,y=get_dataset(dataset_name)
st.dataframe(x)
st.write("Shape of your dataset is:",x.shape)
st.write("Unique target variables:",len(np.unique(y)))
fig=plt.figure()
sns.boxplot(data=x, orient="h")
st.pyplot()
plt.hist(x)
st.pyplot()
# Building Our Algorithm
def add_parameter(name_of_clf):
params=dict()
if name_of_clf=="SVM":
C=st.sidebar.slider("C",0.01,15.0)
params["C"]=C
else:
name_of_clf="KNN"
K=st.sidebar.slider("K",1,15)
params["K"]=K
return params
params=add_parameter(classifier_name)
#Accessing our Classifier
def get_classifier(name_of_clf,params):
clf=None
if name_of_clf=="SVM":
clf=SVC(C=params['C'])
elif name_of_clf=='KNN':
clf=KNeighborsClassifier(n_neighbors=params['K'])
else:
st.warning("you didn't select any option, please select at least one algorithm")
return clf
clf=get_classifier(classifier_name,params)
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.20,random_state=10)
clf.fit(x_train,y_train)
y_pred=clf.predict(x_test)
st.write(y_pred)
accuracy=accuracy_score(y_test,y_pred)
st.write("classifier_name:",classifier_name)
st.write("Accuracy for your model is:",accuracy)