数组与数的计算
- ndarray和数的操作,实际是对数组中每个元素都和该数进行操作
- 这种行为又叫做element-wise运算。
- 这种操作比python原生结果要快的多
import numpy as np
ar1 = np.arange(20).reshape(4, 5)
ar1
---------------------------------------------------------------------
结果:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
ar1 + 2
---------------------------------------------------------------------
结果:
array([[ 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16],
[17, 18, 19, 20, 21]])
ar1 * 2
---------------------------------------------------------------------
结果:
array([[ 0, 2, 4, 6, 8],
[10, 12, 14, 16, 18],
[20, 22, 24, 26, 28],
[30, 32, 34, 36, 38]])
list1 = [1, 2, 3]
list1 * 2
---------------------------------------------------------------------
结果:[1, 2, 3, 1, 2, 3]
- 数组的广播机制
-
如果两个数组维数不相等,维数较低的数组的shape会从左开始填充1,直到和高维数组的维数匹配
-
如果两个数组维数相同,但某些维度的长度不同,那么长度为1的维度会被扩展,和另一数组的同维度的长度匹配
-
如果两个数组维数相同,但有任一维度的长度不同且不为1,则报错
a = np.arange(3)
b = np.ones((2, 3))
display(a.shape, b.shape, a, b)
------------------------------------------------------------------------------------------
结果:
(3,)
(2, 3)
array([0, 1, 2])
array([[1., 1., 1.],
[1., 1., 1.]])
- 根据原则1,a从左填1,shape变为(1, 3)
- 根据原则2,a的shape的1变为b的同维值2,则变为(2, 3)
- 这样,2个数组形状一致了
a + b
array([[1., 2., 3.],
[1., 2., 3.]])
a = np.arange(3).reshape((3, 1))
b = np.arange(3)
display(a.shape, b.shape, a, b)
---------------------------------------------------------------------
结果:
(3, 1)
(3,)
array([[0],
[1],
[2]])
array([0, 1, 2])
- 根据原则1,b的形状变为(1, 3)
- 根据原则2,b的形状变为(3, 3)
- 根据原则2,a的形状变为(3, 3)
A: B:
[[0 0 0] [ [0, 1, 2]
[1 1 1] [0, 1, 2]
[2 2 2] [0, 1, 2]
] ]
a + b
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]])
b = np.ones((3, 2))
a = np.arange(3)
display(a.shape, b.shape, a, b)
--------------------------------------------------------------------
结果:
(3,)
(3, 2)
array([0, 1, 2])
array([[1., 1.],
[1., 1.],
[1., 1.]])
a + b
--------------------------------------------------------------------
结果:ValueError Traceback (most recent call last)
<ipython-input-19-bd58363a63fc> in <module>
----> 1 a + b
ValueError: operands could not be broadcast together with shapes (3,) (3,2)
- 矩阵运算
A = np.arange(4).reshape(2, 2)
A
--------------------------------------------------------------------------------
结果:
array([[0, 1],
[2, 3]])
B = np.full((2, 2), fill_value=10)
B
--------------------------------------------------------------------
结果:
array([[10, 10],
[10, 10]])
A + B
--------------------------------------------------------------------
结果:
array([[10, 11],
[12, 13]])
A * B
--------------------------------------------------------------------
结果:
array([[ 0, 10],
[20, 30]])
A.dot(B)
--------------------------------------------------------------------
结果:
array([[10, 10],
[50, 50]])
矩阵的逆
np.linalg.inv(A)
--------------------------------------------------------------------
结果:
array([[-1.5, 0.5],
[ 1. , 0. ]])
- 案例
学生的平时成绩和期末成绩
a = np.array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
# 各占比例
b = np.array([[0.3], [0.7]])
display(a, b)
array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
array([[0.3],
[0.7]])
a.dot(b)
--------------------------------------------------------------------
结果:
array([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])