Python Study Notes (2) -- NumPy
Python can use List as an array, but since the List element can be any object, saving a List requires saving all pointers and elements. Memory consumption.
This article is a blog Study: Using Python for scientific computing to sort out notes for future use.
First, import the NumPy function library
importnumpy as np
Create an array
Array
Use array to create multi-dimensional arrays
a = np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9,10]])
Shape
Use shape to obtain the array dimension.
a.shape(3,4)
Reshape
Use reshape to change the array size
b=a.reshape((2,6))
However, note that the two arrays share the memory. If you modify one of the two arrays, the other will also change.
Create special functions for Arrays
Arange: Specifies the start value, end value (not included), and step size.
np.arange(0,10,1.5)array([ 0. , 1.5, 3. , 4.5, 6. , 7.5, 9. ])
Linspace: an equal-difference sequence that specifies the start value, end value (including), and number of elements
np.linspace(0,10,7)array([0., 1.66666667, 3.33333333, 5., 6.66666667, 8.33333333,10.)
Logspace: indicates the proportional sequence of the start value, end value (including), and number of elements.
np.logspace(0,1,7)array([1., 1.46779927, 2.15443469, 3.16227766,4.64158883, 6.81292069, 10.])
Access Element
-Integer Sequence
a=np.arange(10,1,-1)b=a[4,4,4,-2]barray([6,6,6,3])
Note: B and a do not share the memory, so changing either of them will not change.
-Boolean Array
Collect the elements whose subscript is True in the array.
a=np.arange(5,0,-1)a[np.array([True, True, True,False, False])]array([5,4,3])
Note:
1. The two arrays do not share memory.
2. It can only correspond to a Boolean array, but not a Boolean List a [[True, False, False]
Otherwise, the output should be
array([4,4,4,5,5])
Structure Array
Create a dtype object. The dictionary has two keywords: names and formats.
import numpy as nppeople=np.dtype({ ‘names’:[‘name’,’ gender’] ‘formats’:[‘S32’,’S32’]})a=np.array([(‘LiuJingjing’,’Female’),(‘ChengYin’,’Male’)],dtype=people)
Ufunc operations
x=np.linspace(0,np.pi,10)t=np.sin(x,x)
Here, the second parameter of sin is used to save the result. The above formula indicates that x has been overwritten, and the effect is the same as that of t.
Broadcast
Broadcast means that the array sizes are different, so that they can perform operations.
a=np.arange(10,70,10).reshape(-1,1)b=np.arange(1,6)c=a+bcarray([[11, 12, 13, 14, 15], [21,22, 23, 24, 25], [31,32, 33, 34, 35], [41,42, 43, 44, 45], [51,52, 53, 54, 55], [61,62, 63, 64, 65]])
Matrix Product
Dot, inner, outer
Dot performs the inner product, inner performs the inner Product in the last dimension, outer generates the column vector, and row vector performs the matrix product.
Welcome to the discussion and follow up on this blog, Weibo, and zhihu personal homepage for further updates ~
Reprinted, please respect the work of the author and keep the above text and link of the article completely. Thank you for your support!