python中的“@”与“*”运算符

1、@运算符

  @运算符是对矩阵进行矩阵乘法(即数学上的矩阵相乘)来用的。

 1.1、二维方阵

  生成二维矩阵a和b:

import numpy as np

a = np.arange(1, 10).reshape(3, 3)
print(a)
print(a.shape)
print(type(a))
print(a.dtype)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
(3, 3)

int32
b = np.array(np.arange(0, 9)).reshape(3, 3)
print(b)
print(b.shape)
print(type(b))
print(b.dtype)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
(3, 3)

int32

  1.1.1、a@b

c = a@b
print(c)
print(c.shape)
print(type(c))
print(c.dtype)
[[ 24  30  36]
 [ 51  66  81]
 [ 78 102 126]]
(3, 3)

int32

  1.1.2、类似算法

  a@b运算相当于numpy的matmul矩阵运算(也可以用np.dot()或者a.dot(),后者多用于向量的点积):

c = np.matmul(a, b)
print(c)
print(c.shape)
print(type(c))
print(c.dtype)
[[ 24  30  36]
 [ 51  66  81]
 [ 78 102 126]]
(3, 3)

int32

 1.2、三维方阵

  生成三维矩阵a和b

a = np.arange(1, 10).reshape(3, 3)
a = np.expand_dims(a, 0).repeat(2, 0)
a = a.repeat(2, axis=0)
print(a)
print(a.shape)
print(type(a))
print(a.dtype)

np.expand_dims(a,0):在矩阵a的0维度增加一个维度(从(3×3)变成(1×3×3))。

a.repeat(2,0):重复0维度的值使得矩阵a的0维度变成2个3×3(从(1×3×3)变成(2×3×3)

[[[1 2 3]
  [4 5 6]
  [7 8 9]]

 [[1 2 3]
  [4 5 6]
  [7 8 9]]]
(2, 3, 3)

int32
b = np.array(np.arange(0, 9)).reshape(3, 3)
b = np.expand_dims(b, 0)
b = b.repeat(2, axis=0)
print(b)
print(b.shape)
print(type(b))
print(b.dtype)
[[[0 1 2]
  [3 4 5]
  [6 7 8]]

 [[0 1 2]
  [3 4 5]
  [6 7 8]]]
(2, 3, 3)

int32

  1.2.1、a@b

c = a@b
print(c)
print(c.shape)
print(type(c))
print(c.dtype)
[[[ 24  30  36]
  [ 51  66  81]
  [ 78 102 126]]

 [[ 24  30  36]
  [ 51  66  81]
  [ 78 102 126]]]
(2, 3, 3)

int32

  1.2.2、类似算法 

  a@b运算相当于numpy的matmul矩阵运算(也可以用np.dot()或者a.dot(),后者多用于向量的点积):

c = np.matmul(a, b)
print(c)
print(c.shape)
print(type(c))
print(c.dtype)
[[[ 24  30  36]
  [ 51  66  81]
  [ 78 102 126]]

 [[ 24  30  36]
  [ 51  66  81]
  [ 78 102 126]]]
(2, 3, 3)

int32

 1.3、矩阵(不是方阵)

  生成矩阵a和b

a = np.arange(1, 7).reshape(2, 3)
print(a)
print(a.shape)
[[1 2 3]
 [4 5 6]]
(2, 3)
b = a.T
print(b)
print(b.shape)
[[1 4]
 [2 5]
 [3 6]]
(3, 2)

  1.3.1、a@b

c = a@b
print(c)
print(c.shape)
[[14 32]
 [32 77]]
(2, 2)

  1.3.2、类似算法 

  a@b运算相当于numpy的matmul矩阵运算(也可以用np.dot()或者a.dot(),后者多用于向量的点积):

c = np.matmul(a, b)
print(c)
print(c.shape)
[[14 32]
 [32 77]]
(2, 2)

2、*运算符

  *运算符也可以做矩阵乘法运算。(注意:这里的矩阵乘法是逐元素的乘法,而且运算符两边的变量必须是同维度的

 2.1、三维方阵

  生成三维矩阵a和b

a = np.arange(1, 10).reshape(3, 3)
a = np.expand_dims(a, 0).repeat(2, 0)
a = a.repeat(1, axis=0)
print(a)
print(a.shape)
print(type(a))
print(a.dtype)
[[[1 2 3]
  [4 5 6]
  [7 8 9]]]
(1, 3, 3)
b = np.array(np.arange(0, 9)).reshape(3, 3)
b = np.expand_dims(b, 0)
b = b.repeat(2, axis=0)
print(b)
print(b.shape)
[[[0 1 2]
  [3 4 5]
  [6 7 8]]

 [[0 1 2]
  [3 4 5]
  [6 7 8]]]
(2, 3, 3)

  2.1.1、a*b

c = a*b
print(c)
print(c.shape)
[[[ 0  2  6]
  [12 20 30]
  [42 56 72]]

 [[ 0  2  6]
  [12 20 30]
  [42 56 72]]]
(2, 3, 3)

  2.1.2、类似算法 

  a*b运算相当于numpy的multiply矩阵运算(np.multiply()括号里不管是数组还是矩阵都是逐元素相乘):

c = np.multiply(a, b)
print(c)
print(c.shape)
[[[ 0  2  6]
  [12 20 30]
  [42 56 72]]

 [[ 0  2  6]
  [12 20 30]
  [42 56 72]]]
(2, 3, 3)

 2.2、不是方阵(均报错)

a = np.arange(1, 7).reshape(2, 3)
print(a)
print(a.shape)
b = a.T
print(b)
print(b.shape)
[[1 2 3]
 [4 5 6]]
(2, 3)
[[1 4]
 [2 5]
 [3 6]]
(3, 2)

  2.2.1、a*b

c = a*b
Traceback (most recent call last):
  File "F:/shuxue.py", line 119, in 
    c = a*b
ValueError: operands could not be broadcast together with shapes (2,3) (3,2)

  2.2.1、类似算法

  a*b运算相当于numpy的multiply矩阵运算(np.multiply()括号里不管是数组还是矩阵都是逐元素相乘):

c = np.multiply(a, b)
Traceback (most recent call last):
  File "F:/shuxue.py", line 122, in 
    c = np.multiply(a, b)
ValueError: operands could not be broadcast together with shapes (2,3) (3,2) 

 2.3、(np.mat(A))*(np.mat(B))

  将数组(之前可以认为是矩阵),通过np.mat()转为矩阵matrix可以进行矩阵乘法(不是逐元素相乘)类似于a@b算法。

a = np.arange(1, 7).reshape(2, 3)
print(a)
print(a.shape)
b = a.T
print(b)
print(b.shape)
[[1 2 3]
 [4 5 6]]
(2, 3)
[[1 4]
 [2 5]
 [3 6]]
(3, 2)

  (np.mat(a))*(np.mat(b)):

c = (np.mat(a))*(np.mat(b))
print(c)
print(c.shape)
print(type(c))
[[14 32]
 [32 77]]
(2, 2)

你可能感兴趣的:(python,numpy)