查看模型评分
# 分类模型可以查看准确率来对模型进行评价 # 直接用算法模型调用score方法来实现 logistic.score(X_test,y_test) knn.score(X_test,y_test) #输出 0.9694444444444444 0.9944444444444445 # 模型在测试集上评分特别高,但是在真实环境下准确率却很低,这叫病态 # 引起这种现象的原因,主要是算法训练的过度拟合分类展示
# 展示分类的成果 # 取测试集的前100个数据 plt.figure(figsize=(12,16)) for i in range(100): axes = plt.subplot(10,10,i+1) img = X_test[i].reshape((8,8)) axes.imshow(img,cmap='gray') axes.axis('off') true = y_test[i] knn_r = y2_[i] logistic_r = y1_[i] title = 'T:'+str(true)+'\nK:'+str(knn_r) + ' L:'+str(logistic_r) axes.set_title(title) # 对logistic函数进行调参,查看参数对结果的影响 # 超参 函数级别 knn n_neibors logistic pentily C # 模型参数 f(x) = wx+b logistic = LogisticRegression(C=0.5,penalty='l1') logistic.fit(X_train,y_train) logistic.score(X_test,y_test) #0.975人脸的自动补全
导包
import numpy as np import pandas as pd from pandas import Series,DataFrame from sklearn.datasets import fetch_olivetti_faces import matplotlib.pyplot as plt %matplotlib inline提取数据
faces = fetch_olivetti_faces() data = faces.data images = faces.images target = faces.target