aboutsummaryrefslogtreecommitdiff
path: root/Assignment 3/ex31.py
blob: 96cbf66b952fae76324e4d9e69fba071a3274313 (plain) (blame)
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
# -*- 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.")