-
Notifications
You must be signed in to change notification settings - Fork 0
/
iris - DT.py
63 lines (42 loc) · 1.43 KB
/
iris - DT.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
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 2 13:17:05 2018
@author: D'Costa
"""
#iris Kernel SVM
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 2 13:07:18 2018
@author: D'Costa
"""
#iris Decision Tree
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
iris = pd.read_csv("Iris_dataset.csv")
#separating the independent variables - pick all rows and all columns except
# the last one
x=iris.iloc[:,0:4].values # independent variables should always be a matrix
#the dependent variables
y=iris.iloc[:,4].values
np.set_printoptions(threshold=100)
from sklearn.preprocessing import LabelEncoder
labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(y)
from sklearn.cross_validation import train_test_split
#to match the same data in the sets, set random_state to the same number as the trainer
xtrain,xtest,ytrain,ytest = train_test_split(x,y,test_size=1/3,random_state = 0)
#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_x = StandardScaler()
xtrain = sc_x.fit_transform(xtrain)
xtest = sc_x.transform(xtest)
# Fitting Decision Tree to the Training set
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion = "entropy", random_state=0)
classifier.fit(xtrain,ytrain)
# Predicting the Test set results
ypred = classifier.predict(xtest)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(ytest, ypred)