Numpy basic understanding
NumPy (Numerical Python) is an extension library Python language, supports a number of dimensions of the array and matrix operations, in addition, it provides a lot of math library for array operations.
NumPy is running very fast math library, mainly for computing array, comprising:
- A powerful N-dimensional array object ndarray
- Broadcast performance function
- Integration of C / C ++ / Fortran code tool
- Linear algebra, Fourier transform, random number generation functions
Numpy underlying data structures
NumPy dimension of the array are called rank (Rank), the rank of a one-dimensional array, a two-dimensional array of rank 2, and so on.
In NumPy, each linear array is called a shaft (Axis), that is, the dimension (dimensions). For example, the equivalent of a two-dimensional array is a two-dimensional array, where the first one-dimensional array, each element is a one-dimensional array. So is the one-dimensional array in NumPy axis (Axis), a first shaft corresponds to the underlying array, a second axis is the underlying array to array. The number of axes - rank is the dimension of the array.
Many times it can declare axis. axis = 0, represents the operation performed along the first axis 0, i.e., each column is operated; axis = 1, represents the operation along the first axis, i.e., the operation of each line.
NumPy array more important ndarray object properties are:
Attributes | Explanation |
---|---|
ndarray.ndim | Rank number, i.e. the number or dimensions of shaft |
ndarray.shape | Dimension of an array, for the matrix, n-rows and m columns |
ndarray.size | The total number of array elements, corresponding to the n * m values of .shape |
ndarray.dtype | Object element type ndarray |
ndarray.itemsize | Ndarray size of each element in the object, in bytes |
ndarray.flags | ndarray object memory information |
ndarray.real | The real portion of the element ndarray |
ndarray.imag | The imaginary part of the element ndarray |
ndarray.data | Buffer contains the actual array elements, since the element is generally provided by the array index is generally not required to use this property. |
import numpy ar = numpy.array([[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]]) print(ar) print(ar.ndim) print(ar.shape) print(ar.size) print(ar.itemsize) print(ar.data) print(ar.dtype) print(ar.flags) print(ar.real) print(ar.imag) 结果: [[1 2 3 4 5] [2 3 4 5 6] [3 4 5 6 7]] 2 (3, 5) 15 4 <memory at 0x00000000056EF748> int32 C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False [[1 2 3 4 5] [2 3 4 5 6] [3 4 5 6 7]] [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]
Creating an array
array () function within the parentheses may be List, tuples, arrays, and other generators
ar = np.array([[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]]) ar1 = np.array(range(10)) ar2 = np.array(10) ar3 = np.array([[1,2,3,4,5],[2,3,4,5,6],['a','b','c','d']]) print(ar) print(ar1) print(ar2) print(ar3) 输入结果: [[1 2 3 4 5] [2 3 4 5 6] [3 4 5 6 7]] [0 1 2 3 4 5 6 7 8 9] 10 [list([1, 2, 3, 4, 5]) list([2, 3, 4, 5, 6]) list(['a', 'b', 'c', 'd'])]
aRange () function is similar range (), return values evenly spaced intervals within a given
Print (np.arange (10)) Print (np.arange (10.0)) Print (np.arange (5,12)) Print (np.arange (5.0,12.3)) input Results: [012345 6789] [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] [567,891,011] [5. 6. 7. 8. 9. 10.11 12.]
print (numpy.linspace (5,14, num = 10)) # Returns the interval [start, stop num] is calculated from the samples evenly spaced Print (numpy.linspace (5,14, num =. 11)) Print ( numpy.zeros ((4,4), dtype = numpy.int)) # create an array, the array elements specified size 0 filled ar = numpy.array ([range (10 ), range (10,20)]) Print (numpy.zeros_like (Ar)) Print (Ar) Print (numpy.ones ((2,3), DTYPE = numpy.int)) # create an array, the array elements to a specified size to fill #ones_like () and zeros_like () is similar to print (numpy.eye (3, dtype = numpy.int)) # Create a square of the N + N matrix, the diagonal bit 1, the remaining value of 0 input results: [5.6 7. 8. 9. 10. 11. 12. 13. 14.] [5. 5.9 6.8 7.7 8.6 9.5 10.4 11.3 12.2 13.1 14.] [[0000] [0000] [000 0] [0000]] [[0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0]] [[0123456789] [10 11 12 13 14 15 16 17 18 19]] [[1 1 1] [1 1 1]] [[1 0 0] [0 1 0] [0 0 1]]
Numpy generic function
Flip array numpy.T
Modify the array shape numpy.reshape
Returns form a new array numpy.resize
import numpy as np ar = np.arange(10) ar1 = np.zeros((2,4)) print(ar) print(ar1) print(ar.T) print(ar1.T) ar2 = np.arange(12) ar3 = np.arange(12).reshape(3,4) print(ar2) print(ar3) print(np.resize(ar3,(4,4))) 输出结果: [0 1 2 3 4 5 6 7 8 9] [[0. 0. 0. 0.] [0. 0. 0. 0.]] [0 1 2 3 4 5 6 7 8 9] [[0. 0.] [0. 0.] [0. 0.] [0. 0.]] [ 0 1 2 3 4 5 6 7 8 9 10 11] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [ 0 1 2 3]]
Array copy copy ()
np.arange = Ar (10) Print (Ar) AR1 = Ar AR1 [2] = 100 Print (Ar, AR1) Ar3 = ar.copy () Ar3 [. 3] = 998 Print (Ar, Ar3) output: [ 0123456789] [011003456789] [011003456789] [011003456789] [01 100998456789]
Conversion of the array of characters
= numpy.array AR2 of (Range (16)) the RESHAPE (4,4 &). Ar3 = ar2.astype (numpy.str) Print (Ar3) output: [[ '0' '1' '2' '3'] [ '4' '5' '6' 7 '] [' 8 '9' 10 '11'] [ '12' '13' '14' '15']]
Connection array
np.array = Ar ([[l, 2,3], [4,5,6]]) AR1 = np.array ([[7,8,9], [10,11, 12]]) AR2 of = np.stack ((ar, ar1), 0) # array along a connection axis new sequence ar3 = np.hstack ((ar, ar1 )) # transverse connecting ar4 = np.vstack ((ar, ar1 )) # longitudinally connected print (ar2, ar3, ar4) output: [[[123] [456]] [[789] [101112]]] [[123789] [45610 11 12]] [[123] [456] [789] [101112]]
Striped array
np.arange = Ar (16) .reshape (4,4 &) AR1 = np.vsplit (Ar, 2) split longitudinally # ar2 = np.hsplit (ar, 2) # lateral resolution Print (Ar) Print (AR1 ) Print (AR2 of) output: [[0. 1 2. 3] [. 4. 5. 6. 7] [. 8. 9 10. 11] [12 is 13 is 14 15]] [Array ([[0,. 1, 2,. 3], [. 4 ,. 5,. 6,. 7]]), Array ([[. 8,. 9, 10,. 11], [12 is, 13 is, 14, 15]])] [Array ([[0,. 1], [. 4,. 5] , [. 8,. 9], [12 is, 13 is]]), Array ([[2,. 3], [. 6,. 7], [10,. 11], [14, 15]])]
An array of computing
= numpy.array AR2 of (Range (16)). the RESHAPE (4,4 &) Ar3 = ar2.astype (numpy.str) Print (Ar3) AR4 = AR2 of 100 + 10 * Print (AR4) Print (ar4.mean () ) # averaging print (ar4.sum ()) # find gross output: [[ '0' '1' '2' '3'] [ '4' '5' '6' 7 '] [ '8' 9 '10' 11 '] [' 12 '' 13 '' 14 '' 15 ']] [[100110120130] [140150160170] [180190200210] [220230 240,250]] 175.0 2800
Numpy indexing and slicing
The basic index and sliced
AS NP numpy Import # is similar to a one-dimensional array index List Ar = np.arange (20 is) # Print (Ar [2]) # Print (Ar [. 3:. 5]) # Print (Ar [:: 2]) # two logical dimensional arrays and multidimensional array as one-dimensional and AR1 ar.reshape = (4,5) # Print (AR1) Print (AR1 [2] [2]) Print (AR1 [2:. 4] [0] [2 ]) Print (AR1 [: 2]) output: 12 is 12 is [[01,234] [56,789]]
Boolean index and sliced
np.arange = Ar (12 is) .reshape (3,4-) Print (Ar) I = np.array ([True, False, True]) J = np.array ([True, False, True, False]) Print (I) print (J) print (Ar [I ,:]) # line is determined, only the value True to retain print (ar [:, j] ) # is determined in a second dimension, retaining only True value print (ar [ar> 5]) # generate a new array of output: [[0. 1 2. 3] [. 4. 5. 6. 7] [. 8. 9 10. 11]] [True False True] [True False True False] [[ 0123] [891011]] [[02] [46] [810]] [67891011]
Numpy random number
print (np.random.normal (size = (4,4 ))) # random number generating print (np.random.rand (4)) # [0-1] to generate a random or a floating point number between N-dimensional --- uniformly distributed array Print (np.random.rand (2,4)) Print (np.random.randn (. 4)) [0-1] # generate a random or a floating point number between N-dimensional array - - normal Print (np.random.randn (2,4)) Print (np.random.randint (. 5)) to generate an integer or # N-dimensional integer array print (np.random.randint (10,50, size = (4,4)))
Data input and output Nmupy
ar = numpy.random.randint (0,100, size = (10,10)) output numpy.savetxt ( "savetxt.txt", ar, delimiter = ",", fmt = "% i") # data txt_load = numpy .loadtxt ( "savetxt.txt", delimiter = ',') # data input Print (txt_load) # Print (Ar)
Related Links:
- NumPy official website http://www.numpy.org/
- NumPy source code https://github.com/numpy/numpy
- Numpy tutorial https://www.runoob.com/numpy/numpy-tutorial.html