Część II — przykład z zajęć (4)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sklearn import tree
from sklearn.model_selection import cross_val_score

from sklearn.externals.six import StringIO
import pydot

lines = open(„bank.csv”,”r”).readlines()
names = lines[0].strip()

data = [] #X
target = [] #y
for x in lines[1:]:
line = x.strip().split(„;”)
data.append(line[:-1])
target.append(line[-1])

import numpy

data = numpy.array(data)
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
for i in range(len(data[0])):
data[:,i] = le.fit_transform(data[:,i])

dt = tree.DecisionTreeClassifier(max_depth=3)
dt.fit(data, target)
result = cross_val_score(dt,data,target,cv=10)
print(dt.predict([[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]))
print(sum(result)/len(result))

dot_data = StringIO()
tree.export_graphviz(dt, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())

graph[0].write_pdf(„tree.pdf”)