在python中利用KNN实现对iris进行分类的方法

yipeiwu_com7年前Python基础

如下所示:

from sklearn.datasets import load_iris
 
iris = load_iris()
 
print iris.data.shape
 
from sklearn.cross_validation import train_test_split
 
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size = 0.25, random_state = 33)
 
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
 
ss = StandardScaler()
 
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)
 
knc = KNeighborsClassifier()
knc.fit(X_train, y_train)
y_predict = knc.predict(X_test)
 
print 'The accuracy of K-Nearest Neighbor Classifier is: ', knc.score(X_test, y_test)
 
from sklearn.metrics import classification_report
 
print classification_report(y_test, y_predict, target_names = iris.target_names)

以上这篇在python中利用KNN实现对iris进行分类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试

用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试

MapReduce与HDFS简介 什么是Hadoop? Google为自己的业务需要提出了编程模型MapReduce和分布式文件系统Google File System,并发布了相关论文...

python 弹窗提示警告框MessageBox的实例

需要安装pywin32模块,pip install pywin32 ##pip install pywin32 import win32api,win32con ##提醒OK...

python网络编程之多线程同时接受和发送

本文实例为大家分享了python多线程同时接受和发的具体代码,供大家参考,具体内容如下 ''' 模仿qq 同时可以发送信息和接受信息多线程 ''' from socket imp...

对Python 2.7 pandas 中的read_excel详解

导入pandas模块: import pandas as pd 使用import读入pandas模块,并且为了方便使用其缩写pd指代。 读入待处理的excel文件: df =...

使用python对多个txt文件中的数据进行筛选的方法

使用python对多个txt文件中的数据进行筛选的方法

一、问题描述 筛选出多个txt文件中需要的数据 二、数据准备 这是我自己建立的要处理的文件,里面是随意写的一些数字和字母 三、程序编写 import os def eachFi...