当前位置:   article > 正文

用knn算法对鸢尾花数据集进行分类

用knn算法对鸢尾花数据集进行分类
  1. from sklearn.datasets import load_iris
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.preprocessing import StandardScaler
  4. from sklearn.neighbors import KNeighborsClassifier
  5. def knn_selector():
  6. iris = load_iris()
  7. x_train,x_test,y_train,y_test = train_test_split(iris.data, iris.target, test_size=0.3)
  8. transfer = StandardScaler()
  9. x_train = transfer.fit_transform(x_train)
  10. x_test = transfer.transform(x_test)
  11. estimator = KNeighborsClassifier(n_neighbors = 3)
  12. estimator.fit(x_train, y_train)
  13. # estimator.predict(x_test)
  14. score = estimator.score(x_test, y_test)
  15. print("score: ", score)
  16. if __name__ == "__main__":
  17. knn_selector()

 

  1. import matplotlib.pyplot as plt
  2. from sklearn.datasets import load_iris
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.neighbors import KNeighborsClassifier
  5. from sklearn.preprocessing import StandardScaler
  6. # 获取数据集
  7. # 划分数据集
  8. # 标准化
  9. # 创建模型
  10. # 模型训练
  11. # 模型预测与评估
  12. def knn_selector():
  13. iris = load_iris()
  14. X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size= 0.3)
  15. # print(X_train)
  16. # plt.plot(X_train[:,0])
  17. # plt.show()
  18. transfer = StandardScaler()
  19. X_train = transfer.fit_transform(X_train)
  20. X_test = transfer.transform(X_test)
  21. # print(X_train[:,0])
  22. # plt.plot(X_train[:,0])
  23. # plt.show()
  24. estimator = KNeighborsClassifier(n_neighbors = 3)
  25. estimator.fit(X_train, y_train)
  26. score = estimator.score(X_test, y_test)
  27. print("准确率: ", score)
  28. if __name__ == "__main__":
  29. knn_selector()

 

  1. from sklearn import datasets
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.neighbors import KNeighborsClassifier
  4. #------------------------------引入数据------------------------------
  5. iris = datasets.load_iris() # 引入 iris 鸢尾花数据集
  6. # 鸢尾花数据集 包含 4个 特征变量
  7. iris_X = iris.data # 特征变量
  8. iris_y = iris.target # 目标值
  9. # iris['data']
  10. # iris['target']
  11. X_train, X_test, y_train, y_test = train_test_split(iris_X, iris_y, test_size=0.3)
  12. #---------------------------训练数据
  13. knn = KNeighborsClassifier() # 引入训练方法
  14. knn.fit(X_train,y_train) # 进行填充测试数据进行训练
  15. knn.predict(X_test) # 预测 特征值
  16. '''
  17. array([2, 1, 2, 0, 0, 1, 1, 2, 0, 1, 0, 2, 0, 1, 0, 2, 1, 2, 2, 2, 2, 2, 1,
  18. 1, 1, 1, 0, 2, 1, 2, 0, 1, 1, 0, 0, 2, 0, 0, 1, 0, 2, 1, 1, 2, 2])
  19. '''
  20. y_test # 真实的 特征值
  21. '''
  22. array([2, 1, 2, 0, 0, 1, 2, 2, 0, 1, 0, 2, 0, 1, 0, 2, 1, 2, 2, 2, 2, 2, 1,
  23. 1, 1, 2, 0, 2, 2, 2, 0, 1, 1, 0, 0, 2, 0, 0, 1, 0, 1, 1, 1, 2, 2])
  24. '''
  25. print(test_y)
  26. print(pre)
  27. print( sum(abs(pre - test_y)) / len(pre) )
  28. knn.score(test_X, test_y)

 

 

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

闽ICP备14008679号