A = NUMPY.ARANGE (0, 16) # Generate 0-16 array b = numpy.arenge (0, 16) .Reshape (4, 4) # Generate a 4x4 array, data is data A data A.Shape = (2, 2, 2, 2) # Modify A's dimension to (2,2,2,2) A.Shape = (2, -1,2,2) # -1 means automatic calculation of this dimension a.Resize = (4, 4) # Modify the dimension of A to (4,4)
2, array and asarray
a = numpy.array([1,2,3,4]) b = numpy.array([1,2,3,4], dtype=numpy.float32) c = numpy.array([[1, 2], [3, 4]]) d = numpy.array(numpy.arange(1,11)) e = numpy.array([numpy.arange(1,5), numpy.arange(11, 15)])
Asarray Usage to Array, but the parameters accepted by Asarray are not copied when the Numpy array is copied, and Array will copy it, so depending on the actual needs.
3, linspace and logspace
1)numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Function: Generate a number of orientation (array) of a number of NUM, the starting value is start, the termination value is STOP
Endpoint: Whether to contain STOP
RETSTEP: The meta group generated (ndarray, spacing) when True, the spacing is also mathematical tolerance
2)numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
Function: Generate a ratio of a number of Num (array). The starting value is base ** start, and the termination value is base ** stop.
4, EMPTY, ONES and Zeros
numpy.empty(shape, dtype = float, order = ‘C’)
Function: Generate an empty array with a dimension to Shape, but because it is written in C language, the variables that are not assigned will be a garbage value.
numpy.zeros(shape, dtype = float, order = ‘C’)
Function: Generate an array with a dimension with shape and 0 value.
numpy.ones(shape, dtype = None, order = ‘C’)
Function: Generate an array with a dimension to Shape and 1
Expand: Advanced index
1) a [one -dimensional index list or array, one -dimensional launch index list or array]
a = np.array ([[1, 2], [3, 4], [5, 6]))) b = a [[0, 1, 2], [0, 1, 0]]] # Compared to B is a new array composed of a [0, 0], a [1, 1], a [2, 0], that is, [1 4 5]
2) a [Multi -dimensional index list or array, multi -dimensional index list or array]
a = np.array ([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11])) b = a [np.array ([0, 0, 3 3]), np.array ([0, 2, 0, 2])]] Will get [0 2 9 11]
will get [[0 2] [9 11]], but the data has not changed, but the dimension of the result changes.
3) Boolean index
b = [False, True, True] a = numpy.array([1, 2, 3]) c = a[b]
You guess what the result is
4) Fancy index
A is a one -dimensional array, and the index is also a one -dimensional array
a = numpy.arange(15)
b = a[range(5)] # [0 1 2 3 4]
c = a[[-1,-2,-3,-4]] # [14 13 12 11]
So when A is a two -dimensional array, and the index is given only one -dimensional array, what will be obtained?
a = numpy.arange(15).reshape(5, 3)
b = a[[-3,-1,-2]]
A little more advanced, please see:
a = numpy.arange(15).reshape(5, 3)
b = a[numpy.ix_([1,3,3],[2,0,1])]
c = a[[1,3,3]][:,[2,0,1]]
This index means to take A [[1,3,3]] to get the array of the first and two third lines of A, and then take the index of [2,0,1] for each line.
numpy.ix_ function to pack [1,3,3], [2,0,1] into one like this ([  ], [2 0 1])) Group. That is, its role is just that the code is easy to understand, otherwise writing a [[  ], [2 0 1]]] Who knows what the result is. Of course you have to write this way, just like writing numpy.ix_. The form of writing C is also easier to understand.
Personal understanding: If you can not use fancy indexes, it is best not to use it. Because fancy indexes are not like slices and indexes, but a new data is copied.
1. Basic operation (the array dimension is exactly the same)
a = numpy.arange(1, 11)
b = numpy.arange(11,21)
c = a + b
d = b – a
e = a * b # and matrix are different, and the multiplication between the array is only the corresponding element multiplication.
f = b / a
g = b // a
h = a > 5
j = a [a> 5] # Burling index application
k = a @ b # matrix multiplication, the same as A.Dot (b)
2, numpy broadcast (different array dimensions, but restrictions)
Professional interpretation: The principle of broadcasting, if the axis length of the two arrays (trailing diarsion, that is, the dimension that starts from the end) is consistent, or the length of one of them is 1, it is considered that they are compatible with broadcasting compatibility of. Broadcasting will be carried out in a dimension with lack and (or) length.
The array with a dimension with (4,3,2) can operate with an array with (3,2) with a dimension. Of course, it is also possible to (3,1) and (1,2).
a = numpy.arange(1, 25).reshape(4,3,2)
b = numpy.arange(1, 7).reshape(3,2)
print(‘a: ‘, a)
print(‘b: ‘, b)
print(a + b)