Create sfse.ipynb
Browse files- sfse.ipynb +108 -0
sfse.ipynb
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pip install -r requirements.txt
|
| 2 |
+
import gradio
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.preprocessing import LabelEncoder
|
| 7 |
+
|
| 8 |
+
df = pd.read_csv("drug200.csv", delimiter=",")
|
| 9 |
+
# -*- coding: utf-8 -*-
|
| 10 |
+
"""pred_drugs_addict.ipynb
|
| 11 |
+
|
| 12 |
+
Automatically generated by Colaboratory.
|
| 13 |
+
|
| 14 |
+
Original file is located at
|
| 15 |
+
https://colab.research.google.com/drive/1miD2NyrOv7i41irDQgUrNs410_vZ-oy9
|
| 16 |
+
|
| 17 |
+
Programme permetttant de detecter sous quelle drogue se trouve un patient.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
from numpy.core.fromnumeric import shape
|
| 21 |
+
import pandas as pd
|
| 22 |
+
import numpy as np
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Créer un objet LabelEncoder
|
| 28 |
+
label_encoder = LabelEncoder()
|
| 29 |
+
|
| 30 |
+
# Définir la variable de catégorie à encoder
|
| 31 |
+
# Encoder les catégories en entiers
|
| 32 |
+
|
| 33 |
+
Drug_encoded = label_encoder.fit_transform(df['Drug'])
|
| 34 |
+
|
| 35 |
+
Sex_encoded = label_encoder.fit_transform(df['Sex'])
|
| 36 |
+
|
| 37 |
+
BP_encoded = label_encoder.fit_transform(df['BP'])
|
| 38 |
+
|
| 39 |
+
Cholesterol_encoded = label_encoder.fit_transform(df['Cholesterol'])
|
| 40 |
+
|
| 41 |
+
df.drop("Drug", axis=1, inplace=True)
|
| 42 |
+
df.drop("Sex", axis=1, inplace=True)
|
| 43 |
+
df.drop("BP", axis=1, inplace=True)
|
| 44 |
+
df.drop("Cholesterol", axis=1, inplace=True)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
df["Drug"] = Drug_encoded
|
| 48 |
+
df['Sex'] = Sex_encoded
|
| 49 |
+
df['BP'] = BP_encoded
|
| 50 |
+
df['Cholesterol'] = Cholesterol_encoded
|
| 51 |
+
df
|
| 52 |
+
|
| 53 |
+
x = df[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']]
|
| 54 |
+
y = df['Drug']
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
from sklearn.model_selection import train_test_split
|
| 58 |
+
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=0)
|
| 59 |
+
|
| 60 |
+
clf = RandomForestClassifier(criterion = 'entropy', n_estimators = 4)
|
| 61 |
+
clf.fit(X_train,y_train)
|
| 62 |
+
score = clf.score(X_test,y_test)
|
| 63 |
+
print(score)
|
| 64 |
+
|
| 65 |
+
def start(Age,Sex,BP,Cholesterol,Na_to_K):
|
| 66 |
+
if (Sex =='male'):
|
| 67 |
+
sex = "1"
|
| 68 |
+
if (Sex =='female'):
|
| 69 |
+
sex = "0"
|
| 70 |
+
if (BP =='LOW'):
|
| 71 |
+
bp = "1"
|
| 72 |
+
if (BP =='NORMAL'):
|
| 73 |
+
bp = "2"
|
| 74 |
+
if (BP =='HIGH'):
|
| 75 |
+
bp = "0"
|
| 76 |
+
if (Cholesterol =='LOW'):
|
| 77 |
+
ch = "1"
|
| 78 |
+
if (Cholesterol =='NORMAL'):
|
| 79 |
+
ch = "2"
|
| 80 |
+
if (Cholesterol =='HIGH'):
|
| 81 |
+
ch = "0"
|
| 82 |
+
cels = Na_to_K
|
| 83 |
+
data = []
|
| 84 |
+
data = [[Age,sex,bp,ch,cels]]
|
| 85 |
+
pred = pd.DataFrame(data, columns=['Age','Sex','BP','Cholesterol','Na_to_K'])
|
| 86 |
+
y_pred = clf.predict(pred)
|
| 87 |
+
if (y_pred ==0):
|
| 88 |
+
ch = "Drug: Y"
|
| 89 |
+
if (y_pred ==4):
|
| 90 |
+
ch = "Drug: X"
|
| 91 |
+
if (y_pred ==3):
|
| 92 |
+
ch = "Drug: C"
|
| 93 |
+
if (y_pred ==1):
|
| 94 |
+
ch = "Drug: A"
|
| 95 |
+
if (y_pred ==2):
|
| 96 |
+
ch = "Drug: B"
|
| 97 |
+
return ch
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def is_number(x):
|
| 101 |
+
return bool(re.match("^[0-9]+$", x))
|
| 102 |
+
|
| 103 |
+
input_text = gr.inputs.Textbox(label="Na_to_K")
|
| 104 |
+
sex = gr.inputs.Radio(['female', 'male'], label="Sex")
|
| 105 |
+
BP = gr.inputs.Radio(['LOW','NORMAL','HIGH'], label="BP")
|
| 106 |
+
Cholesterol = gr.inputs.Radio(['LOW','NORMAL','HIGH'], label="Cholesterol")
|
| 107 |
+
face = gr.Interface(fn=start, inputs=[gr.inputs.Slider(1, 130),sex,BP,Cholesterol,input_text],examples = [[51,"male","HIGH","LOW",5],[60,"male","HIGH","NORMAL",8.61],[69,"female","NORMAL","HIGH",10.05],[64,"female","LOW","NORMAL",25.61],[22,"male","LOW","HIGH",8.11]],cache_examples = True ,outputs=["text"])
|
| 108 |
+
face.launch()
|