diff options
Diffstat (limited to 'Assignment 3/ex31.py')
-rw-r--r-- | Assignment 3/ex31.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Assignment 3/ex31.py b/Assignment 3/ex31.py new file mode 100644 index 0000000..96cbf66 --- /dev/null +++ b/Assignment 3/ex31.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Oct 23 14:45:21 2015 + +@author: Camil Staps, s4498062 + +This is Python 2 code. +""" + +import sys +sys.path.insert(0, './packages') + +from scipy import io as sciio +from sklearn import tree +from sklearn.externals.six import StringIO +import pydot + +# 3.1.1 +wine = sciio.loadmat('./Data/wine.mat') +data = wine['X'] +clss = wine['y'] +classNames = [str(n[0][0]) for n in wine['classNames']] + +# 3.1.2 +clf = tree.DecisionTreeClassifier(min_samples_split=100, criterion='gini') +clf = clf.fit(data, clss) + +dot_data = StringIO() +tree.export_graphviz(clf, out_file=dot_data) +graph = pydot.graph_from_dot_data(dot_data.getvalue()) +graph.write_pdf('./report/ex31_wine_tree.pdf') # (that would be nice, a wine tree) + +# 3.1.3 +new_data = [6.9, 1.09, 0.06, 2.1, 0.0061, 12, 31, 0.99, 3.5, 0.64, 12] +print(classNames[clf.predict(new_data)[0]]) + +# 3.1.4 +correct = 0 +for w, c in zip(data, clss): + if clf.predict(w)[0] == c: + correct = correct + 1 +print(str((100 * correct) / len(data)) + "% classified correctly.") |