toweber

decision_tree_pruning

Aug 15th, 2022
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. from sklearn import tree
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import ipdb
  5.  
  6. X = np.random.random([500,2])
  7. Y = []
  8. for x_value in X:
  9.     y_value = 0
  10.     if (x_value[0] > 0.5) and (x_value[1] > 0.5):  # AND "logic"
  11.         y_value = 1
  12.  
  13.     #parte aleatória
  14.     if np.random.random() > 0.9:
  15.         y_value = not y_value
  16.  
  17.     Y.append(y_value)
  18.  
  19. Y = np.array(Y)        
  20.  
  21. X_array = np.asarray(X)
  22.  
  23. #plt.scatter(np.asarray(X[:,0]),X[:,1],c=Y,cmap='hsv')
  24. #plt.show()
  25.  
  26. feature_names = ['x1','x2']
  27. target_names = ['falso','verdadeiro']
  28.  
  29. clf = tree.DecisionTreeClassifier()
  30. #clf = tree.DecisionTreeClassifier(max_depth=2)
  31. #clf = tree.DecisionTreeClassifier(min_samples_split=200)
  32. #clf = tree.DecisionTreeClassifier(min_samples_leaf=100)
  33. #clf = tree.DecisionTreeClassifier(min_impurity_decrease=0.01)
  34.  
  35. clf = clf.fit(X, Y)
  36.  
  37.  
  38.  
  39. # exportando de forma gráfica
  40. import graphviz
  41. dot_data = tree.export_graphviz(clf, out_file=None, filled=False, rounded=True, impurity=True,
  42. class_names=target_names,
  43.                                 feature_names=feature_names
  44. )
  45.  
  46. graph = graphviz.Source(dot_data)
  47. graph.render("graph")
  48.  
  49. # exportando de formato texto
  50. r = tree.export_text(clf)
  51. print('\n'+r)
  52.  
Advertisement
Add Comment
Please, Sign In to add comment