Basic operators of python arrays

Arithmetic
math = np.array([98,83,86,92,67,82])
english = np.array([68,74,66,82,75,89])
chinese = np.array([92,83,76,85,87,77])
tot_symbol = math+english+chinese
tot_fun = np.add(np.add(math,english),chinese)
print('符号加法:\n',tot_symbol)
print('函数加法:\n',tot_fun)

# 除法运算
height = np.array([165,177,158,169,173])
weight = np.array([62,73,59,72,80])
BMI_symbol = weight/(height/100)**2
BMI_fun = np.divide(weight,np.divide(height,100)**2)
print('符号除法:\n',BMI_symbol)
print('函数除法:\n',BMI_fun)

The symbols in the four arithmetic operations are "+, -, *, /", and the corresponding numpy module functions are np.add, np.subtract, np.multiply, and np.divide. It should be noted that the function can only accept the operation of two objects. If you need the operation of multiple objects, you need to use the nested method, as shown above. All must ensure that the operated arrays have the same shape, except for operations between arrays and scalars.

arr7 = np.array([[1,2,10],[10,8,3],[7,6,5]])
arr8 = np.array([[2,2,2],[3,3,3],[4,4,4]])
print('数组arr7:\n',arr7)
print('数组arr8:\n',arr8)
# 求余数
print('计算余数:\n',arr7 % arr8)
# 求整除
print('计算整除:\n',arr7 // arr8)
# 求指数
print('计算指数:\n',arr7 ** arr8)

The above operations are remainder, divisor, and integer. The functions np.fmod, np.modf, and np.power can be used respectively. However, the application of integer functions will be a little more complicated and needs to be written as np.modf(arr7/arr8)[1], Among them, modf can return the decimal part and the integer part of the value, and the integer part is the integer value to be taken.

Comparison operation
np.greater(arr1,arr2) #>
np.greater_equal(arr1,arr2) #>=
np.less(arr1,arr2) #<
np.less_equal(arr1,arr2) #<=
np.equal(arr1,arr2) #== 等于
np.not_equal(arr1,arr2) #!= 不等于

Examples are as follows:

# 取子集
# 从arr7中取出arr7大于arr8的所有元素
print(arr7)
print('满足条件的二维数组元素获取:\n',arr7[arr7>arr8])
# 从arr9中取出大于10的元素
arr9 = np.array([3,10,23,7,16,9,17,22,4,8,15])
print('满足条件的一维数组元素获取:\n',arr9[arr9>10])

# 判断操作
# 将arr7中大于7的元素改成5,其余的不变
print('二维数组的条件操作:\n',np.where(arr7>7,5,arr7))
# 将arr9中大于10 的元素改为1,否则改为0
print('一维数组的条件操作:\n',np.where(arr9>10,1,0))

Comparison operators can return values ​​of type bool, namely true and false. Use bool index to select elements that meet the conditions from the array, but whether it is a one-dimensional array or a multi-dimensional array, all returned by bool index is a one-dimensional array; the np.where function is the same as the if function in Excel, according to the judgment Conditions make different branches.

Broadcast computing

It is suitable for use when the array shapes are different, but the broadcast function of the array is based on rules, and an error will be reported if it is not satisfied. The rules are as follows: 1. The dimensions of each input array can be unequal, but the corresponding dimension values ​​from right to left must be equal.
2. If the corresponding dimension values ​​are not equal, you must ensure that one of them is 1.
3. Each input array is aligned with the array with the longest shape, and the insufficient part of the shape is filled by adding 1 to the front.

# 各输入数组维度一致,对应维度值相等
arr10 = np.arange(12).reshape(3,4)
arr11 = np.arange(101,113).reshape(3,4)
print('3×4的二维矩阵运算:\n',arr10 + arr11)
# 各输入数组维度不一致,对应维度值相等
arr12 = np.arange(60).reshape(5,4,3)
arr10 = np.arange(12).reshape(4,3)
print('维数不一致,但末尾的维度值一致:\n',arr12 + arr10)
# 各输入数组维度不一致,对应维度值不相等,但其中有一个为1
arr12 = np.arange(60).reshape(5,4,3)
arr13 = np.arange(4).reshape(4,1)
print('维数不一致,维度值也不一致,但维度值至少一个为1:\n',arr12 + arr13)
# 加1补齐
arr14 = np.array([5,15,25])
print('arr14的维度自动补齐为(1,3):\n',arr10 + arr14)

Guess you like

Origin blog.csdn.net/m0_46445293/article/details/115029165