1 import numpy as np 2 3 # 数组的全通用函数,要求数组的形状必须相同——同型数组 4 # 创建数组 5 arr1 = np.array([[0, 1], [2, 3]]) 6 arr2 = np.array([[1, 2], [3, 4]]) 7 print("arr1: \n", arr1) 8 print("arr2: \n", arr2) 9 10 # # 四则运算——对应位置进行四则运算 11 print("数组相加: \n", arr1+arr2) 12 print("数组相减: \n", arr1-arr2) 13 print("数组相乘: \n", arr1*arr2) 14 print("数组相乘: \n", np.multiply(arr1, arr2)) 15 print("数组相乘: \n", type(np.multiply(np.mat(arr1), np.mat(arr2)))) 16 print("数组相除: \n", arr1/arr2) # 0不能做除数 17 print("数组相幂: \n", arr1**arr2) 18 # 19 # # 比较运算——返回bool类型的数组 20 print("arr1 > arr2: \n", arr1 > arr2) 21 print("arr1 < arr2: \n", arr1 < arr2) 22 print("arr1 == arr2: \n", arr1 == arr2) 23 print("arr1 != arr2: \n", arr1 != arr2) 24 # 25 # # 逻辑运算符——or and——返回值是bool值 26 # # or——np.any 只要有一个满足,就返回True 27 # # and——np.all 只有全部为True, 才能返回True 28 print("any: \n", np.any(arr1 == arr2)) 29 print("any: \n", np.any(arr1 > arr2)) 30 # 31 print("all: \n", np.all(arr1 == arr2)) 32 print("all: \n", np.all(arr1 > arr2))
原文地址:https://www.cnblogs.com/Tree0108/p/12115518.html
时间: 2024-10-11 15:43:15