吴恩达机器学习代码及相关知识点总结--ex3(1.神经网络)
作者:互联网
python矩阵乘积运算(multiply/maumul/*/@)解析
1.加载数据
def load_data(path,transpose=True):
data=sio.loadmat(path)
y=data.get("y")#before reshape:(5000, 1)
y=y.reshape(y.shape[0])
X=data.get("X")#(5000,400),(400,)
if transpose:
#向量化,让每列为一个样本变为每行是一个样本?
X=np.array([im.reshape((20,20)).T for im in X])
X=np.array([im.reshape(400) for im in X])#将样本展开为原来的(400,)
return X,y
X, y = load_data('code/ex3-neural network/ex3data1.mat')
print(X.shape)
print(y.shape)
(5000, 400)
(5000,)
2.画图
def plot_an_image(image):
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]))
- matplotlib.pyplot.matshow 矩阵可视化,plot a matrix or an array as an image。
- 在matplotlib中ticks表示的是刻度,而刻度有两层意思,一个是刻标(locs),一个是刻度标签(tick labels)。在作图时,x轴y轴都是连续的,所以刻标可以随意指定,就是在连续变量上找寻位置,而刻度标签则可以对应替换:
xticks()返回了两个对象,一个是刻标(locs),另一个是刻度标签
locs, labels = xticks()
def plot_100_image(X):
size=int(np.sqrt(X.shape[1]))
sample_idx=np.random.choice(np.arange(X.shape[0]),100)#在5000个样本里选100个
sample_images=X[sample_idx,:]
fig,ax_array=plt.subplots(nrows=10,ncols=10,sharex=True,sharey=True)
for r in range(10):
for c in range(10):
ax_array[r,c].matshow(sample_images[10*r+c].reshape((size,size)))
plt.xticks(np.array([]))
plt.yticks(np.array([]))
#绘图函数,画100张图片
- np.arange()
函数返回一个有终点和起点的固定步长的排列,如[1,2,3,4,5],起点是1,终点是5,步长为1。
参数个数情况: np.arange()函数分为一个参数,两个参数,三个参数三种情况
1)一个参数时,参数值为终点,起点取默认值0,步长取默认值1。
2)两个参数时,第一个参数为起点,第二个参数为终点,步长取默认值1。
3)三个参数时,第一个参数为起点,第二个参数为终点,第三个参数为步长。其中步长支持小数
原文链接:https://blog.csdn.net/qq_41550480/article/details/89390579 - random.choice()随机选取内容:可以从一个int数字或1维array里随机选取内容,并将选取结果放入n维array中返回。
准备数据
raw_X, raw_y = load_data('code/ex3-neural network/ex3data1.mat')
print(raw_X.shape)
print(raw_y.shape)
(5000, 400)
(5000,)
# 添加x0=1
X = np.insert(raw_X, 0, values=np.ones(raw_X.shape[0]), axis=1)#插入了第一列(全部为1)
X.shape
(5000, 401)
向量化标签
y_matrix=[]
for k in range(1,11):
y_matrix.append((raw_y==k).astype(int))
# 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_matrix[-1]为(5000,),[y_matrix[-1]]为(1,5000),y_matrix[:-1]shape为(9,5000)
y = np.array(y_matrix)
y.shape
(10, 5000)
训练一维模型
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def cost(theta,X,y):
return np.mean(-y * np.log(sigmoid(X @ theta)) - (1 - y) * np.log(1 - sigmoid(X @ theta)))
def regularized_cost(theta, X, y, lr=1):
theta_ji_to_n=theta[1:]
regularized_term=(lr/(2*len(X)))*np.sum(np.power(theta_ji_to_n,2))
return cost(theta,X,y)+regularized_term
def gradient(theta, X, y):
'''just 1 batch gradient'''
return (1 / len(X)) * X.T@ (sigmoid(X @ theta) - y)
def regularized_gradient(theta, X, y, lr=1):
theta_j1_to_n = theta[1:]
regularized_theta = (lr / len(X)) * theta_j1_to_n
regularized_term = np.concatenate([np.array([0]), regularized_theta])
return gradient(theta, X, y) + regularized_term
def logistic_regression(X,y,lr=1):
theta=np.zeros(X.shape[1])
res=opt.minimize(fun=regularized_cost,x0=theta,args=(X,y,lr),method="TNC",jac=regularized_gradient, options={'disp': True})
final_theta = res.x
return final_theta
def predict(x, theta):
prob = sigmoid(x @ theta)
return (prob >= 0.5).astype(int)
t0=logistic_regression(X,y[0])
print(t0.shape)
y_pred = predict(X, t0)
print('Accuracy={}'.format(np.mean(y[0] == y_pred)))
(401,)
Accuracy=0.9974
- numpy提供了**numpy.concatenate((a1,a2,…), axis=0)**函数。能够一次完成多个数组的拼接。其中a1,a2,…是数组类型的参数
a=np.array([1,2,3])
b=np.array([11,22,33])
c=np.array([44,55,66])
np.concatenate((a,b,c),axis=0) # 默认情况下,axis=0可以不写
array([ 1, 2, 3, 11, 22, 33, 44, 55, 66]) #对于一维数组拼接,axis的值不影响最后的结果
训练多维数据
k_theta = np.array([logistic_regression(X, y[k]) for k in range(10)])
print(k_theta.shape)
(10, 401)
prob_matrix = sigmoid(X @ k_theta.T)
np.set_printoptions(suppress=True)
print(prob_matrix.shape)
print(prob_matrix)
(5000, 10)
[[0.99577353 0. 0.00053536 … 0.0000647 0.00003916 0.00172426]
[0.99834639 0.0000001 0.00005611 … 0.00009681 0.00000291 0.00008494]
[0.99139822 0. 0.00056824 … 0.00000655 0.02655352 0.00197512]
…
[0.00000068 0.04144103 0.00321037 … 0.00012724 0.00297365 0.707625 ]
[0.00001843 0.00000013 0.00000009 … 0.00164807 0.0680994 0.86118731]
[0.02879745 0. 0.00012979 … 0.36617606 0.00498225 0.14829291]]
y_pred=np.argmax(prob_matrix,axis=1)#返回每一行最大值的索引值
print(y_pred)
[0 0 0 … 9 9 7]
y_answer = raw_y.copy()
y_answer[y_answer==10] = 0
print(classification_report(y_answer, y_pred))
precision recall f1-score support
0 0.97 0.99 0.98 500
1 0.95 0.99 0.97 500
2 0.95 0.92 0.93 500
3 0.95 0.91 0.93 500
4 0.95 0.95 0.95 500
5 0.92 0.92 0.92 500
6 0.97 0.98 0.97 500
7 0.95 0.95 0.95 500
8 0.93 0.92 0.92 500
9 0.92 0.92 0.92 500
avg / total 0.94 0.94 0.94 5000
神经网络的前馈预测
下面我们通过加载已有的权值对模型进行评价:
def load_weight(path):
data=sio.loadmat(path)
return data["Theta1"],data["Theta2"]
theta1, theta2 = load_weight('code/ex3-neural network/ex3weights.mat')
theta1.shape, theta2.shape
((25, 401), (10, 26))
因为在数据加载函数中,原始数据做了转置,然而,转置的数据与给定的参数不兼容,因为这些参数是由原始数据训练的。 所以为了应用给定的参数,我需要使用原始数据(不转置)
X, y = load_data('code/ex3-neural network/ex3data1.mat',transpose=False)
X = np.insert(X, 0, values=np.ones(X.shape[0]), axis=1) # intercept
print(X.shape)
print(y.shape)
(5000, 401)
(5000,)
feed forward prediction(前馈预测)
第一层:
a1=X#输入
z2=a1@theta1.T
z2.shape
(5000, 401)
(5000,)
第二层:
z2 = np.insert(z2, 0, values=np.ones(z2.shape[0]), axis=1)#添加一列bias
a2=sigmoid(z2)
a2.shape
(5000, 26)
z3=a2@theta2.T
a3=sigmoid(z3)
a3
array([[0.00013825, 0.0020554 , 0.00304012, …, 0.00049102, 0.00774326,
0.99622946],
[0.00058776, 0.00285027, 0.00414688, …, 0.00292311, 0.00235617,
0.99619667],
[0.00010868, 0.0038266 , 0.03058551, …, 0.07514539, 0.0065704 ,
0.93586278],
…,
[0.06278247, 0.00450406, 0.03545109, …, 0.0026367 , 0.68944816,
0.00002744],
[0.00101909, 0.00073436, 0.00037856, …, 0.01456166, 0.97598976,
0.00023337],
[0.00005908, 0.00054172, 0.0000259 , …, 0.00700508, 0.73281465,
0.09166961]])
y_pred = np.argmax(a3, axis=1) + 1 # numpy is 0 base index, +1 for matlab convention,返回沿轴axis最大值的索引,axis=1代表行
y_pred.shape
print(classification_report(y, y_pred))
precision recall f1-score support
1 0.97 0.98 0.97 500
2 0.98 0.97 0.97 500
3 0.98 0.96 0.97 500
4 0.97 0.97 0.97 500
5 0.98 0.98 0.98 500
6 0.97 0.99 0.98 500
7 0.98 0.97 0.97 500
8 0.98 0.98 0.98 500
9 0.97 0.96 0.96 500
10 0.98 0.99 0.99 500
avg / total 0.98 0.98 0.98 5000
qq_41462598 发布了5 篇原创文章 · 获赞 0 · 访问量 117 私信 关注标签:知识点,吴恩达,5000,shape,ex3,np,theta,array,500 来源: https://blog.csdn.net/qq_41462598/article/details/104553802