机器学习第四次作业
第一题
求解过程如下:
第二题
f-1616158471171)]
import cv2
import numpy as np
import random
import os
# 读取一张图片
def load_date_single(file_name):
im = cv2.imread(file_name, -1)
im = im.reshape(im.shape[0] * im.shape[1],)
# im = im / np.max(im)
im = np.mat(im)
return im
# 读取一个文件下下的所有图片并用矩阵返回
def load_data(file_path):
fileList = os.listdir(file_path)
X = np.empty((10, 10304))
for i in range(len(fileList)):
file = file_path + "/{}.pgm".format(i+1)
im = load_date_single(file)
X[i] = im
X = np.mat(X)
return X
if __name__ == "__main__":
name = "data/s1/2.pgm"
img = load_date_single(name)
print(type(img))
print(img.shape)
B = np.mat(np.zeros((10304, 40)))
for k in range(40):
ran = random.randint(1, 10)
path = "data/s{}/{}.pgm".format(k+1, ran)
img = load_date_single(path)
B[:, k] = img.T
print("B:")
print(B)
print(B.shape)
inp = load_data("data/s1")
print("input:")
print(inp)
print(inp.shape)