+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2019-08(47)

2019-09(90)

2019-10(13)

2019-11(9)

2019-12(19)

【集成学习】随机森林实现鸢尾花分类

发布于2020-08-28 20:59     阅读(61)     评论(0)     点赞(2)     收藏(0)


0

1

2

3

4

5

6

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.colors import ListedColormap
  4. from sklearn.ensemble import RandomForestClassifier
  5. from sklearn.ensemble import ExtraTreesClassifier
  6. from sklearn.datasets import load_iris
  7. from sklearn.preprocessing import StandardScaler
  8. import matplotlib as mpl
  9. from sklearn.model_selection import train_test_split
  10. from sklearn.metrics import classification_report
  11. from sklearn.metrics import accuracy_score
  12. mpl.rcParams['font.sans-serif'] = ['SimHei']
  13. mpl.rcParams['axes.unicode_minus'] = False
  14. RF = RandomForestClassifier(n_estimators=100, n_jobs=4, oob_score=True)
  15. iris = load_iris()
  16. X1 = iris.data
  17. y = iris.target
  18. X_train, X_test, y_train, y_test = train_test_split(X1, y, test_size=0.25, random_state=0)
  19. sc = StandardScaler()
  20. X_train = sc.fit_transform(X_train)
  21. X_test = sc.transform(X_test)
  22. # classifier = RandomForestClassifier(n_estimators=10, criterion='entropy', random_state=0)
  23. RF.fit(X_train, y_train)
  24. y_pred = RF.predict(X_test)
  25. print("随机森林准确率:", accuracy_score(y_test, y_pred))
  26. print("其他指标:\n", classification_report(y_test, y_pred, target_names=['0', '1', '2']))
  27. # 混淆矩阵在二分类和多分类中的使用 https://blog.csdn.net/Orange_Spotty_Cat/article/details/80520839
  28. # 画图
  29. x = iris.data[:, :2]
  30. RF.fit(x, y)
  31. N = 50
  32. cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
  33. cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])
  34. # for weight in ['uniform', 'distance']:
  35. x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
  36. y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
  37. xx, yy = np.meshgrid(np.linspace(x_min, x_max, N), np.linspace(y_min, y_max, N))
  38. z_show = np.stack((xx.flat, yy.flat), axis=1) # 测试点
  39. # z = RF.predict(np.c_[xx.ravel(), yy.ravel()])
  40. z = RF.predict(z_show)
  41. plt.figure()
  42. plt.pcolormesh(xx, yy, z.reshape(xx.shape), shading='auto', cmap=cmap_light)
  43. plt.scatter(x[:, 0], x[:, 1], c=y, cmap=cmap_bold, edgecolors='k', s=20)
  44. plt.xlim(xx.min(), xx.max())
  45. plt.title('RandomForestClassifier')
  46. plt.show()
  47. # print('RandomForestClassifier:', RF.score(x, y))

运行效果:

可视化:

0

1

2

3

4



所属网站分类: 技术文章 > 博客

作者:dfh8374

链接: https://www.pythonheidong.com/blog/article/497119/a079bc7271caf3db9596/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

2 0
收藏该文
已收藏

评论内容:(最多支持255个字符)