当前位置:   article > 正文

经典的鸢尾花分类_侃一下鸢尾的常见分类

侃一下鸢尾的常见分类

数据的分类和处理是以后更要加强学习的部分,今天先把代码贴在这了,算是立一个flag

  1. # -*- coding:utf-8 -*-
  2. import pandas as pd
  3. import numpy as np
  4. from sklearn.decomposition import PCA
  5. from sklearn.feature_selection import SelectKBest, SelectPercentile, chi2
  6. from sklearn.linear_model import LogisticRegressionCV
  7. from sklearn import metrics
  8. from sklearn.model_selection import train_test_split
  9. from sklearn.pipeline import Pipeline
  10. from sklearn.preprocessing import PolynomialFeatures
  11. from sklearn.manifold import TSNE
  12. import matplotlib as mpl
  13. import matplotlib.pyplot as plt
  14. import matplotlib.patches as mpatches
  15. def extend(a, b):
  16. return 1.05*a-0.05*b, 1.05*b-0.05*a
  17. if __name__ == '__main__':
  18. stype = 'chi2'
  19. pd.set_option('display.width', 200)
  20. data = pd.read_csv('iris.data', header=None)
  21. # columns = np.array(['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'type'])
  22. columns = np.array(['花萼长度', '花萼宽度', '花瓣长度', '花瓣宽度', '类型'])
  23. data.rename(columns=dict(list(zip(np.arange(5), columns))), inplace=True)
  24. data['类型'] = pd.Categorical(data['类型']).codes
  25. print(data.head(5))
  26. x = data[columns[:-1]]
  27. y = data[columns[-1]]
  28. if stype == 'pca':
  29. pca = PCA(n_components=2, whiten=True, random_state=0)
  30. x = pca.fit_transform(x)
  31. print('各方向方差:', pca.explained_variance_)
  32. print('方差所占比例:', pca.explained_variance_ratio_)
  33. x1_label, x2_label = '组分1', '组分2'
  34. title = '鸢尾花数据PCA降维'
  35. else:
  36. fs = SelectKBest(chi2, k=2)
  37. # fs = SelectPercentile(chi2, percentile=60)
  38. fs.fit(x, y)
  39. idx = fs.get_support(indices=True)
  40. print('fs.get_support() = ', idx)
  41. x = x[columns[idx]]
  42. x = x.values # 为下面使用方便,DataFrame转换成ndarray
  43. x1_label, x2_label = columns[idx]
  44. title = '鸢尾花数据特征选择'
  45. print(x[:5])
  46. cm_light = mpl.colors.ListedColormap(['#77E0A0', '#FF8080', '#A0A0FF'])
  47. cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b'])
  48. mpl.rcParams['font.sans-serif'] = 'SimHei'
  49. mpl.rcParams['axes.unicode_minus'] = False
  50. plt.figure(facecolor='w')
  51. plt.scatter(x[:, 0], x[:, 1], s=30, c=y, marker='o', cmap=cm_dark)
  52. plt.grid(b=True, ls=':')
  53. plt.xlabel(x1_label, fontsize=14)
  54. plt.ylabel(x2_label, fontsize=14)
  55. plt.title(title, fontsize=18)
  56. # plt.savefig('1.png')
  57. plt.show()
  58. x, x_test, y, y_test = train_test_split(x, y, train_size=0.7)
  59. model = Pipeline([
  60. ('poly', PolynomialFeatures(degree=2, include_bias=True)),
  61. ('lr', LogisticRegressionCV(Cs=np.logspace(-3, 4, 8), cv=5, fit_intercept=False))
  62. ])
  63. model.fit(x, y)
  64. print('最优参数:', model.get_params('lr')['lr'].C_)
  65. y_hat = model.predict(x)
  66. print('训练集精确度:', metrics.accuracy_score(y, y_hat))
  67. y_test_hat = model.predict(x_test)
  68. print('测试集精确度:', metrics.accuracy_score(y_test, y_test_hat))
  69. N, M = 500, 500 # 横纵各采样多少个值
  70. x1_min, x1_max = extend(x[:, 0].min(), x[:, 0].max()) # 第0列的范围
  71. x2_min, x2_max = extend(x[:, 1].min(), x[:, 1].max()) # 第1列的范围
  72. t1 = np.linspace(x1_min, x1_max, N)
  73. t2 = np.linspace(x2_min, x2_max, M)
  74. x1, x2 = np.meshgrid(t1, t2) # 生成网格采样点
  75. x_show = np.stack((x1.flat, x2.flat), axis=1) # 测试点
  76. y_hat = model.predict(x_show) # 预测值
  77. y_hat = y_hat.reshape(x1.shape) # 使之与输入的形状相同
  78. plt.figure(facecolor='w')
  79. plt.pcolormesh(x1, x2, y_hat, cmap=cm_light) # 预测值的显示
  80. plt.scatter(x[:, 0], x[:, 1], s=30, c=y, edgecolors='k', cmap=cm_dark) # 样本的显示
  81. plt.xlabel(x1_label, fontsize=14)
  82. plt.ylabel(x2_label, fontsize=14)
  83. plt.xlim(x1_min, x1_max)
  84. plt.ylim(x2_min, x2_max)
  85. plt.grid(b=True, ls=':')
  86. # 画各种图
  87. # a = mpl.patches.Wedge(((x1_min+x1_max)/2, (x2_min+x2_max)/2), 1.5, 0, 360, width=0.5, alpha=0.5, color='r')
  88. # plt.gca().add_patch(a)
  89. patchs = [mpatches.Patch(color='#77E0A0', label='Iris-setosa'),
  90. mpatches.Patch(color='#FF8080', label='Iris-versicolor'),
  91. mpatches.Patch(color='#A0A0FF', label='Iris-virginica')]
  92. plt.legend(handles=patchs, fancybox=True, framealpha=0.8, loc='lower right')
  93. plt.title('鸢尾花Logistic回归分类效果', fontsize=17)
  94. plt.show()

iris.data文本内容:

5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
5.0,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa
4.7,3.2,1.6,0.2,Iris-setosa
4.8,3.1,1.6,0.2,Iris-setosa
5.4,3.4,1.5,0.4,Iris-setosa
5.2,4.1,1.5,0.1,Iris-setosa
5.5,4.2,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.0,3.2,1.2,0.2,Iris-setosa
5.5,3.5,1.3,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
4.4,3.0,1.3,0.2,Iris-setosa
5.1,3.4,1.5,0.2,Iris-setosa
5.0,3.5,1.3,0.3,Iris-setosa
4.5,2.3,1.3,0.3,Iris-setosa
4.4,3.2,1.3,0.2,Iris-setosa
5.0,3.5,1.6,0.6,Iris-setosa
5.1,3.8,1.9,0.4,Iris-setosa
4.8,3.0,1.4,0.3,Iris-setosa
5.1,3.8,1.6,0.2,Iris-setosa
4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
5.0,3.3,1.4,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4.0,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
5.7,2.8,4.5,1.3,Iris-versicolor
6.3,3.3,4.7,1.6,Iris-versicolor
4.9,2.4,3.3,1.0,Iris-versicolor
6.6,2.9,4.6,1.3,Iris-versicolor
5.2,2.7,3.9,1.4,Iris-versicolor
5.0,2.0,3.5,1.0,Iris-versicolor
5.9,3.0,4.2,1.5,Iris-versicolor
6.0,2.2,4.0,1.0,Iris-versicolor
6.1,2.9,4.7,1.4,Iris-versicolor
5.6,2.9,3.6,1.3,Iris-versicolor
6.7,3.1,4.4,1.4,Iris-versicolor
5.6,3.0,4.5,1.5,Iris-versicolor
5.8,2.7,4.1,1.0,Iris-versicolor
6.2,2.2,4.5,1.5,Iris-versicolor
5.6,2.5,3.9,1.1,Iris-versicolor
5.9,3.2,4.8,1.8,Iris-versicolor
6.1,2.8,4.0,1.3,Iris-versicolor
6.3,2.5,4.9,1.5,Iris-versicolor
6.1,2.8,4.7,1.2,Iris-versicolor
6.4,2.9,4.3,1.3,Iris-versicolor
6.6,3.0,4.4,1.4,Iris-versicolor
6.8,2.8,4.8,1.4,Iris-versicolor
6.7,3.0,5.0,1.7,Iris-versicolor
6.0,2.9,4.5,1.5,Iris-versicolor
5.7,2.6,3.5,1.0,Iris-versicolor
5.5,2.4,3.8,1.1,Iris-versicolor
5.5,2.4,3.7,1.0,Iris-versicolor
5.8,2.7,3.9,1.2,Iris-versicolor
6.0,2.7,5.1,1.6,Iris-versicolor
5.4,3.0,4.5,1.5,Iris-versicolor
6.0,3.4,4.5,1.6,Iris-versicolor
6.7,3.1,4.7,1.5,Iris-versicolor
6.3,2.3,4.4,1.3,Iris-versicolor
5.6,3.0,4.1,1.3,Iris-versicolor
5.5,2.5,4.0,1.3,Iris-versicolor
5.5,2.6,4.4,1.2,Iris-versicolor
6.1,3.0,4.6,1.4,Iris-versicolor
5.8,2.6,4.0,1.2,Iris-versicolor
5.0,2.3,3.3,1.0,Iris-versicolor
5.6,2.7,4.2,1.3,Iris-versicolor
5.7,3.0,4.2,1.2,Iris-versicolor
5.7,2.9,4.2,1.3,Iris-versicolor
6.2,2.9,4.3,1.3,Iris-versicolor
5.1,2.5,3.0,1.1,Iris-versicolor
5.7,2.8,4.1,1.3,Iris-versicolor
6.3,3.3,6.0,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3.0,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
6.5,3.0,5.8,2.2,Iris-virginica
7.6,3.0,6.6,2.1,Iris-virginica
4.9,2.5,4.5,1.7,Iris-virginica
7.3,2.9,6.3,1.8,Iris-virginica
6.7,2.5,5.8,1.8,Iris-virginica
7.2,3.6,6.1,2.5,Iris-virginica
6.5,3.2,5.1,2.0,Iris-virginica
6.4,2.7,5.3,1.9,Iris-virginica
6.8,3.0,5.5,2.1,Iris-virginica
5.7,2.5,5.0,2.0,Iris-virginica
5.8,2.8,5.1,2.4,Iris-virginica
6.4,3.2,5.3,2.3,Iris-virginica
6.5,3.0,5.5,1.8,Iris-virginica
7.7,3.8,6.7,2.2,Iris-virginica
7.7,2.6,6.9,2.3,Iris-virginica
6.0,2.2,5.0,1.5,Iris-virginica
6.9,3.2,5.7,2.3,Iris-virginica
5.6,2.8,4.9,2.0,Iris-virginica
7.7,2.8,6.7,2.0,Iris-virginica
6.3,2.7,4.9,1.8,Iris-virginica
6.7,3.3,5.7,2.1,Iris-virginica
7.2,3.2,6.0,1.8,Iris-virginica
6.2,2.8,4.8,1.8,Iris-virginica
6.1,3.0,4.9,1.8,Iris-virginica
6.4,2.8,5.6,2.1,Iris-virginica
7.2,3.0,5.8,1.6,Iris-virginica
7.4,2.8,6.1,1.9,Iris-virginica
7.9,3.8,6.4,2.0,Iris-virginica
6.4,2.8,5.6,2.2,Iris-virginica
6.3,2.8,5.1,1.5,Iris-virginica
6.1,2.6,5.6,1.4,Iris-virginica
7.7,3.0,6.1,2.3,Iris-virginica
6.3,3.4,5.6,2.4,Iris-virginica
6.4,3.1,5.5,1.8,Iris-virginica
6.0,3.0,4.8,1.8,Iris-virginica
6.9,3.1,5.4,2.1,Iris-virginica
6.7,3.1,5.6,2.4,Iris-virginica
6.9,3.1,5.1,2.3,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
6.8,3.2,5.9,2.3,Iris-virginica
6.7,3.3,5.7,2.5,Iris-virginica
6.7,3.0,5.2,2.3,Iris-virginica
6.3,2.5,5.0,1.9,Iris-virginica
6.5,3.0,5.2,2.0,Iris-virginica
6.2,3.4,5.4,2.3,Iris-virginica
5.9,3.0,5.1,1.8,Iris-virginica

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/131011
推荐阅读
相关标签
  

闽ICP备14008679号