Python中的矩阵处理
在Python中,我们可以解决不同的矩阵操作和运算。Numpy模块为矩阵运算提供了不同的方法。
add() -将两个矩阵的元素相加。
减去() -减去两个矩阵的元素。
split() -将两个矩阵的元素相除。
乘法() -将两个矩阵的元素相乘。
dot() -它执行矩阵乘法,而不是元素明智的乘法。
sqrt() -矩阵每个元素的平方根。
sum(x,axis) -添加到矩阵中的所有元素。第二个参数是可选的,当我们要计算axis为0时的列总和,而axis为1时要计算行总和时使用它。
“ T” -执行指定矩阵的转置。
范例程式码
import numpy# Two matrices are initialized by value
x = numpy.array([[1, 2], [4, 5]])
y = numpy.array([[7, 8], [9, 10]])
# add()is used to add matrices
print ("Addition of two matrices: ")
print (numpy.add(x,y))
# subtract()is used to subtract matrices
print ("Subtraction of two matrices : ")
print (numpy.subtract(x,y))
# divide()is used to divide matrices
print ("Matrix Division : ")
print (numpy.divide(x,y))
print ("Multiplication of two matrices: ")
print (numpy.multiply(x,y))
print ("The product of two matrices : ")
print (numpy.dot(x,y))
print ("square root is : ")
print (numpy.sqrt(x))
print ("The summation of elements : ")
print (numpy.sum(y))
print ("The column wise summation : ")
print (numpy.sum(y,axis=0))
print ("The row wise summation: ")
print (numpy.sum(y,axis=1))
# using "T" to transpose the matrix
print ("Matrix transposition : ")
print (x.T)
输出结果
Addition of two matrices:[[ 8 10]
[13 15]]
Subtraction of two matrices :
[[-6 -6]
[-5 -5]]
Matrix Division :
[[0.14285714 0.25 ]
[0.44444444 0.5 ]]
Multiplication of two matrices:
[[ 7 16]
[36 50]]
The product of two matrices :
[[25 28]
[73 82]]
square root is :
[[1. 1.41421356]
[2. 2.23606798]]
The summation of elements :
34
The column wise summation :
[16 18]
The row wise summation:
[15 19]
Matrix transposition :
[[1 4]
[2 5]]
以上是 Python中的矩阵处理 的全部内容, 来源链接: utcz.com/z/341082.html