[Depth] learn basic --NumPy

Because the depth of learning will be applied to mathematical knowledge --- linear algebra college learning. (Matrix then was quite interesting to think of it, there are postgraduate experience the feeling)
and how to display the calculation and application of the matrix inside the computer, you need to use NumPy, it is an external library Python.

To start learning about how to apply Numpycalculates arrays and matrices.

1. The one-dimensional array and generate calculated

import numpy as np

x= np.array([1.0,2.0,3.0])
print(x)
y = np.array([3.0, 6.0, 9.0])

print(x+y)
print(x-y)

Demonstration effect is as follows:

(zsdpy1) zsd@zsd-virtual-machine:~/ZAI$ python section01.py 
[1. 2. 3.]
[ 4.  8. 12.]
[-2. -4. -6.]

2. Calculation and generation matrix

Generating a simple 2 * 2 matrix, and two simple matrix calculation

import numpy as np
A= np.array([[1,2],[5,6]])
print(A)
A.shape

B = np.array([[3, 0],[0, 6]])

print(A+B)

Display effects:

(zsdpy1) zsd@zsd-virtual-machine:~/ZAI$ python section02.py 
[[1 2]
 [5 6]]
[[ 4  2]
 [ 5 12]]

Where the multiplication of matrices is calculated as the time of reading, the picture effect is as follows:

code show as below:

import numpy as np
A= np.array([[1,2],[5,6]])

B = np.array([10, 20])
print(A)
print(B)

print(A*B)

Display effects:

(zsdpy1) zsd@zsd-virtual-machine:~/ZAI$ python section03.py 
[[1 2]
 [5 6]]
[10 20]
[[ 10  40]
 [ 50 120]]

Guess you like

Origin www.cnblogs.com/zhangshengdong/p/12613246.html