1.导入模块
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
from sklearn.svm import SVC
import pickle as pickle
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
%matplotlib inline
2.获取训练数据和测试数据
base_path = '../day14/day14/kinds-of-pictures/data_batch_'
def load_batch_data(i):file_path = base_path + str(i)with open(file_path,mode='rb') as file:data = pickle.load(file,encoding='ISO-8859-1')train = data['data']target = data['labels']return train,target
for i in range(1,6):if i == 1:train,target = load_batch_data(i)else:train_next,target_next = load_batch_data(i)train = np.concatenate((train,train_next))target = np.concatenate((target,target_next))
X_train,y_train = train[:5000],target[:5000]
x_test,y_test = train[-1000:],target[-1000:]
3.用PCA技术对数据进行降维处理
pca = PCA(n_components=0.9,whiten=True)
pca.fit(X_train,y_train)
X_train_pca = pca.transform(X_train)
x_test_pca = pca.transform(x_test)
4.使用机器学习模型进行训练和预测
svc = SVC(kernel='rbf')
svc.fit(X_train_pca,y_train)
y_pre = svc.predict(x_test_pca[:100])
5.展示效果
plt.figure(figsize=(12,18))
icon_name = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
for i in range(100):plt.subplot(10,10,i+1)plt.imshow(x_test[i].reshape(3,32,32).transpose([1,2,0]))true_name = icon_name[y_test[i]]predict_name = icon_name[y_pre[i]]title = 'T:'+true_name+'\nP:'+predict_nameplt.title(title)plt.axis('off')