python 基本矩阵计算整理

python 基本矩阵计算整理 numpy

import numpy as np

# 全部是1的矩阵
a = np.ones((2,3))
print ("all ones matrix\n",a)

# 全部是0的矩阵
b = np.zeros((2,3))
print ("\nall zeros matrix\n",b)

# 单位矩阵
c = np.identity((4))
print ("\nidentity matrix\n", c)

# 全部是6的矩阵
d = np.full((3,4),6)
print ("\na matrix filled with one value\n", d)

# 随机生成0-1的值填满矩阵
e = np.random.rand(3,4)
print ("\na matrix filled with random values between 0-1\n", e)

# 转置矩阵
f = np.array([[1,2,3],[4,5,6]])
print ("\nThe transpose of a matrix\n",f.T)

# 逆矩阵
g = np.array([[3,2,3],[4,5,6],[0,8,9]])
print ("\nThe inverse of a square matrix\n", np.linalg.inv(g))

# 矩阵乘法
h = np.array([[1,2,3],[2,4,2],[2,3,5]])
i = np.array([[2,3],[4,5],[3,2]])
print ("\nMatrix multiplication\n",h@i)

# 从1-100等距分10个点的矩阵
j = np.linspace(1,100,10)
# 将矩阵重新塑形
k = j.reshape((5,2))
print ("\nCreate a 1*10 array between 1-100 equally distanced\n",j)
print ("\nReshape the Matrix\n", k)

你可能感兴趣的:(1024程序员节,python,矩阵,机器学习,numpy)