{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "数据集结构: (150, 4)\n", "测试集大小: (30, 4)\n", "真实结果: [2 1 0 2 0 2 0 1 1 1 2 1 1 1 1 0 1 1 0 0 2 1 0 0 2 0 0 1 1 0]\n", "预测结果: [2 1 0 2 0 2 0 1 1 1 2 1 1 1 2 0 1 1 0 0 2 1 0 0 2 0 0 1 1 0]\n", "预测精确率: 0.9666666666666667\n" ] } ], "source": [ "from sklearn import datasets\n", "from sklearn.neighbors import KNeighborsClassifier\n", "from sklearn.model_selection import train_test_split\n", "#导入鸢尾花数据并查看数据特征\n", "iris = datasets.load_iris()\n", "print('数据集结构:',iris.data.shape)\n", "# 获取属性\n", "iris_X = iris.data\n", "# 获取类别\n", "iris_y = iris.target\n", "# 划分成测试集和训练集\n", "iris_train_X,iris_test_X,iris_train_y,iris_test_y=train_test_split(iris_X,iris_y,test_size=0.2, random_state=0)\n", "#分类器初始化\n", "knn = KNeighborsClassifier()\n", "#对训练集进行训练\n", "knn.fit(iris_train_X, iris_train_y)\n", "#对测试集数据的鸢尾花类型进行预测\n", "predict_result = knn.predict(iris_test_X)\n", "\n", "#补充程序,显示下面的程序结果\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }