If you don't know numpy, don't say you're a python programmer

Tianyuan prodigan 2020-11-13 09:06:03
don know numpy don say


List of articles

0. Preface

 Insert picture description here
About seven or eight years ago , I used to use it. pyOpenGL A three-dimensional model of the earth's magnetopause , This code is still running in a scientific research institution . Until then, , I always feel that I am a person (you) grid (xiu) Of python The programmer , Seems omnipotent . But the display of the magnetopause model frustrated me —— Although the model has only a few hundred thousand vertices , Drag and drop 、 Zoom is very stuck . Final , I cut the number of vertices to about 20000 , In order to balance the model quality and response speed , Just barely delivered the task . From then on, I began to doubt python Performance of , Even doubted for a time python Is it still my preferred tool .

Fortunately, , Then I met numpy This artifact .numpy yes python Basic software package for Scientific Computing , Provides more dimensional array objects , Multiple derived objects ( Mask array 、 Matrix, etc ) And the functions for quickly manipulating arrays and API, It includes mathematics 、 Logic 、 Array shape transformation 、 Sort 、 choice 、I/O 、 Discrete Fourier transform 、 Basic linear algebra 、 Basic statistical operations 、 Random simulation and so on .

understand numpy after , I just want to understand why the 3D model of magnetopause was slow , Because of the use of list(python Array ) instead of ndarray(numpy Array ) Store the data . With numpy,python It's possible for a programmer to write something comparable to C Language speed code . be familiar with numpy, To learn to use pyOpenGL / pyOpenCV / pandas / matplotlib Data processing and visualization module .

in fact ,numpy The organization of data , Especially arrays (numpy.ndarray), It has become the standard data structure for almost all data processing and visualization modules ( This point , It's similar to in the field of machine learning python It has almost become the preferred tool language ). More and more based on python The science and mathematics software package uses numpy Array , Although these tools usually support python As parameters , But they will still convert the input array to numpy Array of , And it's usually output as numpy Array . stay python In the circle of ,numpy The importance and universality of the system are increasing . let me put it another way , In order to use today's science efficiently / Mathematics is based on python Tools for ( Most scientific computing tools ), You only know how to use python The native array type of is not enough , You also need to know how to use numpy Array .

summary : In this AI and ML The era of big screen , If you don't understand numpy, Please don't say you are python The programmer .

1. list VS ndarray

numpy The core is ndarray object (numpy Array ), It encapsulates the python Of the same data type n Dimension group (python Array ).numpy Array and python There are several important differences between arrays :

  • numpy Once the array is created , The number of elements can no longer be changed . Add or delete ndarray Operation of element , It means creating a new array and deleting the original array .python The elements of an array can be increased or decreased dynamically .

  • numpy Elements in an array need to have the same data type , So the size in memory is the same . python Arrays have no such requirement .

  • numpy Array method covers a large number of mathematical operations and complex operations , Many methods are in the outermost layer of numpy There are corresponding mapping functions in the namespace . and python Array comparison ,numpy Array method is more powerful , More efficient execution , The code is simpler .

However , The above differences do not really reflect ndarray What's the advantage of ,ndarray The essence of numpy Two characteristics of : Vectorization (vectorization) And broadcast (broadcast). Vectorization can be understood as no explicit loop in the code 、 Index, etc. , Broadcasting can be understood as an implicit operation on each element . Vectorization and broadcasting are a bit abstract to understand , Let's give a chestnut to illustrate .

** Example ** a and b It's an array of two integers of equal length , seek a and b An array of the products of the corresponding elements .

  1. use python Array implementation :
c = list()
for i in range(len(a)):
c.append(a[i]*b[i])
  1. use numpy Array implementation :
c = a*b

Does this chestnut embody the power of vectorization and broadcasting ? Please feel it carefully !

summary :

  • Vectorization code is more concise , Easier to read
  • Fewer lines of code usually means fewer errors
  • The code is closer to the standard mathematical notation
  • Vectorization code is more pythonic

2. dtype AND shape

Confucius said : Find a partner and understand your character first , Learn about attributes first . ndarray Objects have many properties , For details, see table below. .

attribute explain
ndarray.dtype Element type
ndarray.shape The structure of the array
ndarray.ndim Rank , That is, the number of axes or dimensions
ndarray.size Number of array elements
ndarray.itemsize The size of each element , In bytes
ndarray.flags Array memory information
ndarray.real The real part of the element
ndarray.imag The imaginary part of the element
ndarray.data The actual storage area of array elements

For three reasons , In my submission ,dtype and shape yes ndarray The two most important attributes , So important that you can almost ignore other attributes .

  • The pit we went through , Almost all dtype Dug
  • Our confusion , Almost all because of shape It's not what we expected
  • Our work , A lot of it's changing shape

ndarray.astype() You can modify the element type , ndarray.reshape() You can redefine the structure of an array , These two methods are as important as their corresponding properties . Remember these two properties and the corresponding two methods , Even if you're in the house . Want to know numpy Supported element types , Please click on 《 Mathematical modeling three swordsmen MSN》

3. Create array

(1) Create a simple array

numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
numpy.empty(shape, dtype=float, order='C')
numpy.zeros(shape, dtype=float, order='C')
numpy.ones(shape, dtype=float, order='C')
numpy.eye(N, M=None, k=0, dtype=float, order='C')

Application example :

>>> import numpy as np
>>>> np.array([1, 2, 3])
array([1, 2, 3])
>>> np.empty((2, 3))
array([[2.12199579e-314, 6.36598737e-314, 1.06099790e-313],
[1.48539705e-313, 1.90979621e-313, 2.33419537e-313]])
>>> np.zeros(2)
array([0., 0.])
>>> np.ones(2)
array([1., 1.])
>>> np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

(2) Create random arrays

numpy.random.random(size=None)
numpy.random.randint(low, high=None, size=None, dtype='l')

Application example :

>>> np.random.random(3)
array([0.29334156, 0.45858765, 0.99297047])
>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
>>> np.random.randint(3,10,(2,4))
array([[4, 8, 9, 6],
[7, 7, 7, 9]])

(3) Create an array within a numeric range

numpy.arange(start, stop, step, dtype=None)
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

Application example :

>>> np.arange(5)
array([0, 1, 2, 3, 4])
>>> np.arange(0,5,2)
array([0, 2, 4])
>>> np.linspace(0, 5, 5)
array([0. , 1.25, 2.5 , 3.75, 5. ])
>>> np.linspace(0, 5, 5, endpoint=False)
array([0., 1., 2., 3., 4.])
>>> np.logspace(1,3,3)
array([ 10., 100., 1000.])
>>> np.logspace(1, 3, 3, endpoint=False)
array([ 10. , 46.41588834, 215.443469 ])

(4) Create an array from an existing array

numpy.asarray(a, dtype=None, order=None)
numpy.empty_like(a, dtype=None, order='K', subok=True)
numpy.zeros_like(a, dtype=None, order='K', subok=True)
numpy.ones_like(a, dtype=None, order='K', subok=True)[source]

Application example :

>>> np.asarray([1,2,3])
array([1, 2, 3])
>>> np.empty_like(np.asarray([1,2,3]))
array([0, 0, 0])
>>> np.zeros_like(np.asarray([1,2,3]))
array([0, 0, 0])
>>> np.ones_like(np.asarray([1,2,3]))
array([1, 1, 1])

(5) Construct complex array

[1] Repeating array tile

>>> a = np.arange(3)
>>> a
array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])
>>> np.tile(a, (2,3))
array([[0, 1, 2, 0, 1, 2, 0, 1, 2],
[0, 1, 2, 0, 1, 2, 0, 1, 2]])

[2] Repetitive elements repeat

>>> a = np.arange(3)
>>> a
array([0, 1, 2])
>>> a.repeat(2)
array([0, 0, 1, 1, 2, 2])

[3] One dimensional array gridding : meshgrid

>>> lon = np.arange(30, 120, 10)
>>> lon
array([ 30, 40, 50, 60, 70, 80, 90, 100, 110])
>>> lat = np.arange(10, 50, 10)
>>> lat
array([10, 20, 30, 40])
>>> lons, lats = np.meshgrid(lon, lat)
>>> lons
array([[ 30, 40, 50, 60, 70, 80, 90, 100, 110],
[ 30, 40, 50, 60, 70, 80, 90, 100, 110],
[ 30, 40, 50, 60, 70, 80, 90, 100, 110],
[ 30, 40, 50, 60, 70, 80, 90, 100, 110]])
>>> lats
array([[10, 10, 10, 10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40, 40, 40, 40]])

[4] Gridding of specified ranges and divisions : mgrid

>>> lats, lons= np.mgrid[10:50:10, 30:120:10]
>>> lats
array([[10, 10, 10, 10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40, 40, 40, 40]])
>>> lons
array([[ 30, 40, 50, 60, 70, 80, 90, 100, 110],
[ 30, 40, 50, 60, 70, 80, 90, 100, 110],
[ 30, 40, 50, 60, 70, 80, 90, 100, 110],
[ 30, 40, 50, 60, 70, 80, 90, 100, 110]])
>>> lats, lons = np.mgrid[10:50:5j, 30:120:10j]
>>> lats
array([[10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
[20., 20., 20., 20., 20., 20., 20., 20., 20., 20.],
[30., 30., 30., 30., 30., 30., 30., 30., 30., 30.],
[40., 40., 40., 40., 40., 40., 40., 40., 40., 40.],
[50., 50., 50., 50., 50., 50., 50., 50., 50., 50.]])
>>> lons
array([[ 30., 40., 50., 60., 70., 80., 90., 100., 110., 120.],
[ 30., 40., 50., 60., 70., 80., 90., 100., 110., 120.],
[ 30., 40., 50., 60., 70., 80., 90., 100., 110., 120.],
[ 30., 40., 50., 60., 70., 80., 90., 100., 110., 120.],
[ 30., 40., 50., 60., 70., 80., 90., 100., 110., 120.]])

In the above example, we use imaginary numbers . The way to construct a complex number is as follows :

>>> complex(2,5)
(2+5j)

4. Array operation

(1) Slicing and indexing

For indexes and slices of one-dimensional arrays ,numpy and python Of list equally , Even more flexible .

a = np.arange(9)
>>> a[-1] # The last element 
8
>>> a[2:5] # Back to page 2 To the first 5 Elements 
array([2, 3, 4])
>>> a[:7:3] # Back to page 0 To the first 7 Elements , In steps of 3
array([0, 3, 6])
>>> a[::-1] # Returns an array in reverse order 
array([8, 7, 6, 5, 4, 3, 2, 1, 0])

Suppose there is a building 2 floor , The rooms on each floor are 3 That's ok 4 Column , So we can use a three-dimensional array to store the number of people in each room ( Of course , It can also be room area and other numerical information ).

>>> a = np.arange(24).reshape(2,3,4) # 2 layer 3 That's ok 4 Column 
>>> a
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> a[1][2][3] # Although it can be 
23
>>> a[1,2,3] # But that's the standard usage 
23
>>> a[:,0,0] # On all floors 1 Row of the first 1 Column 
array([ 0, 12])
>>> a[0,:,:] # 1 All the rooms in the building , Equivalence and a[0] or a[0,...]
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a[:,:,1:3] # All the floors of the row 2 To 4 Column 
array([[[ 1, 2],
[ 5, 6],
[ 9, 10]],
[[13, 14],
[17, 18],
[21, 22]]])
>>> a[1,:,-1] # 2 The last room in each row on the floor 
array([15, 19, 23])

Tips : The result of slicing or indexing a multidimensional array , Dimensions are not certain .

(2) Change the structure of the array

numpy The storage order of arrays is irrelevant to the dimensions of arrays , Therefore, it is very convenient to change the dimension of the array , except resize() Outside , This kind of operation does not change the storage order of the array itself .

>>> a = np.array([[1,2,3],[4,5,6]])
>>> a.shape # View array dimensions 
(2, 3)
>>> a.reshape(3,2) # return 3 That's ok 2 Array of columns 
array([[1, 2],
[3, 4],
[5, 6]])
>>> a.ravel() # Returns a one-dimensional array 
array([1, 2, 3, 4, 5, 6])
>>> a.transpose() # Row to column ( It's like matrix transposition )
array([[1, 4],
[2, 5],
[3, 6]])
>>> a.resize((3,2)) # Be similar to reshape, But it will change the array being operated on 
>>> a
array([[1, 2],
[3, 4],
[5, 6]])

np.rollaxis() Used to change the order of the axes , Returns a new array . Usage is as follows :

numpy.rollaxis(a, axis, start=0)
  • a: Array
  • axis: The axis to change . The relative order of the other axes remains unchanged
  • start: The axis to be changed rolls before this position . The default value is 0

Application example :

>>> a = np.ones((3,4,5,6))
>>> np.rollaxis(a, 3, 1).shape
(3, 6, 4, 5)
>>> np.rollaxis(a, 2).shape
(5, 3, 4, 6)
>>> np.rollaxis(a, 1, 4).shape
(3, 5, 6, 4)

(3) Array copy

Changing the array structure returns a new view of the original metadata , And a copy of the original metadata . A shallow copy (view) And deep replication (copy) It's creating a copy of the original data , But there are subtle differences between the two : A shallow copy (view) It's shared memory , Deep copy (copy) It's exclusive .

>>> a = np.arange(6).reshape((2,3))
>>> b = a.view()
>>> b is a
False
>>> b.base is a
False
>>> b.flags.owndata
False
>>> c = a.copy()
>>> c is a
False
>>> c.base is a
False
>>> c.flags.owndata
True

(4) Array merge

[1] append

Just started with numpy For programmers , The biggest confusion is that you can't use append() Method to add elements to the array , Even append() I can't find a way . Actually ,numpy It's still there append() Method , It's just that this method is no longer numpy Array method , It's about upgrading to the outermost layer numpy Namespace , And the function of this method is no longer to add elements , Instead of merging arrays .

>>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.append([[1, 2, 3]], [[4, 5, 6]], axis=0)
array([[1, 2, 3],
[4, 5, 6]])
>>> np.append(np.array([[1, 2, 3]]), np.array([[4, 5, 6]]), axis=1)
array([[1, 2, 3, 4, 5, 6]])

[2] concatenate

concatenate() and append() It's very similar to , It's just writing two merged objects into a tuple .

>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])

[3] stack

except append() and concatenate() , There are also more direct horizontal merges (hstack)、 Merge vertically (vstack)、 Deep merge (dstack) Methods such as . If you are lazier than me , Then just use stack Well , enough .

>>> a = np.arange(9).reshape(3,3)
>>> b = np.arange(9,18).reshape(3,3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> b
array([[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]])
>>> np.hstack((a,b)) # Horizontal merger 
array([[ 0, 1, 2, 9, 10, 11],
[ 3, 4, 5, 12, 13, 14],
[ 6, 7, 8, 15, 16, 17]])
>>> np.vstack((a,b)) # Merge vertically 
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]])
>>> np.dstack((a,b)) # Deep merge 
array([[[ 0, 9],
[ 1, 10],
[ 2, 11]],
[[ 3, 12],
[ 4, 13],
[ 5, 14]],
[[ 6, 15],
[ 7, 16],
[ 8, 17]]])

stack The function prototype is stack(arrays, axis=0), Please pay attention to the following example axis Usage of .

>>> a = np.arange(60).reshape(3,4,5)
>>> b = np.arange(60).reshape(3,4,5)
>>> a.shape, b.shape
((3, 4, 5), (3, 4, 5))
>>> np.stack((a,b), axis=0).shape
(2, 3, 4, 5)
>>> np.stack((a,b), axis=1).shape
(3, 2, 4, 5)
>>> np.stack((a,b), axis=2).shape
(3, 4, 2, 5)
>>> np.stack((a,b), axis=3).shape
(3, 4, 5, 2)

(5) Array splitting

Splitting is the inverse process of merging , The concept is the same , But it's a little different :

>>> a = np.arange(4).reshape(2,2)
>>> a
array([[0, 1],
[2, 3]])
>>> x, y = np.hsplit(a, 2) # Horizontal split , return list
>>> x
array([[0],
[2]])
>>> y
array([[1],
[3]])
>>> x, y = np.vsplit(a, 2) # Split Vertically , return list
>>> x
array([[0, 1]])
>>> y
array([[2, 3]])
>>> a = np.arange(8).reshape(2,2,2)
>>> a
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> x,y = np.dsplit(a, 2) # Deep split , return list
>>> x
array([[[0],
[2]],
[[4],
[6]]])
>>> y
array([[[1],
[3]],
[[5],
[7]]])

(6) Array sorting

Sorting is not numpy The strengths of arrays , but python The sorting speed of arrays is still as fast as it can be .

[1] numpy.sort()

numpy.sort() Function returns a sorted copy of the input array .

numpy.sort(a, axis=-1, kind='quicksort', order=None)
  • a: Array to sort
  • axis: Sort the axis of the array along it , without , The array will be expanded , Sort along the last axis
  • kind: Sorting method , The default is ’quicksort’( Quick sort ), Other options are ‘mergesort’( Merge sort ) and ‘heapsort’( Heap sort )
  • order: If the array contains fields , The fields to be sorted

Application example :

>>> a = np.array([3, 1, 2])
>>> np.sort(a)
array([1, 2, 3])
>>> dt = np.dtype([('name', 'S10'),('age', int)])
>>> a = np.array([("raju",21),("anil",25),("ravi", 17), ("amar",27)], dtype = dt)
>>> a
array([(b'raju', 21), (b'anil', 25), (b'ravi', 17), (b'amar', 27)],
dtype=[('name', 'S10'), ('age', '<i4')])
>>> np.sort(a, order='name')
array([(b'amar', 27), (b'anil', 25), (b'raju', 21), (b'ravi', 17)],
dtype=[('name', 'S10'), ('age', '<i4')])

[2] numpy.argsort()

Function returns the index value of the array value from small to large .

numpy.argsort(a, axis=-1, kind='quicksort', order=None)
  • a: Array to sort
  • axis: Sort the axis of the array along it , without , The array will be expanded , Sort along the last axis
  • kind: Sorting method , The default is ’quicksort’( Quick sort ), Other options are ‘mergesort’( Merge sort ) and ‘heapsort’( Heap sort )
  • order: If the array contains fields , The fields to be sorted

Application example :

>>> a = np.array([3, 1, 2])
>>> np.argsort(a)
array([1, 2, 0], dtype=int64)

(7) Search and filter

[1] Returns the index of the maximum and minimum values in an array

numpy.argmax(a, axis=None, out=None)
numpy.argmin(a, axis=None, out=None)

[2] Returns the index of non-zero elements in an array

numpy.nonzero(a)

[3] Returns the index of the elements in an array that satisfy the given condition

numpy.where(condition[, x, y])

Application example :

>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(a < 5)
(array([0, 1, 2, 3, 4], dtype=int64),)
>>> a = a.reshape((2, -1))
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.where(a < 5)
(array([0, 0, 0, 0, 0], dtype=int64), array([0, 1, 2, 3, 4], dtype=int64))
>>> np.where(a < 5, a, 10*a)
array([[ 0, 1, 2, 3, 4],
[50, 60, 70, 80, 90]])

[4] Returns the selected elements of the same structure Boolean array

numpy.extract(condition, arr)

Application example :

>>> a = np.arange(12).reshape((3, 4))
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> condition = np.mod(a, 3)==0
>>> condition
array([[ True, False, False, True],
[False, False, True, False],
[False, True, False, False]])
>>> np.extract(condition, a)
array([0, 3, 6, 9])

(8) Add or subtract elements

[1] Inserts a value into the input array at the leading edge of a given index, a given axis , And return the new array

numpy.insert(arr, obj, values, axis=None)

Application example :

>>> a = np.array([[1, 1], [2, 2], [3, 3]])
>>> a
array([[1, 1],
[2, 2],
[3, 3]])
>>> np.insert(a, 1, 5)
array([1, 5, 1, 2, 2, 3, 3])
>>> np.insert(a, 1, 5, axis=0)
array([[1, 1],
[5, 5],
[2, 2],
[3, 3]])
>>> np.insert(a, 1, [5,7], axis=0)
array([[1, 1],
[5, 7],
[2, 2],
[3, 3]])
>>> np.insert(a, 1, 5, axis=1)
array([[1, 5, 1],
[2, 5, 2],
[3, 5, 3]])

[2] Delete the specified subarray on the given axis at the leading edge of a given index , And return the new array

numpy.delete(arr, obj, axis=None)

Application example :

>>> a = np.array([[1, 2], [3, 4], [5, 6]])
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.delete(a, 1)
array([1, 3, 4, 5, 6])
>>> np.delete(a, 1, axis=0)
array([[1, 2],
[5, 6]])
>>> np.delete(a, 1, axis=1)
array([[1],
[3],
[5]])

[3] Remove duplicate elements

numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
  • arr: Input array , If it is not a one-dimensional array, it will expand
  • return_index: If true, Returns the location of the new list element in the old list ( Subscript ), And in the form of lists
  • return_inverse: If true, Returns the position of the old list element in the new list ( Subscript ), And in the form of lists
  • return_counts: If true, Returns the number of occurrences of elements in the de duplicated array in the original array

Application example :

>>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
>>> np.unique(a)
array([0, 1, 2, 3, 4])
>>> np.unique(a, axis=0)
array([[1, 0, 0],
[2, 3, 4]])
>>> u, indices = np.unique(a, return_index=True)
>>> u
array([0, 1, 2, 3, 4])
>>> indices
array([1, 0, 6, 7, 8], dtype=int64)
>>> u, indices = np.unique(a, return_inverse=True)
>>> u
array([0, 1, 2, 3, 4])
>>> indices
array([1, 0, 0, 1, 0, 0, 2, 3, 4], dtype=int64)
>>> u, num = np.unique(a, return_counts=True)
>>> u
array([0, 1, 2, 3, 4])
>>> num
array([4, 2, 1, 1, 1], dtype=int64)

(9) Array IO

numpy by ndarray Object introduces a new binary file format , For storage reconstruction ndarray Data required 、 graphics 、dtype And other information ..npy File stores a single array ,.npz File access multiple arrays .

[1] Save a single array to a file

numpy.save(file, arr, allow_pickle=True, fix_imports=True)
  • file: File to save , extension .npy, If there is no extension at the end of the file path .npy, The extension will be automatically added with
  • arr: The array to save
  • allow_pickle: Optional , Boolean value , Allow to use python pickles Save an array of objects ,python Medium pickle Used before saving to or reading from a disk file , Serialize and deserialize objects
  • fix_imports: Optional , For convenience pyhton2 Read python3 Saved data

[2] Save multiple arrays to a file

numpy.savez() Function is used to write multiple arrays to a file , By default , The array is stored in an uncompressed raw binary format with an extension name .npz In the file of .

numpy.savez(file, *args, **kwds)
  • file: File to save , extension .npz, If there is no extension at the end of the file path .npz, The extension will be automatically added with
  • args: The array to save , You can use the keyword parameter to give the array a name , The array passed by a non keyword parameter is automatically named arr_0, arr_1, …
  • kwds: The array to be saved uses the keyword name

[3] Load array from file

numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII')
  • file: Class file object ( Support seek() and read() Method ) Or the file path to read
  • arr: Open mode ,None | ‘r+’ | ‘r’ | ‘w+’ | ‘c’
  • allow_pickle: Optional , Boolean value , Allow to use python pickles Save an array of objects ,python Medium pickle Used before saving to or reading from a disk file , Serialize and deserialize objects
  • fix_imports: Optional , For convenience pyhton2 Read python3 Saved data
  • encoding: Coding format ,‘latin1’ | ‘ASCII’ | ‘bytes’

Application example :

a = np.array([[1,2,3],[4,5,6]])
b = np.arange(0, 1.0, 0.1)
c = np.sin(b)
# c Using keyword parameters sin_array
np.savez("runoob.npz", a, b, sin_array = c)
r = np.load("runoob.npz")
print(r.files) # Look at the array names 
print(r["arr_0"]) # Array a
print(r["arr_1"]) # Array b
print(r["sin_array"]) # Array c

[4] Using text files to access arrays

numpy It also supports accessing data as text files .savetxt() The function stores data in a simple text file format , Corresponding use loadtxt() Function to get data .

Application example :

a = np.array([1,2,3,4,5])
np.savetxt('out.txt',a)
b = np.loadtxt('out.txt')
print(b)

5. Common functions

(1) Rounding function

[1] rounding

numpy.around(a, decimals=0, out=None)

Application example :

>>> np.around([-0.42, -1.68, 0.37, 1.64])
array([-0., -2., 0., 2.])
>>> np.around([-0.42, -1.68, 0.37, 1.64], decimals=1)
array([-0.4, -1.7, 0.4, 1.6])
>>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
array([ 0., 2., 2., 4., 4.])

[2] Go to the end and go in

numpy.floor(a)
numpy.ceil(a)

Application example :

>>> np.floor([-0.42, -1.68, 0.37, 1.64])
array([-1., -2., 0., 1.])
>>> np.ceil([-0.42, -1.68, 0.37, 1.64])
array([-0., -1., 1., 2.])

(2) Mathematical functions

function explain
numpy.deg2rad() / numpy.radians() Degrees to radians
numpy.rad2deg() / numpy.degrees() Radians, turns
numpy.sin() Sine function
numpy.arcsin() Antisinusoidal function
numpy.cos() cosine function
numpy.arccos() Arccosine function
numpy.tan() Tangent function
numpy.arctan() Arctangent function
numpy.hypot() Calculate the hypotenuse of a right triangle
numpy.square() square
numpy.sqrt() Square root
numpy.power chengfang
numpy.exp() Index
numpy.log() logarithm
numpy.log2() logarithm
numpy.log10() logarithm

(3) Statistical function

function explain
numpy.sum(a[, axis, dtype, out, keepdims]) Sum the elements according to the specified axis
numpy.nansum(a[, axis, dtype, out, keepdims]) Sum the elements according to the specified axis ,numpy.nan As 0
numpy.cumsum(a[, axis, dtype, out]) Find the progressive sum of elements according to the specified axis
numpy.prod(a[, axis, dtype, out, keepdims]) Find the product of elements according to the specified axis
numpy.diff(a[, n, axis]) Returns the difference between adjacent elements
numpy.ptp() Returns the difference between the maximum and minimum values of elements in an array
numpy.var() Return array variance
numpy.std() Returns the array standard deviation
numpy.median() Returns the median of array elements
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=) Returns the arithmetic average of all elements
numpy.average() According to the weight data , Returns the weighted average of all elements of a data array

6. a master hand 's first small display

** Example ** vertices It is a set of random points in three-dimensional space ,p It's a little bit of three-dimensional space , find vertices middle distance p Point to the nearest point , And calculate their distance .

  1. use python Array implementation :
import math
vertices = [[3,4,5], [7,8,9], [4,9,3]]
p = [2,7,4]
d = list()
for v in vertices:
d.append(math.sqrt(math.pow(v[0]-p[0], 2)+math.pow(v[1]-p[1], 2)+math.pow(v[2]-p[2], 2)))
print(vertices[d.index(min(d))], min(d))
  1. use numpy Array implementation :
import numpy as np
vertices = np.array([[3,4,5], [7,8,9], [4,9,3]])
p = np.array([2,7,4])
d = np.sqrt(np.sum(np.square((vertices-p)), axis=1))
print(vertices[d.argmin()], d.min())

Generate in a random way 1000 A little bit , Compare the efficiency of the two methods .

7. Postscript

Recently, a lot of friends have consulted through private letters about Python Learning problems . To facilitate communication , I am here CSDN Of app Created on “Python Homework guidance ” stronghold , oriented Python beginner , Provide consulting services for you 、 Coach Python Homework . Welcome interested students to use wechat code scanning to join .

 Insert picture description here

From blogs to official account , Every article 、 Every question 、 Every sentence 、 Every line of code , All insist on originality , Never copy , This is my principle . If you like , Please pay attention to my WeChat official account “Python Homework counselor ”.

 Insert picture description here

版权声明
本文为[Tianyuan prodigan]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database