# neural network(神经网络)import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import matplotlib
import scipy.optimize as opt
from sklearn.metrics import classification_report#这个包是评价报告
defload_data(path, transpose=True):
data = sio.loadmat(path)
y = data.get('y')# (5000,1)
y = y.reshape(y.shape[0])# make it back to column vector
X = data.get('X')# (5000,400)if transpose:# for this dataset, you need a transpose to get the orientation right
X = np.array([im.reshape((20,20)).T for im in X])# and I flat the image again to preserve the vector presentation
X = np.array([im.reshape(400)for im in X])return X, y
X, y = load_data('ex3data1.mat')print(X.shape)print(y.shape)
defplot_an_image(image):# """# image : (400,)# """
fig, ax = plt.subplots(figsize=(1,1))
ax.matshow(image.reshape((20,20)), cmap=matplotlib.cm.binary)
plt.xticks(np.array([]))# just get rid of ticks
plt.yticks(np.array([]))
#绘图函数
pick_one = np.random.randint(0,5000)
plot_an_image(X[pick_one,:])
plt.show()print('this should be {}'.format(y[pick_one]))
defplot_100_image(X):""" sample 100 image and show them
assume the image is square
X : (5000, 400)
"""
size =int(np.sqrt(X.shape[1]))# sample 100 image, reshape, reorg it
sample_idx = np.random.choice(np.arange(X.shape[0]),100)# 100*400
sample_images = X[sample_idx,:]
fig, ax_array = plt.subplots(nrows=10, ncols=10, sharey=True, sharex=True, figsize=(8,8))for r inrange(10):for c inrange(10):
ax_array[r, c].matshow(sample_images[10* r + c].reshape((size, size)),
cmap=matplotlib.cm.binary)
plt.xticks(np.array([]))
plt.yticks(np.array([]))#绘图函数,画100张图片
plot_100_image(X)
plt.show()
# 准备数据# add intercept=1 for x0
X = np.insert(raw_X,0, values=np.ones(raw_X.shape[0]), axis=1)#插入了第一列(全部为1)
X.shape
# y have 10 categories here. 1..10, they represent digit 0 as category 10 because matlab index start at 1# I'll ditit 0, index 0 again
y_matrix =[]for k inrange(1,11):
y_matrix.append((raw_y == k).astype(int))# 见配图 "向量化标签.png"# last one is k==10, it's digit 0, bring it to the first position,最后一列k=10,都是0,把最后一列放到第一列
y_matrix =[y_matrix[-1]]+ y_matrix[:-1]
y = np.array(y_matrix)
y.shape
# 扩展 5000*1 到 5000*10# 比如 y=10 -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]: ndarray# """
y
# train 1 model(训练一维模型)defcost(theta, X, y):''' cost fn is -l(theta) for you to minimize'''return np.mean(-y * np.log(sigmoid(X @ theta))-(1- y)* np.log(1- sigmoid(X @ theta)))
# train k model(训练k维模型)
k_theta = np.array([logistic_regression(X, y[k])for k inrange(10)])print(k_theta.shape)'''# 进行预测
* think about the shape of k_theta, now you are making $X\times\theta^T$
> $(5000, 401) \times (10, 401).T = (5000, 10)$
* after that, you run sigmoid to get probabilities and for each row, you find the highest prob as the answer
'''