-
Notifications
You must be signed in to change notification settings - Fork 0
/
intro2.py
132 lines (86 loc) · 2.95 KB
/
intro2.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
120
121
122
123
124
125
126
127
128
129
130
131
132
import streamlit as st
import numpy as np
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('Machine Learning Using Streamlit')
image = Image.open("Techy.jfif")
#st.image(image,use_column_width=True)
# Get the new width and height
new_width = st.slider("Select new width", min_value=1, max_value=image.width, value=image.width)
new_height = st.slider("Select new height", min_value=1, max_value=image.height, value=image.height)
# Reshape the image to the new dimensions
resized_image = image.resize((new_width, new_height))
# Display the resized image
st.image(resized_image, caption=f"Resized Image ({new_width}x{new_height})", 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 Classifier',('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()
st.set_option('deprecation.showPyplotGlobalUse', False)
#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 algo")
return clf
clf=get_classifier(classifier_name,params)
# Split data
x_train,x_test,y_train,y_test=train_test_split(x,y, test_size=0.2, random_state=10)
#Fit data
clf.fit(x_train,y_train) #80% for training
#Predict
y_pred=clf.predict(x_test) #20% for testing
st.write(y_pred)
#Test score
accuracy=accuracy_score(y_test,y_pred)
st.write('classifier_name:',classifier_name)
st.write('Acccuray for your model is:',accuracy)