降维算法PCA及其应用
利用PCA算法实现手写字体识别,要求:
1、实现手写数字数据集的降维;
2、比较两个模型(64维和10维)的准确率;
3、对两个模型分别进行10次10折交叉验证,绘制评分对比曲线。
实验步骤
1. 导入数据集
from sklearn.datasets import load_digits
digits = load_digits()
train = digits.data
target = digits.target
2. 实现手写数字数据集的降维;
pca = PCA(n_components=10,whiten=True)
pca.fit(x_train,y_train)
x_train_pca = pca.transform(x_train)
x_test_pca = pca.transform(x_test)
3. 比较两个模型(64维和10维)的准确率;
64维
svc = SVC(kernel = 'rbf')
svc.fit(x_train,y_train)
y_predict = svc.predict(x_test)
print('The Accuracy of SVC is', svc.score(x_test, y_test))
print("classification report of SVC\n",classification_report(y_test, y_predict,
target_names=digits.target_names.astype(str)))
10维
svc = SVC(kernel = 'rbf')
svc.fit(x_train_pca,y_train)
y_pre_svc = svc.predict(x_test_pca)
print("The Accuracy of PCA_SVC is ", svc.score(x_test_pca,y_test))
print("classification report of PCA_SVC\n", classification_report(y_test, y_pre_svc,
target_names=digits.target_names.astype(str)))
4. 对两个模型分别进行10次10折交叉验证,绘制评分对比曲线。
for i in range(100):
plt.subplot(10,10,i+1)
plt.imshow(samples[i].reshape(8,8),cmap='gray')
title = str(y_pre[i])
plt.title(title,color='red')
plt.axis('off')
plt.show()
代码详解
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_digits
digits = load_digits()
train = digits.data
target = digits.target
x_train,x_test,y_train,y_test = train_test_split(train,target,test_size=0.2,random_state=33)
ss = StandardScaler()
x_train = ss.fit_transform(x_train)
x_test = ss.transform(x_test)
svc = SVC(kernel = 'rbf')
svc.fit(x_train,y_train)
y_predict = svc.predict(x_test)
print('The Accuracy of SVC is', svc.score(x_test, y_test))
print("classification report of SVC\n",classification_report(y_test, y_predict,
target_names=digits.target_names.astype(str)))
pca = PCA(n_components=10,whiten=True)
pca.fit(x_train,y_train)
x_train_pca = pca.transform(x_train)
x_test_pca = pca.transform(x_test)
svc = SVC(kernel = 'rbf')
svc.fit(x_train_pca,y_train)
y_pre_svc = svc.predict(x_test_pca)
print("The Accuracy of PCA_SVC is ", svc.score(x_test_pca,y_test))
print("classification report of PCA_SVC\n", classification_report(y_test, y_pre_svc,
target_names=digits.target_names.astype(str)))
samples = x_test[:100]
y_pre = y_pre_svc[:100]
plt.figure(figsize=(12,38))
for i in range(100):
plt.subplot(10,10,i+1)
plt.imshow(samples[i].reshape(8,8),cmap='gray')
title = str(y_pre[i])
plt.title(title)
plt.axis('off')
plt.show()
结果:
SVC
PCA
|