-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dectree.py
78 lines (50 loc) · 1.73 KB
/
Dectree.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
# -*- coding: utf-8 -*-
"""ML_LAB_W10_DecTREE.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1G_fL2pYcYqd7Zcw6VEr0jJKr3nTxEEXH
"""
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.read_csv('/content/data4_19.csv')
df.head()
df = pd.DataFrame(iris.data,columns=iris.feature_names)
df.head()
df['target'] = iris.target
df.head()
df['flower_name'] =df.target.apply(lambda x: iris.target_names[x])
df.head()
X = df.drop(['target','flower_name'],axis=1)
y = df['target']
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25)
print(X_train.shape,X_test.shape)
from sklearn import tree
model = tree.DecisionTreeClassifier()
model.fit(X_train,y_train)
model.score(X_test,y_test)
flower = {0:'Iris-setosa',2:'Iris-virginica',1:'Iris-Versicolor'}
flower[model.predict([[5.2,3.1,1.4,0.2]])[0]]
pdf = pd.read_csv('/content/golf-dataset.csv')
pdf.head()
pdf['Play Golf'] = pdf['Play Golf'].map({'No':0,'Yes':1})
pdf.head()
from sklearn import tree
X = pdf.drop('Play Golf',axis=1)
y = pdf['Play Golf']
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25)
model = tree.DecisionTreeClassifier()
model.fit(X_train,y_train)
model.score(X_test,y_test)
pdf.head()
pdf['TEMPERATUR'].unique()
pdf['Temp'] = pdf['Temp'].map({'Hot':0,'Mild':1,'Cool':2})
pdf['OUTLOOK'].unique()
pdf['Outlook'] = pdf['Outlook'].map({'Rainy':0,'Overcast':1,'Sunny':2})
pdf['WINDY'] = pdf['WINDY'].map({'False':0,'True':1})
pdf['HUMIDITY'].unique()
pdf['Humidity'] = pdf['Humidity'].map({'High':0,'Normal':1})
pdf['Windy'].unique
pdf.Windy = pdf.Windy.replace({True: 1, False: 0})