Guest User

Untitled

a guest
Jan 24th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import numpy as np
  2. from sklearn.ensemble import RandomForestClassifier
  3.  
  4. cancer = load_breast_cancer()
  5.  
  6. cancer = load_breast_cancer()
  7. X, y, features, labels = cancer.data, cancer.target, cancer.feature_names, cancer.target_names
  8. print ('X.shape= ', X.shape)
  9. print ('y.shape= ', y.shape)
  10. X_train, X_test, y_train, y_test = train_test_split(X,y, random_state = 3)
  11. print ('X_train.shape= ',X_train.shape)
  12. print ('X_test.shape= ',X_test.shape)
  13. print ('y_train.shape= ',y_train.shape)
  14. print ('y_test.shape= ',y_test.shape)
  15. print ('features:', features)
  16. dict_names = {i:v for i,v in enumerate(labels)}
  17. print ('target names :', dict_names)
  18.  
  19. clf = RandomForestClassifier().fit (X_train, y_train)
  20. clf.score (X_train, y_train)
  21. n_estimators_list= [5,10,20]
  22. max_features_list= list(np.arange(1, X_train.shape[1]+1))
  23.  
  24. for i in range (len(n_estimators_list)):
  25. for j in range (len(max_features_list)):
  26. index = len(max_features_list)*i+j
  27. clf = RandomForestClassifier(
  28. n_estimators= n_estimators_list[i],
  29. max_features=max_features_list[j],
  30. ).fit(X_train, y_train)
  31. accuracy_train = clf.score (X_train, y_train)
  32. accuracy_test = clf.score (X_test, y_test)
  33. print ('n_estimators= {}, max_features = {}, accuracy_train = {:.3%}, accuracy_test = {:.3%}'.format (
  34. n_estimators_list[i],max_features_list[j], accuracy_train, accuracy_test))
  35.  
  36. print ('Comparing to desicion tree clf')
  37. max_depth = 3
  38. clf = DecisionTreeClassifier(
  39. criterion= 'entropy',
  40. random_state=10,
  41. max_depth=max_depth,
  42. # max_leaf_nodes=4,
  43. ).fit(X_train, y_train)
  44. print("train accuracy= {:.3%}".format(clf.score (X_train, y_train)))
  45. print("test accuracy= {:.3%}".format(clf.score (X_test, y_test)))
Add Comment
Please, Sign In to add comment