Professional Documents
Culture Documents
Decision Tree
Decision Tree
Omkar Singh
102
A1
Decision Tree
Out[87]: stalk- st
cap- cap- cap- gill- gill- gill- gill- surface- co
class bruises odor ...
shape surface color attachment spacing size color below- abo
ring
0 p x s n t p f c n k ... s
1 e x s y t a f c b k ... s
2 e b s w t l f c b n ... s
3 p x y w t p f c n n ... s
4 e x s g f n f w b k ... s
5 rows × 23 columns
In [88]: df.drop(['cap-shape','cap-color','cap-surface','gill-attachment','gill-spacing','gi
In [89]: df.head()
0 p t p s u
1 e t a n g
2 e t l n m
3 p t p s u
4 e f n a g
In [90]: print(df)
In [91]: df.describe()
unique 2 2 4 5 5
top e t a s g
d = {'t': 1, 'f': 0}
df['bruises'] = df['bruises'].map(d)
# bruises=t,no=f
print(df)
In [98]: len(X_train)
422
Out[98]:
In [99]: len(X_test)
181
Out[99]:
In [101… model.fit(X_train,y_train)
DecisionTreeClassifier()
Out[101]:
In [105… model.score(X_test,y_test)
1.0
Out[105]:
x = df[features]
y = df['bruises']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(x, y)
tree.plot_tree(dtree, feature_names=features)