-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiris.py
More file actions
52 lines (37 loc) · 1.36 KB
/
Copy pathiris.py
File metadata and controls
52 lines (37 loc) · 1.36 KB
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
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
import numpy as np
iris = load_iris()
# Pick 50 test examples at random
testID = np.random.choice(150, 50)
trainX = np.delete(iris.data, testID, axis = 0)
trainY = np.delete(iris.target, testID)
testX = iris.data[testID]
testY = iris.target[testID]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(trainX, trainY)
prediction = clf.predict(testX)
correct = [1 if a == b else 0 for (a, b) in zip(prediction, testY)]
print "Decision Tree"
print prediction
print testY
print "Accuracy = ", np.sum(correct)*100.0/len(correct)
# Random Forest Classifier
model = RandomForestClassifier(n_estimators = 1000)
model.fit(trainX, trainY)
prediction = model.predict(testX)
correct = [1 if a == b else 0 for (a, b) in zip(prediction, testY)]
print "Random Forest"
print prediction
print testY
print "Accuracy = ", np.sum(correct)*100.0/len(correct)
# Visualizing the tree
# from IPython.display import Image
# dot_data = tree.export_graphviz(clf, out_file=None,
# feature_names=iris.feature_names,
# class_names=iris.target_names,
# filled=True, rounded=True,
# special_characters=True)
# graph = pydotplus.graph_from_dot_data(dot_data)
# Image(graph.create_png())