You are on page 1of 4

Diabetes_diagrama_de_arbol

June 1, 2023

[76]: import pandas as pd

df = pd.read_csv('diabetes.csv')
df

[76]: Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \


0 6 148 72 35 0 33.6
1 1 85 66 29 0 26.6
2 8 183 64 0 0 23.3
3 1 89 66 23 94 28.1
4 0 137 40 35 168 43.1
.. … … … … … …
763 10 101 76 48 180 32.9
764 2 122 70 27 0 36.8
765 5 121 72 23 112 26.2
766 1 126 60 0 0 30.1
767 1 93 70 31 0 30.4

DiabetesPedigreeFunction Age Outcome


0 0.627 50 1
1 0.351 31 0
2 0.672 32 1
3 0.167 21 0
4 2.288 33 1
.. … … …
763 0.171 63 0
764 0.340 27 0
765 0.245 30 0
766 0.349 47 1
767 0.315 23 0

[768 rows x 9 columns]

[77]: X = df[list(df.columns[0:8])]
y = df[['Outcome']]

1
[78]: import plotly.graph_objects as go
import pandas as pd
import plotly.express as px

fig = go.Figure()
for feature in list(df.columns[0:8]):
fig.add_trace(go.Violin(y=df[feature],
name=feature,
box_visible=True,
meanline_visible=True))

fig.show()

[79]: import plotly.express as px


df_iris = df
fig = px.parallel_coordinates(df_iris, color="Outcome", labels={"Outcome":␣
↪"Diagnostico",

"Pregnancies": "Pregnancies", "Glucose": "Glucose",␣


↪"BloodPressure": "Blood Pressure",

"SkinThickness": "Skin Thickness", "Insulin": "Insulin",␣


↪"BMI": "BMI",

"DiabetesPedigreeFunction": "Diabetes Pedigree Function",␣


↪"Age": "Age", },
color_continuous_scale=px.colors.diverging.Tealrose,␣
↪color_continuous_midpoint=2)

fig.show()

[80]: from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0,␣


↪test_size=0.18)

[81]: from sklearn.model_selection import cross_val_score


from sklearn.tree import DecisionTreeClassifier

clf = DecisionTreeClassifier(random_state=0)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)

[81]: 0.8057553956834532

[82]: display(X_test[0:8])

Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \


661 1 199 76 43 0 42.9
122 2 107 74 30 100 33.6
113 4 76 62 0 0 34.0

2
14 5 166 72 19 175 25.8
529 0 111 65 0 0 24.6
103 1 81 72 18 40 26.6
338 9 152 78 34 171 34.2
588 3 176 86 27 156 33.3

DiabetesPedigreeFunction Age
661 1.394 22
122 0.404 23
113 0.391 25
14 0.587 51
529 0.660 31
103 0.283 24
338 0.893 33
588 1.154 52

[83]: #show predicted dataset


pd.concat([X_test.reset_index(drop=True), pd.DataFrame(clf.predict(X_test))],␣
↪axis=1)

[83]: Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \


0 1 199 76 43 0 42.9
1 2 107 74 30 100 33.6
2 4 76 62 0 0 34.0
3 5 166 72 19 175 25.8
4 0 111 65 0 0 24.6
.. … … … … … …
134 7 94 64 25 79 33.3
135 9 102 76 37 0 32.9
136 1 107 50 19 0 28.3
137 7 103 66 32 0 39.1
138 3 141 0 0 0 30.0

DiabetesPedigreeFunction Age 0
0 1.394 22 1
1 0.404 23 0
2 0.391 25 0
3 0.587 51 1
4 0.660 31 0
.. … … ..
134 0.738 41 0
135 0.665 46 1
136 0.181 29 0
137 0.344 31 1
138 0.761 27 1

[139 rows x 9 columns]

3
[84]: from sklearn import tree
from matplotlib import pyplot as plt

tree.plot_tree(clf)
plt.savefig('out.pdf')
plt.show()

[93]: %cd root/

[Errno 2] No such file or directory: 'root/'


/root

You might also like