np.Linear algebra学习

本文转自相关文档,介绍了Python numpy库中线性代数的相关内容。包括分解(如svd奇异值分解)、矩阵特征值计算、范数和其他数字(如矩阵范数、条件数、行列式等)的计算,以及解方程和逆矩阵(如线性矩阵方程求解、最小二乘求解)等函数的使用。

转自:https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.linalg.html

1.分解

//其中我觉得可以的就是svd奇异值分解吧,虽然并不知道数学原理

 np.linalg.svd(afull_matrices=1compute_uv=1)

 a是要分解的(M,N)array;

full_matrices : bool, optional

If True (default), u and v have the shapes (MM) and (NN), respectively. Otherwise, the shapes are (MK) and (KN), respectively, where K = min(MN).

 当full_matrices是True时(默认):

>>> d=np.mat("4 11 14;8 7 -2")
>>> d
matrix([[ 4, 11, 14],
        [ 8,  7, -2]])
>>> U,sigma,V=np.linalg.svd(d)
>>> U
matrix([[-0.9486833 , -0.31622777],
        [-0.31622777,  0.9486833 ]])
>>> V
matrix([[-0.33333333, -0.66666667, -0.66666667],
        [ 0.66666667,  0.33333333, -0.66666667],
        [-0.66666667,  0.66666667, -0.33333333]])
>>> sigma
array([18.97366596,  9.48683298])
>>> U.shape,sigma.shape,V.shape
((2, 2), (2,), (3, 3))
>>> S=np.zeros((2,3))
>>> S[:2,:2]=np.diag(sigma)
>>> S
array([[18.97366596,  0.        ,  0.        ],
       [ 0.        ,  9.48683298,  0.        ]])
>>> U*S*V
matrix([[ 4., 11., 14.],
        [ 8.,  7., -2.]])

 

当full_matrices是False时:

>>> U,sigma,V=np.linalg.svd(d,full_matrices=0)
>>> U
matrix([[-0.9486833 , -0.31622777],
        [-0.31622777,  0.9486833 ]])
>>> sigma
array([18.97366596,  9.48683298])
>>> V
matrix([[-0.33333333, -0.66666667, -0.66666667],
        [ 0.66666667,  0.33333333, -0.66666667]])
>>> S=np.diag(sigma)#####
>>> S
array([[18.97366596,  0.        ],
       [ 0.        ,  9.48683298]])
>>> U*S*V
matrix([[ 4., 11., 14.],
        [ 8.,  7., -2.]])

 

2.矩阵特征值

np.linalg.eig(a) Compute the eigenvalues and right eigenvectors of a square array.

>>> w,v=LA.eig(np.diag((1,2,3)))
>>> w
array([1., 2., 3.])
>>> v
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
>>> np.diag((1,2,3))
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])

 

np.linalg.eigvals(g):Compute the eigenvalues of a general matrix.

>>> w2=LA.eigvals(np.diag((1,2,3)))
>>> w2
array([1., 2., 3.])

 

3.范数和其他数字

3.1 np.linalg.norm(xord=Noneaxis=Nonekeepdims=False):Matrix or vector norm.

Using the axis argument to compute vector norms:axis用来计算矩阵中的向量范数。

>>> a=np.array([3,4])
>>> a
array([3, 4])
>>> LA.norm(a)
5.0
>>> LA.norm(a,ord=1)
7.0
>>> a=np.array([3,-4])
>>> LA.norm(a,ord=1)
7.0
>>> LA.norm(a,ord=np.inf)
4.0
>>> LA.norm(a,ord=-np.inf)
3.0

 

3.2 np.linalg.cond(xp=None):Compute the condition number of a matrix.

>>> a=np.array([[1, 0, -1], [0, 1, 0], [1, 0, 1]])
>>> a
array([[ 1,  0, -1],
       [ 0,  1,  0],
       [ 1,  0,  1]])
>>> LA.cond(a)
1.4142135623730951
>>> LA.cond(a,2)
1.4142135623730951
>>> LA.cond(a,1)
2.0

 //其中:

p : {None, 1, -1, 2, -2, inf, -inf, ‘fro’}, optional

Order of the norm:

pnorm for matrices
None2-norm, computed directly using the SVD
‘fro’Frobenius norm
infmax(sum(abs(x), axis=1))
-infmin(sum(abs(x), axis=1))
1max(sum(abs(x), axis=0))
-1min(sum(abs(x), axis=0))
22-norm (largest sing. value)
-2smallest singular value

inf means the numpy.inf object, and the Frobenius norm is the root-of-sum-of-squares norm.

使用的范数,默认是L2范数。

3.3 np.linalg.det(a):Compute the determinant of an array.

>>> a = np.array([[1, 2], [3, 4]])
>>> LA.det(a)
-2.0000000000000004

 

3.4 np.linalg.matrix_rank(Mtol=None):Return matrix rank of array using SVD method

>>> LA.matrix_rank(np.eye(4))
4
>>> I=np.eye(4)
>>> I[-1,-1]=0
>>> I
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 0.]])
>>> LA.matrix_rank(I)
3

 

3.5 trace(aoffset=0axis1=0axis2=1dtype=Noneout=None)

>>> np.trace(np.eye(4))
4.0

 

矩阵对角线上的和。

4.解方程和逆矩阵

4.1 np.linalg.solve(a,b):Solve a linear matrix equation, or system of linear scalar equations.

 Solve the system of equations x0 x1 9 and x0 x1 8:

>>> a = np.array([[3,1], [1,2]])
>>> b = np.array([9,8])
>>> x = np.linalg.solve(a, b)
>>> x
array([ 2.,  3.])

 

check:

>>> np.allclose(np.dot(a, x), b)
True

 

4.2 np.linalg.lstsq(abrcond=-1):Return the least-squares solution to a linear matrix equation

最小二乘求解。

转载于:https://www.cnblogs.com/BlueBlueSea/p/10562989.html

### 线性代数在计算机科学中的应用 #### 图像处理与计算机视觉 图像本质上可以被看作矩阵,其中每个像素对应于矩阵的一个元素。通过运用线性变换,如旋转、缩放和平移,能够实现对图像的操作。这些操作通常借助于特定类型的矩阵来完成[^2]。 ```python import numpy as np def rotate_image(image_matrix, angle): rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]) rotated_image = image_matrix @ rotation_matrix return rotated_image ``` #### 数据挖掘与机器学习 在线性回归分析中,目标是最小化预测值与实际观测之间的误差平方和。这可以通过求解正规方程组或梯度下降法等方式达成。对于大规模数据集而言,采用高效的数值算法至关重要,而这些都是建立在线性代数基础之上的理论和技术。 ```python from sklearn.linear_model import LinearRegression X = [[1], [2], [3]] # 输入特征向量 y = [2, 4, 6] # 输出标签列表 model = LinearRegression() model.fit(X, y) print(f"Coefficients: {model.coef_}") print(f"Intercept: {model.intercept_}") ``` #### 计算机图形学 为了创建逼真的三维场景,在渲染过程中需要考虑光照效果以及物体间的相互作用等问题。此时会涉及到大量的矢量运算及坐标转换工作,均离不开线性代数的支持[^1]。 ```c++ // C++ code snippet demonstrating vector operations using GLM library #include <glm/glm.hpp> glm::vec3 lightDirection(0.577f, -0.577f, 0.577f); glm::vec3 normalVector(-0.894f, 0.447f, 0); float dotProduct = glm::dot(lightDirection, normalVector); // Calculate the cosine of the angle between two vectors. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值