badrih21 commited on
Commit
4ad71d9
·
1 Parent(s): 91965db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -150
app.py CHANGED
@@ -18,27 +18,9 @@ Programme permetttant de detecter sous quelle drogue se trouve un patient.
18
  from numpy.core.fromnumeric import shape
19
  import pandas as pd
20
  import numpy as np
21
- from scipy import stats
22
- import matplotlib.pyplot as plt
23
- import seaborn as sns
24
 
25
 
26
- df=pd.read_csv('D:/downloads/drug200.csv')
27
-
28
- print(df.shape)
29
- print(df.dtypes)
30
- print(df.describe)
31
-
32
- # Extraction des différentes classes de la colonne 'classe'
33
- classes = df['Drug'].unique()
34
-
35
- # Affichage des différentes classes
36
- print(classes)
37
-
38
- """Nous observons dans notre dataset, plusieurs colonnes qui sont: Age , Sexe, Pression sanguine, taux de cholesterol, ration de sodium a potassium et la colone cible, Type de drogue."""
39
-
40
- #print(df['Drug'].value_counts(normalize=True))
41
- #Nous alons commencer par encoder les variables qualitatives afin de pouvoir continuer notre traitement
42
 
43
  from sklearn.model_selection import train_test_split
44
  #nous allons encoder les diferentes variables
@@ -59,24 +41,12 @@ BP_encoded = label_encoder.fit_transform(df['BP'])
59
 
60
  Cholesterol_encoded = label_encoder.fit_transform(df['Cholesterol'])
61
 
62
- #df.Drug=Drug_encoded
63
-
64
-
65
- #df.Sex=Sex_encoded
66
-
67
- #df.BP=BP_encoded
68
-
69
- #df.Cholesterol= Cholesterol_encoded
70
-
71
- #print(df['Drug'])
72
- df
73
  df.drop("Drug", axis=1, inplace=True)
74
  df.drop("Sex", axis=1, inplace=True)
75
  df.drop("BP", axis=1, inplace=True)
76
  df.drop("Cholesterol", axis=1, inplace=True)
77
 
78
- # Appending the array to our dataFrame
79
- # with column name 'Purchased'
80
  df["Drug"] = Drug_encoded
81
  df['Sex'] = Sex_encoded
82
  df['BP'] = BP_encoded
@@ -87,127 +57,9 @@ x = df[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']]
87
  y = df['Drug']
88
 
89
 
90
- #nous allons maintenant creer les instances de test et d'entrainement pour x et y
91
-
92
  from sklearn.model_selection import train_test_split
93
  X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=0)
94
 
95
- y
96
- # choix du model d'entrainement
97
-
98
-
99
- #Premier : Knn
100
-
101
-
102
- classifier.fit(X_train, y_train)
103
-
104
- y_pred = classifier.predict(X_test)
105
- classifier.score(X_train,y_train)
106
- #classifier.score(X_test,y_test)
107
- #Evaluation des performances du modele de KNN
108
-
109
- #importons les metriques de perf
110
- from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
111
-
112
- #accuracy
113
- acc= round(accuracy_score(y_test,y_pred),6)
114
-
115
- #precision
116
- prec=round(precision_score(y_test,y_pred,average='macro'),6)
117
-
118
- #
119
- rec=round(recall_score(y_test,y_pred,average='macro'),6)
120
-
121
- #
122
- f1= round(f1_score(y_test,y_pred,average='macro'),6)
123
-
124
- print("Acc=",acc," prec=",prec," rec=",rec," f1=",f1)
125
-
126
- #recherche des meilleurs parametres
127
-
128
- for p in [0.1, 0.2, 0.3, 0.4, 0.5]:
129
- X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=p, random_state=0)
130
- classifier = KNeighborsClassifier(n_neighbors=6)
131
- classifier.fit(X_train, y_train)
132
- y_pred = classifier.predict(X_test)
133
- acc=round(accuracy_score(y_test,y_pred),6)
134
- prec=round(precision_score(y_test,y_pred,average='macro'),6)
135
-
136
- print(acc, prec)
137
-
138
- score = []
139
- for i in range(1,5):
140
- Xtrain, Xtest, ytrain, ytest = train_test_split(x, y, test_size=i/10, random_state=0)
141
- classifier = KNeighborsClassifier(n_neighbors=6)
142
- classifier.fit(Xtrain, ytrain)
143
- ypred = classifier.predict(Xtest)
144
- acc=round(accuracy_score(ytest,ypred),6)
145
- prec=round(precision_score(ytest,ypred,average='macro'),6)
146
- score.append(acc)
147
-
148
- score_rate = pd.DataFrame(data = score, columns = ['Acc'])
149
- score_rate.insert(loc=0, column = 'test_size', value=range(1,5))
150
-
151
- X = score_rate['test_size']
152
- Y = score_rate['Acc']
153
- fig = px.bar(X,Y)
154
- fig.show()
155
-
156
-
157
- score = []
158
- for i in range(1,51):
159
- clf = RandomForestClassifier(criterion = 'entropy', n_estimators = i)
160
- clf.fit(X_train,y_train)
161
- scores = clf.score(X_test,y_test)
162
- score.append(scores)
163
-
164
- score_rate = pd.DataFrame(data = score, columns = ['Score'])
165
- score_rate.insert(loc=0, column = 'n_estimators', value=range(1,51))
166
-
167
- X = score_rate['n_estimators']
168
- Y = score_rate['Score']
169
- fig = px.bar(X,Y)
170
- fig.show()
171
- print("dd")
172
- print(clf.score(X_test,y_test))
173
-
174
- #Essayons la svm
175
- from sklearn import svm
176
-
177
- # Entraîner le SVM
178
- clf = svm.SVC(kernel='linear')
179
- clf.fit(X_train, y_train)
180
-
181
- #testons la svm
182
-
183
- y_pred = clf.predict(X_test)
184
- accuracy = accuracy_score(y_test, y_pred)
185
- prec=round(precision_score(y_test,y_pred,average='macro'),6)
186
- print("Accuracy:", accuracy)
187
- print(prec)
188
-
189
- """Meilleure accuracy"""
190
-
191
- #Normaliser les données
192
- X_train_norm = (X_train - X_train.min()) / (X_train.max() - X_train.min())
193
- X_test_norm = (X_test - X_train.min()) / (X_train.max() - X_train.min())
194
-
195
- clf.fit(X_train_norm, y_train)
196
-
197
- # Tester le modèle
198
-
199
-
200
-
201
- y_pred = clf.predict(X_test_norm)
202
- accuracy = accuracy_score(y_test, y_pred)
203
- prec=round(precision_score(y_test,y_pred,average='macro'),6)
204
- print("Accuracy:", accuracy)
205
- print(prec)
206
- i=Y.max()
207
- print("Accuracy:::",i)
208
-
209
- """Prefere la version non normalise."""
210
-
211
  clf = RandomForestClassifier(criterion = 'entropy', n_estimators = 4)
212
  clf.fit(X_train,y_train)
213
  score = clf.score(X_test,y_test)
 
18
  from numpy.core.fromnumeric import shape
19
  import pandas as pd
20
  import numpy as np
 
 
 
21
 
22
 
23
+ n de pouvoir continuer notre traitement
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  from sklearn.model_selection import train_test_split
26
  #nous allons encoder les diferentes variables
 
41
 
42
  Cholesterol_encoded = label_encoder.fit_transform(df['Cholesterol'])
43
 
 
 
 
 
 
 
 
 
 
 
 
44
  df.drop("Drug", axis=1, inplace=True)
45
  df.drop("Sex", axis=1, inplace=True)
46
  df.drop("BP", axis=1, inplace=True)
47
  df.drop("Cholesterol", axis=1, inplace=True)
48
 
49
+
 
50
  df["Drug"] = Drug_encoded
51
  df['Sex'] = Sex_encoded
52
  df['BP'] = BP_encoded
 
57
  y = df['Drug']
58
 
59
 
 
 
60
  from sklearn.model_selection import train_test_split
61
  X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=0)
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  clf = RandomForestClassifier(criterion = 'entropy', n_estimators = 4)
64
  clf.fit(X_train,y_train)
65
  score = clf.score(X_test,y_test)