This is the 12th day of my participation in the First Challenge 2022

The arithmetic operators on arrays are elements, which is the advantage of Numpy. For example, if a and B are both numpy arrays, numpy can add, subtract, multiply, and divide elements without iterating through the two arrays.

a = np.array([10.20.50.60.80])
b = np.arange(0.10.2)

print(a)
print(b)
print(a - b)
Copy the code
[10 20 50 60 80]
[0 2 4 6 8]
[10 18 46 54 72]
Copy the code
print(b**2)
#[ 0  4 16 36 64]
Copy the code

Where b**2 is the square of each element in the array.

10 * np.sin(a)
Copy the code
print(10*np.sin(a))
Copy the code

Unlike other languages used to manipulate matrices, * means that the corresponding elements of two matrices are multiplied, so the two matrices need to have the same shape. Can be

A = np.array([[1.1],
               [0.1]])
>>> B = np.array([[2.0],
               [3.4]])
>>> A * B     # point by point
array([[2.0],
       [0.4]])
Copy the code

Np. Multiply can be used to multiply two matrices point by point, but this does not have A*B

np.multiply(A,B)
Copy the code

We can also multiply parts of the matrix.

A = np.array([[1.2.3.4.5], [6.7.8.9.10]])

B = np.array([[11.12.13.14.15], [16.17.18.19.20]])

print(np.multiply(A[ 0,:], B[ 1, :))Copy the code

After Python 3.5 the dot product can be implemented using the @ operator, which can be called

a = np.array([[1.2], [5.6]]) 
b = np.array([2.3])
print(np.dot(a,b))
Copy the code

> > > A @ # B matrix multiplication array ([[5, 4], [3, 4]]) > > > a. d. ot # (B) matrix dot array ([[5, 4], [3, 4]])Copy the code

Examples such as += and *= are used locally, modifying an existing array rather than creating a new one.

We can think of a 2-dimensional matrix as a table of numbers, or a set of numbers organized in a certain structure. For this set of numbers, we represent these numbers by some quantity. Sometimes we are concerned with the total number of goods sold, sometimes we are concerned with the maximum or minimum value of the number.


a = rg.random((2.3))
>>> a
array([[0.82770259.0.40919914.0.54959369],
       [0.02755911.0.75351311.0.53814331]])
>>> a.sum(a)3.1057109529998157
>>> a.min(a)0.027559113243068367
>>> a.max(a)0.8277025938204418
Copy the code

Summing these arrays, and calculating their maximum and minimum values, is provided by the Ndarray class.

b = np.arange(12).reshape(3.4)
>>> b
array([[ 0.1.2.3],
       [ 4.5.6.7],
       [ 8.9.10.11]]) > > >>>> b.sum(axis=0)     # sum of each column
array([12.15.18.21) > > >>>> b.min(axis=1)     # min of each row
array([0.4.8) > > >>>> b.cumsum(axis=1)  # cumulative sum along each row
array([[ 0.1.3.6],
       [ 4.9.15.22],
       [ 8.17.27.38]])
Copy the code

By default, these summation, Max, and min operations apply to arrays, but you can also sum, Max, and min values along the specified Axis pair by specifying axis.