# -*- coding: utf-8 -*-
"""
Created on Sat Oct 13 19:26:26 2018
@author: fengjuan
"""
'''
K-近邻算法与其他模型最大不同在于该模型没有参数训练过程,即,没有通过任何学习算法训练数据而且
只是根据测试样本在训练数据的分布直接做出分类决策,因此k-近邻属于无 参数模型中非常简答的一种
'''
#使用加载器读取数据并存放在变量iris中
from sklearn.datasets import load_iris
iris=load_iris()
print(iris.data.shape)
print(iris.DESCR)
'''结果:(150, 4)
Iris Plants Database
====================
Notes
-----
Data Set Characteristics:
:Number of Instances: 150 (50 in each of three classes)
:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
- class:
- Iris-Setosa
- Iris-Versicolour
- Iris-Virginica
:Summary Statistics:
============== ==== ==== ======= ===== ====================
Min Max Mean SD Class Correlation
============== ==== ==== ======= ===== ====================
sepal length: 4.3 7.9 5.84 0.83 0.7826
sepal width: 2.0 4.4 3.05 0.43 -0.4194
petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)
petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)
============== ==== ==== ======= ===== ====================
:Missing Attribute Values: None
:Class Distribution: 33.3% for each of 3 classes.
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%[email protected])
:Date: July, 1988
This is a copy of UCI ML iris datasets.
http://archive.ics.uci.edu/ml/datasets/Iris'''
#将数据分割,25%作为测试集,75%作为训练集
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)
print(y_train.shape)
print(y_test.shape)
''':(112,)
(38,)'''
#sklearn.preprocessing中导入标准化模块
from sklearn.preprocessing import StandardScaler
ss=StandardScaler()
from sklearn.neighbors import KNeighborsClassifier
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)
from sklearn.metrics import classification_report
print('Accuracy of =K-NeighborsClassifier is:',knc.score(X_test,y_test))
print(classification_report(y_test,y_predict,target_names=iris.target_names.astype(str)))
'''结果:
Accuracy of =K-NeighborsClassifier is: 0.8947368421052632
precision recall f1-score support
setosa 1.00 1.00 1.00 8
versicolor 0.73 1.00 0.85 11
virginica 1.00 0.79 0.88 19
avg / total 0.92 0.89 0.90 38
'''