发布时间: 2023-7-20 文章作者: myluzh 分类名称: Python 朗读文章
import numpy as np arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]]) """ arr + 1 array([[2, 3, 4, 3, 2, 5], [6, 7, 2, 3, 4, 2]]) arr / 2 array([[0.5, 1. , 1.5, 1. , 0.5, 2. ], [2.5, 3. , 0.5, 1. , 1.5, 0.5]]) """ # 可以对比python列表的运算,看出区别 a = [1, 2, 3, 4, 5] """ a*3 Out[3]: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] """
import numpy as np arr1 = np.array([[0], [1], [2], [3]]) """ arr1.shape Out[3]: (4, 1) """ arr2 = np.array([1, 2, 3]) """ arr2.shape Out[4]: (3,) """ arr1 + arr2 """ 结果为: array([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]) """上述代码中,数组arr1是4行1列,ar2是1行3列。这两个数组要进行相加,按照广播机制会对数组arr1和arr2都进行扩展,使得数组arr1和arr2都变成4行3列。
发表评论