我需要解释这段代码,有人可以让我了解火车中发生的事情并测试代码的某些部分
#pre-processing
X_ = io.imread_collection('dataset/ATnT/s[1-9]/[1-5].pgm')
Y_ = io.imread_collection('dataset/ATnT/s[1-9]/1.pgm')
Y_test = io.imread_collection('dataset/ATnT/s[1-9]/[6-9].pgm')
train_size = 5
test_size = 4
X=[]
for im in X_:
im = resize(im, (10,5), mode='constant')
X.append(im)
Y=[]
for im in Y_:
im = resize(im, (10,5), mode='constant')
Y.append(im)
test=[]
for im in Y_test:
im = resize(im, (10,5), mode='constant')
test.append(im)
X=np.array(X)/255
Y=np.array(Y)/255
test=np.array(test)/255
# train model
x_new = []
hat_y_i = []
for num, x in enumerate(X):
x_new.append(x.flatten('F'))
if np.remainder(num + 1, train_size) == 0:
hat_beta_i = np.dot(np.dot(np.linalg.pinv(np.dot(x.T, x)), x.T), Y[np.int(num / train_size)])
hat_y_i.append(np.dot(x, hat_beta_i))
x_new = []
y_pred = []
dist = []
#test Model
for num, y in enumerate(test):
for h in hat_y_i:
d = round(np.linalg.norm(y - h),5) #round(np.linalg.norm(y - h), 2)
dist.append(d)
print('Actual Class : '+str(np.int(np.ceil((num + 1)/4)))+' / Predicted Class : '+str(np.argmin(dist) + 1) )
if np.int(num / test_size) == np.argmin(dist)
y_pred.append(Y_test[num])
dist = []''
# results
total_img = len(test)
correct = len(y_pred)
print('Total images', total_img)
print('Correctly Predicted:', correct)
print('Accuracy :' + str(round(correct/total_img * 100, 2))+'%')