# -*- coding: utf-8 -*-
"""
Created on Mon Jul 2 20:05:20 2018
@author: GY
"""
#train_test_split(*arrys,**option)
#test_size:=0.25
#train_size:=0.75
#random_state:=None
#shuffle:True,是否在splitting之前拆分数据
#stratify:None,分层。如果不是None,则数据以分层方式进行分割,将其用作类标签。
#--------------------------------------------------------------------------------------------------------------------
#k近邻分类
import mglearn
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.neighbors import KNeighborsRegressor
import numpy as np
forge=mglearn.datasets.make_forge()
mglearn.plots.plot_knn_classification(n_neighbors=3)
X,y=mglearn.datasets.make_forge()
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=0)
knn=KNeighborsClassifier(n_neighbors=3)
#用训练集对分类器进行拟合
knn.fit(X_train,y_train)
knn.score(X_test,y_test)
#决策边界
fig,axes=plt.subplots(1,3)#并列图
for n_neighbors,ax in zip([1,3,9],axes):
knn=KNeighborsClassifier(n_neighbors)
knn.fit(X,y)
mglearn.plots.plot_2d_separator(knn,X,fill=True,eps=0.5,ax=ax,alpha=0.4)#决策边界
mglearn.discrete_scatter(X[:,0],X[:,1],y,ax=ax)#散点图
#分别设置title,xlabel
ax.set_title("{}".format(n_neighbors))
ax.set_xlabel("feature 0")
ax.set_ylabel("feature 1")
axes[0].legend(loc=3)
#模型复杂度---泛化能力
cancer=load_breast_cancer()
X_train,X_test,y_train,y_test=train_test_split(cancer.data,cancer.target,stratify=cancer.target,random_state=66)
training_accuracy=[]
testing_accuracy=[]
nerghbors_settings=range(1,11)
for n in nerghbors_settings:
knn=KNeighborsClassifier(n)
knn.fit(X_train,y_train)
#训练精度
training_accuracy.append(knn.score(X_train,y_train))
#测试精度
testing_accuracy.append(knn.score(X_test,y_test))
plt.plot(nerghbors_settings,training_accuracy,label='training_accuracy')
plt.plot(nerghbors_settings,testing_accuracy,label='testing_accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Nerghbors')
plt.legend()
#---------------------------------------------------------------------------------------------------------------------
#k近邻回归
X,y=mglearn.datasets.make_wave(n_samples=40)
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=0)
reg=KNeighborsRegressor(n_neighbors=3)
reg.fit(X_train,y_train)
#评估
#回归:R^2分数----决定系数,是回归预测模型的优度度量,0--1,1(完美预测),0(常数模型,平均值)
reg.score(X_test,y_test)
fig,axes=plt.subplots(1,3,figsize=(15,4))
line=np.linspace(-3,3,1000).reshape(-1,1)
for n_neighbors,ax in zip([1,3,9],axes):
reg=KNeighborsRegressor(n_neighbors).fit(X_train,y_train)
ax.plot(line,reg.predict(line))
ax.plot(X_train,y_train,'^',c=mglearn.cm2(0),markersize=8)
ax.plot(X_test,y_test,'.',c=mglearn.cm2(1),markersize=8)
ax.set_title('{} nerghbors\n train score:{:.2f} test score:{:.2f}'.format(n_neighbors,reg.score(X_train,y_train),reg.score(X_test,y_test)))
ax.set_xlabel('Feature')
ax.set_ylabel('Target')
axes[0].legend(['Model prediction','Training data/target','Test data/target'],loc='best')