# 这一步是必须的,要把图像转化成一维向量
def img2vector(filename):
returnVector = zeros((1,1024))
fr = open(filename)
for i in range(32):
linestr = fr.readline()
for j in range(32):
returnVector[0,32*i+j]=int(linestr[j])
return returnVector
# 获取训练了数据的图像数据,并转化为向量
def training2vetor():
trainingfileList = listdir('./trainingDigits')
m = len(trainingfileList)
trainMat = zeros((m,1024))
hwLabels = []
for i in range(m):
trainMat[i, :] = img2vector('./trainingDigits/%s' % trainingfileList)
trainingNum = int(trainingfileList.split('_')[0])
hwLabels.append(trainingNum)
return trainMat,hwLabels