if __name__=='__main__': for n in n_s: for i, oneC in enumerate(C_s): for j, gamma in enumerate(gamma_s): tmp = n_component_analysis(n, oneC, gamma,X_train, y_train, X_test, y_test) accuracy.append(tmp)
保存模型
# coding=utf-8 import os import sys from PIL import Image import numpy as np from sklearn.svm import SVC import pickle from sklearn.decomposition import PCA #将数据分割训练数据与测试数据 from sklearn.model_selection import train_test_split # 随机采样20%的数据构建测试样本,其余作为训练样本 def Gain_Img(imgDir): lable = os.listdir(imgDir) OK_name=os.listdir(imgDir+'/'+lable[0]) NG_name=os.listdir(imgDir+'/'+lable[1]) print(lable) for i in range(len(OK_name)):
OK_path = imgDir + "/" + lable[0]+'/'+OK_name[i-1] OK_img = Image.open(OK_path) OK_img=OK_img.convert('L') OK_img = OK_img.resize((256,256)) out1 = OK_img.rotate(90) # 逆时针旋转45度 if not os.path.exists('rotation/{}'.format(lable[0])): os.makedirs('rotation/{}'.format(lable[0])) if not os.path.exists('rotation/{}'.format(lable[1])): os.makedirs('rotation/{}'.format(lable[1])) out1.save("rotation\\{}\\{}_90_{}.bmp".format(lable[0],lable[0],i)) OK_img.save("rotation\\{}\\{}_{}.bmp".format(lable[0],lable[0],i))