K-近鄰演算法-iris資料集
# -*- 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
'''