Starfruit Python Advanced Lecture 9-Array array (2) Elementary operation of array array

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

Reference tutorial: https://www.runoob.com/numpy/numpy-tutorial.html

 

Array array has various elementary operations similar to numbers, which need to be implemented by NumPy arithmetic functions.

Four operations on arrays

NumPy arithmetic functions include simple addition, subtraction, multiplication and division:  add () , subtract () , multiply ()  and  divide ()

It should be noted that the array must have the same shape, or comply with the array broadcasting rules.

 

Examples of four operations on arrays:

import numpy as np 
 
a = np.arange(9, dtype = np.float_).reshape(3,3)  
print ('第一个数组:')
print (a)
print ('\n')
print ('第二个数组:')
b = np.array([20,20,20])
print (b)
print ('\n')
print ('两个数组相加:')
print (np.add(a,b))
print ('\n')
print ('两个数组相减:')
print (np.subtract(a,b))
print ('\n')
print ('两个数组相乘:')
print (np.multiply(a,b))
print ('\n')
print ('两个数组相除:')
print (np.divide(a,b))

The original array of b is [20 20 20]

It should be noted that the b array is automatically broadcasted by Python , and the actual calculation is
[[20. 20. 20.]
 [20. 20. 20.]
 [20. 20. 20.]]

operation result:

第一个数组:
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]


第二个数组:
[20 20 20]


两个数组相加:
[[20. 21. 22.]
 [23. 24. 25.]
 [26. 27. 28.]]


两个数组相减:
[[-20. -19. -18.]
 [-17. -16. -15.]
 [-14. -13. -12.]]


两个数组相乘:
[[  0.  20.  40.]
 [ 60.  80. 100.]
 [120. 140. 160.]]


两个数组相除:
[[0.   0.05 0.1 ]
 [0.15 0.2  0.25]
 [0.3  0.35 0.4 ]]

 

Find the reciprocal of an array

numpy.reciprocal () The
numpy.reciprocal () function returns the reciprocal of the argument element by element. For example, the reciprocal of 5 is 0.2.

Examples:

import numpy as np 
 
a = np.array([0.25,  1,  2,  0.5])  
print ('原始数组是:')
print (a)
print ('\n')
print ('调用reciprocal函数之后的数组是:')
print (np.reciprocal(a))

operation result:

原始数组是:
[0.25 1.   2.   0.5 ]


调用reciprocal函数之后的数组是:
[4.  1.  0.5 2. ]

Array exponentiation

numpy.power () The
numpy.power () function takes the element in the first input array as the base and calculates the power of it with the corresponding element in the second input array.

Examples:

import numpy as np 
 
a = np.array([2,4,8])  
print ('原始数组a是:')
print (a)
print ('调用power(a,2)函数的结果是:')
print (np.power(a,2))
print ('原始数组b是:')
b = np.array([1,2,3])  
print (b)
print ('调用power(a,b)函数的结果是:')
print (np.power(a,b))

operation result:

原始数组a是:
[2 4 8]
调用power(a,2)函数的结果是:
[ 4 16 64]
原始数组b是:
[1 2 3]
调用power(a,b)函数的结果是:
[  2  16 512]

Array remainder

numpy.mod ()
numpy.mod () calculates the remainder after dividing the corresponding element in the input array. The function numpy.remainder () also produces the same result.

Examples:

import numpy as np
 
a = np.array([10,20,30]) 
b = np.array([5,7,9])  
print ('第一个数组a是:')
print (a)
print ('第二个数组b是:')
print (b)
print ('调用mod(a,b)函数的结果是:')
print (np.mod(a,b))
print ('调用remainder(a,b)函数的结果是:')
print (np.remainder(a,b))

operation result:

第一个数组a是:
[10 20 30]
第二个数组b是:
[5 7 9]
调用mod(a,b)函数的结果是:
[0 6 3]
调用remainder(a,b)函数的结果是:
[0 6 3]

 

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

If you think this chapter is helpful to you, welcome to follow, comment and like! Github welcomes your Follow and Star!

Published 55 original articles · won praise 16 · views 6111

Guess you like

Origin blog.csdn.net/yty_7/article/details/104552585