Visualizing the data
from PIL import Image import numpy as np import matplotlib.pyplot as plt import scipy.io as sio import math import random def displayData(x,y,start,number): #start为起始行,number为输出图像的行数和列数 m = int(np.sqrt(len(x[0]))) data = np.zeros((number*m, number*m)) for l in range(start,start+number): for k in range(start,start+number): h=0 for i in range((l-start)*m,(l+1-start)*m): for j in range((k-start)*m,(k+1-start)*m): data[j][i] = x[l*m+k][h] h = h + 1 data = data * 255 new_im = Image.fromarray(data.astype(np.uint8)) plt.imshow(data, cmap=plt.cm.gray, interpolation='nearest') new_im.show() return None data = sio.loadmat('ex4data1.mat') #x为5000*400,y为5000*1,theta1为25*401,theta2为10*26 y = data['y'] x = data['X'] displayData(x, y,0,30)
我是输出了从第0行开始,输成30*30的数据,相当于输出900个数据。可以自由调整。