Advertisement
toweber

decision_tree_no_pruning

Aug 15th, 2022
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 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.  
  31. clf = clf.fit(X, Y)
  32.  
  33.  
  34.  
  35. # exportando de forma gráfica
  36. import graphviz
  37. dot_data = tree.export_graphviz(clf, out_file=None, filled=False, rounded=True, impurity=True,
  38. class_names=target_names,
  39.                                 feature_names=feature_names
  40. )
  41.  
  42. graph = graphviz.Source(dot_data)
  43. graph.render("graph")
  44.  
  45. # exportando de formato texto
  46. r = tree.export_text(clf)
  47. print('\n'+r)
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement