#!/usr/bin/python
# coding=utf-8
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
def knn_demo():
#KNN算法对鸢尾花数据集进行分类,并用网格化搜索与交叉验证
#获取数据
iris = load_iris()
#划分数据
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=6)
#特征工程:标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer. transform(x_test)
#KNN算法预估器,网格化搜索与交叉验证
estimator = KNeighborsClassifier()
pram_dict = {"n_neighbors": [1, 3, 5, 7, 9]}
estimator = GridSearchCV(estimator, param_grid=pram_dict, cv=10)
estimator.fit(x_train, y_train)
#模型评估
#方法一:比对真实值和预测值
y_predict = estimator.predict(x_test)
print "y_predict:\n", y_predict
print "对比真实值和预测值:\n", y_test== y_predict
#方法二:计算正确率
score = estimator.score(x_test, y_test)
print "准确率:\n", score
print "最佳参数:", estimator.best_params_
print "最佳结果:", estimator.best_score_
print "最佳估计器:", estimator.best_estimator_
print "交叉验证结果:", estimator.cv_results_
return None
knn_demo()