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)            

 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)

猜你喜欢

转载自www.cnblogs.com/Shinered/p/9206210.html