python numpy小记1
1.参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.outer.html#numpy.outer
向量$b=[1,2,3]^T$,求$bb^T$为3x3的矩阵。因为numpy.dot得到的是向量的内积。
为计算$bb^T$,使用numpy.outer命令。故可以得到3x3的矩阵。
2.reshape(-1,1)的用法。变成一列,行数自动得到。得到2*1的维度。
x = np.array([1,2]).reshape(-1, 1)
2.1关于np.array()直接‘’=‘’号赋值,对新array的改变将直接改变旧array的值。故使用copy()方法。
old = np.array([[1, 1, 1], [1, 1, 1]]) new = old new[0, :2] = 0 print(old) """ 我们得到的输出为: [ [0 0 1], [1 1 1] ] """
使用copy()方法:
old = np.array([[1, 1, 1], [1, 1, 1]]) new = old.copy() new[:, 0] = 0 print(old) """输出为原来的值: [[1 1 1], [1 1 1] ] ""
2.2 np.array() 的遍历:
test = np.random.randint(0, 10, (4,3)) """ >>> test array([[4, 7, 3], [8, 5, 7], [1, 4, 8], [1, 9, 8]]) """ for row in test: print(row) """ output: [4 7 3] [8 5 7] [1 4 8] [1 9 8] """ for i in range(len(test)): print i """ output: 0 1 2 3 """
for i, row in enumerate(test): print('row', i,'is', row ) """output: ('row', 0, 'is', array([4, 7, 3])) ('row', 1, 'is', array([8, 5, 7])) ('row', 2, 'is', array([1, 4, 8])) ('row', 3, 'is', array([1, 9, 8])) """
test2 = test**2 """ output: >>> test2 array([[16, 49, 9], [64, 25, 49], [ 1, 16, 64], [ 1, 81, 64]]) """
同时遍历这两个array,可以使用zip()
for i, j in zip(test, test2): print i, "+" , j,'=', i+j """ [4 7 3] + [16 49 9] = [20 56 12] [8 5 7] + [64 25 49] = [72 30 56] [1 4 8] + [ 1 16 64] = [ 2 20 72] [1 9 8] + [ 1 81 64] = [ 2 90 72] """
3. 投影矩阵
1.1.将$x$投影到$b$的向量为: $\frac{bb^T}{ {\lVert b \rVert}^2} x$ ,投影矩阵为 $\frac{bb^T}{ {\lVert b \rVert}^2}$
def projection_matrix_1d(b): """Compute the projection matrix onto the space spanned by `b` Args: b: ndarray of dimension (D,), the basis for the subspace Returns: P: the projection matrix """ D, = b.shape P = np.eye(D) # EDIT THIS P = np.outer(b, b.T) / np.dot(b,b) return P
投影向量:
def project_1d(x, b): """Compute the projection matrix onto the space spanned by `b` Args: x: the vector to be projected b: ndarray of dimension (D,), the basis for the subspace Returns: y: projection of x in space spanned by b """ p = np.zeros(3) # EDIT THIS P = np.dot(projection_matrix_1d(b),x) return p
2.1 将$x$投影到多维空间,设该多维空间的基为 $[b_1, b_2...b_M]$,可视为DxM维矩阵。得到的投影矩阵为 $B (B^TB)^{-1} B^T$。
def projection_matrix_general(B): """Compute the projection matrix onto the space spanned by `B` Args: B: ndarray of dimension (D, M), the basis for the subspace Returns: P: the projection matrix """ P = np.eye(B.shape[0]) # EDIT THIS P = np.dot( np.dot( B, np.linalg.pinv(B.T, B)), B.T ) return P
2.2 得到的投影向量计算为 $B (B^TB)^{-1} B^T x$
def project_general(x, B): """Compute the projection matrix onto the space spanned by `B` Args: B: ndarray of dimension (D, E), the basis for the subspace Returns: y: projection of x in space spanned by b """ p = np.zeros(x.shape) # EDIT THIS P = np.dot( projection_matrix_general(B), x ) return p
3.关于向量乘法中@ 符号的用法。
参考来源:https://legacy.python.org/dev/peps/pep-0465/
import numpy as np from numpy.linalg import inv, solve # Using dot function: S = np.dot((np.dot(H, beta) - r).T, np.dot(inv(np.dot(np.dot(H, V), H.T)), np.dot(H, beta) - r)) # Using dot method: S = (H.dot(beta) - r).T.dot(inv(H.dot(V).dot(H.T))).dot(H.dot(beta) - r)
如果使用@操作符。
#With the @ operator, the direct translation of the above formula #becomes: S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)
The Safest Way to Get what you Want is to Try and Deserve What you Want.