#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sklearn import tree
from sklearn.model_selection import cross_val_score
import pandas as pa
import numpy
import matplotlib.pyplot as plt
from sklearn.externals.six import StringIO
import pydot
import os
fNames = [„python_test/”+fn for fn in os.listdir(„python_test”) if
os.path.isfile(„python_test/”+fn)]
data = [] #X + y
for fn in fNames:
dataTmp = [l.strip().split(‚,’) for l in open(fn,’r’).readlines()]
for l in dataTmp:
data.append([int(el) for el in l])
# data += [list(map(lambda x: int(x),l.strip().split(‚,’))) for l in open(fn,’r’).readlines()]
#print(data)
df = pa.DataFrame(numpy.array(data))
print(df.describe())
print(df)
df.hist()
plt.show()
df.plot()
plt.show()
df.plot(kind=”box”)
plt.show()
from pandas.plotting import scatter_matrix as sm
sm(df,figsize=[8,6],s=500)
plt.show()
print(„ML”)
X=[] #dane, przypadki
y=[] #decyzja
for d in data:
X.append(d[:-1])
y.append(d[-1])
dt = tree.DecisionTreeClassifier(max_depth=3)
dt.fit(X,y)
result = cross_val_score(dt,X,y,cv=2)
print(sum(result)/len(result))
Część II — przykład z zajęć (3)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sklearn import tree
from sklearn.model_selection import cross_val_score
import pandas as pa
import numpy
import matplotlib.pyplot as plt
from sklearn.externals.six import StringIO
import pydot
import os
fNames = [„python_test/”+fn for fn in os.listdir(„python_test”) if
os.path.isfile(„python_test/”+fn)]
data = [] #X + y
for fn in fNames:
dataTmp = [l.strip().split(‚,’) for l in open(fn,’r’).readlines()]
for l in dataTmp:
data.append([int(el) for el in l])
# data += [list(map(lambda x: int(x),l.strip().split(‚,’))) for l in open(fn,’r’).readlines()]
#print(data)
df = pa.DataFrame(numpy.array(data))
print(df.describe())
print(df)
df.hist()
plt.show()
df.plot()
plt.show()
df.plot(kind=”box”)
plt.show()
from pandas.plotting import scatter_matrix as sm
sm(df,figsize=[8,6],s=500)
plt.show()
print(„ML”)
X=[] #dane, przypadki
y=[] #decyzja
for d in data:
X.append(d[:-1])
y.append(d[-1])
dt = tree.DecisionTreeClassifier(max_depth=3)
dt.fit(X,y)
result = cross_val_score(dt,X,y,cv=2)
print(sum(result)/len(result))
dt_data = StringIO()
tree.export_graphviz(dt,out_file=dt_data)
graphDt = pydot.graph_from_dot_data(dt_data.getvalue())
graphDt[0].write_pdf(„drzewo.pdf”)
fData=[[0,2,2],[1,2,2]]
pred=dt.predict(fData)
print(pred)