import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load
运行这段代码import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier # 加载鸢尾花数据集 iris = load_iris() X = iris.data[:, :2] # 只使用前两个特征 y = iris.target # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3, random_state=42) # 训练KNN模型 knn = KNeighborsClassifier(n_neighbors=5) knn.fit(X_train, y_train) # 可视化训练集和测试集 plt.figure(figsize=(10, 6)) plt.scatter(X_train[:, ], X_train[:, 1], c=y_train, cmap='viridis', label='Train') plt.scatter(X_test[:, ], X_test[:, 1], c=y_test, cmap='viridis', marker='x', label='Test') plt.xlabel('Sepal length') plt.ylabel('Sepal width') plt.legend() plt.show() # 可视化KNN分类结果 plt.figure(figsize=(10, 6)) h = .02 # 网格步长 x_min, x_max = X[:, ].min() - .5, X[:, ].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = knn.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap='viridis', alpha=.5) plt.scatter(X_train[:, ], X_train[:, 1], c=y_train, cmap='viridis', label='Train') plt.scatter(X_test[:, ], X_test[:, 1], c=y_test, cmap='viridis', marker='x', label='Test') plt.xlabel('Sepal length') plt.ylabel('Sepal width') plt.legend() plt.show()
相关知识
基本统计图绘制
「超级干货大放送」机器学习十二种经典模型实例
鸢尾花完整数据做聚类并用散点图显示.
鸢尾花数据集的数据可视化
对鸢尾花数据集和月亮数据集,分别采用线性LDA、k
Iris鸢尾花数据集可视化、线性回归、决策树分析、KMeans聚类分析
机器学习之对鸢尾花数据集和月亮数据集,分别采用线性LDA、k
Python数据分析
基于Jupyter 对鸢尾花数据集和月亮数据集,分别采用线性LDA、k
鸢尾花数据分析初步
原文链接: import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load https://www.huajiangbk.com/newsview1946995.html
| 上一篇: 鸢尾花数据聚类分析 | 下一篇: 朴素贝叶斯对鸢尾花数据集进行分类 |
推荐分享

- 1明日花キララ:明日花绮罗年度... 24723
- 2明日花キララ(明日花绮罗)经... 14519
- 3家庭养花知识大全 家庭养花有... 9895
- 4家庭养花风水知识 家庭养花“... 9606
- 5兰花叶子扭的是什么兰 8875
- 6五月天婷婷开心六月丁香:音乐... 8595
- 7君子兰什么品种最名贵 十大名... 8322
- 8秋天养花,掌握这5点养花知识... 8113
- 9十大致癌花卉排行榜,哪些花卉... 7920
- 10【花卉知识 】养花须知:新手... 7716




