机器学习过程中需要应用Python Numpy,会经常调用一些API,所以在这里做一些记录,Mark Mark
From 20200521 to Future
[20200521]
今天在学习Matrix Cal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import numpy as np data = np.array([ [80, 200], [95, 230], [104, 245], [112, 274], [125, 259], [135, 262], ]) feature = data[:,0:1] #切每一行的第一项,保留原维度 # print(np.expand_dims(data[:, 0], axis=1)) label = data[:,-1:] #切每一行的第二项,保留原维度 # print(np.expand_dims(data[:, -1], axis=1)) m = 1 b= 1 weight = np.array([ [m], [b] ]) featureMatrix = np.append(feature, (np.ones(shape=(6, 1))),axis=1) print(np.dot(featureMatrix, weight)) dMatrix = np.dot(featureMatrix, weight) - label print(dMatrix) print(np.dot(featureMatrix.T, dMatrix)*2/len(feature)) |
应用1:对于一个array组,如何切得每一行的某一项
1 | np.expand_dims(data[:, 0], axis=1) #每一行第一列,保留原维度 |
原API代码:
1 | def expand_dims(a, axis): |
Expand the shape of an array.
Insert a new axis that will appear at the
a : array_like Input array.
axis : int or tuple of ints
Position in the expanded axes where the new axis (or axes) is placed.
应用2:如何增加一个矩阵的维度?
例如从(6, 1)->(6, 2) .
1 | np.append(feature, (np.ones(shape=(6, 1))),axis=1) |
可将feature后边增加一列1,变成(6,2)
1 | def ones(shape, dtype=None, order='C'): |
Return a new array of given shape and type, filled with ones.
1 | def append(arr, values, axis=None): |
Append values to the end of an array.