Advertisement
Aaaaa988

Untitled

Oct 31st, 2020
2,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. import pandas as pd
  2. import pydotplus
  3. from sklearn import tree
  4. from sklearn.model_selection import train_test_split
  5.  
  6. df = pd.read_csv('data.csv', header=0)
  7. df = df.replace('?', 0.)
  8. dt = tree.DecisionTreeClassifier(min_samples_leaf=4, max_depth=5)
  9. x = df.values[:, 0:13]
  10. y = df.values[:, 13]
  11. y = y.astype('int')
  12. for i in range(10):
  13.     train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.3)
  14.     dt.fit(train_x, train_y)
  15.     print(str(i) + '.На обучающей выборке: ', dt.score(train_x, train_y))
  16.     print(str(i) + '.На тестовой выборке: ', dt.score(test_x, test_y))
  17. gv_str = tree.export_graphviz(dt, out_file=None, feature_names=df.head()[0:0].columns[:-1])
  18. graph = pydotplus.graph_from_dot_data(gv_str)
  19. graph.write_png("tree.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement