NumPy Array (2, operation of Array)
Basic operations
An array is an arithmetic operation that is based on an element. An array operation creates a new array that contains the results of the operation.
>>> a= Np.array ([20,30,40,50])
>>> b= np.arange (4)
>>> b
Array ([0, 1, 2, 3])
>>> c= a-b
>>> C
Array ([A, p, p])
>>> b**2
Array ([0, 1, 4, 9]) C10/>>>> 10*np.sin (a)
array ([9.12945251,-9.88031624, 7.4511316, -2.62374854])
>>> A <35
Array ([True, True, False, false], Dtype=bool)
Unlike other matrix languages, the multiplication operator in NumPy * is calculated by element, and matrix multiplication can be implemented using the DOT function or creating a matrix object (described in a later section)
>>> a= Np.array ([[1,1],
... [0,1]])
>>> b= Np.array ([[2,0],
... [3,4]])
>>> A*b # Element multiplication Array ([[
2, 0],
[0, 4]])
>>> Np.dot (a,b) # matrix multiplication
Array ([[5, 4],
Some operators, such as + + and *=, are used to change an existing array without creating a new array.
>>> a= np.ones (2,3), dtype=int)
>>> b= np.random.random ((2,3))
>>> a*= 3
> >> a
array ([[3, 3, 3],
[3, 3, 3]])
>>> b+= a
>>> b
Array ([[3.69092703, 3.8 324276, 3.0114541],
[3.18679111, 3.3039349, 3.37600289]]
>>> a+= B # b converted to integer type
>>> a< C12/>array ([[6, 6, 6],
When a different type of element is stored in an array, the array uses the data type that occupies more bits (bit) as its own data type, which is biased toward a more precise data type, which is called upcast.
>>> a= np.ones (3, Dtype=np.int32)
>>> b= np.linspace (0,np.pi,3)
>>> B.dtype.name
' float64 '
>>> c= a+b
>>> C
Array ([1, 2.57079633, 4.14159265])
>>> c.dtype.name
' float64 '
>>> d= exp (c*1j)
>>> D
Array ([ 0.54030231+0.84147098J,-0.84147098+0.54030231J,
-0.54030231-0.84147098j])
>>> d.dtype.name
Many non-array operations, such as calculating the sum of all elements of an array, are implemented as methods of the Ndarray class, which need to be invoked using an instance of the Ndarray class.
>>> a= Np.random.random ((2,3))
>>> a
array ([[0.65806048, 0.58216761, 0.59986935],
[ 0.6004008, 0.41965453, 0.71487337]]
>>> a.sum ()
3.5750261436902333
>>> a.min ()
0.41965453489104032
>>> A.max ()
These operations treat an array as a one-dimensional linear list. However, you can perform the appropriate operation on the specified axis by specifying the axis parameter (that is, the row of the array):
>>> b= Np.arange () reshape (3,4)
>>> b
Array ([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, )
>>> b.sum (axis=0) # calculates the and of each column, paying attention to understanding the meaning of the axis, the first article of the reference array ([A, a
,])
>> > B.min (Axis=1) # Gets the minimum array of each row (
[0, 4, 8])
>>> b.cumsum (Axis=1) # Calculates the cumulative and array for each row
([[0, 1, 3 , 6],
[4, 9, 15, 22],
indexes, slices, and iterations
As with lists and other python sequences, one-dimensional arrays can be indexed, sliced, and iterated.
>>> a= Np.arange (a) **3 #记住, operators are in the array of elements to deal with!
>>> a
array ([0, 1, 8, 216, 343, 729])
>>> a[2]
8
>>> a[2 : 5]
Array ([8, a[:6:2]=]) >>>-
1000 # equals a[0:6:2]=-1000, from start to 6th position, assign it to -1000
> >> a
array ([ -1000, 1,-1000, 27,-1000, 216, 343, 72, 729])
>>> a[::-1] # Reverse a
array ([ 9, 343, 216, 125,-1000, 27,-1000, 1,-1000])
>>>for i in a:
... Print i** (1/3 ...),
...
Nan 1.0 Nan 3.0 Nan 5.0 6.0 7.0 8.0 9.0
Multidimensional arrays can have one index per axis. These indexes are given by a comma-separated tuple.
>>>def f (x,y): .. Return 10*x+y ...
>>> b= np.fromfunction (F, (5,4), Dtype=int) #fromfunction是一个函数, next article introduces.
>>> b
Array ([[0, 1, 2, 3], [All, one,], [[A], [a], [a], [M, M], [
40, 1, 1]]
>>> b[2,3] [b[0:5]
(#] # (a) The second element of each row, array ([each,,,
,])
>>> b[:, 1] # with the preceding effect the same
array ([1, one,,])
>>> b[1:3,:] # The second and third elements of each column
array ([ One, one,
[20, 21, 22, 23]]
When less than the number of indexes provided, the values given are copied in order of rank, and the missing indexes are implicitly considered to be the entire slice:
>>> B[-1] # The last line, equivalent to B[-1,:],-1 is the first axis, and the missing is:, the equivalent of the entire slice.
The expressions in parentheses in b[i] are treated as I and a series: to represent the remaining axes. NumPy also allows you to use "dots" like b[i,...].
Point (...) Represents many of the necessary semicolons to produce a complete set of indexed tuples. If x is an array with a rank of 5 (that is, it has 5 axes), then:
- x[1,2,...] equivalent to x[1,2,:,:,:],
- x[...,3] equal to x[:,:,:,:,3]
- X[4,..., 5,:] Equal to x[4,:,:,5,:]
>>> c= Array ([[[0, 1, 2], #三维数组 (with two 2-dimensional arrays stacked)
... [Ten, A]],
...
... [[100,101,102],
... [110,112,113]]]
>>> c.shape
(2, 2, 3)
>>> c[1,...] #等同于c [1,:,:] or c[1]
array ([[100 , 102],
[112, 113]])
>>> c[...,2] #等同于c [:,:, 2]
Array ([[2],
[102, 113]])
The traversal of multidimensional arrays is based on the first axis:
>>>for row in B: ... Print row
...
[0 1 2 3]
[A]
[O]
[30 31 32 33]
If you want to process each element in an array, you can use the Flat property, which is an array element iterator:
>>>for element in B.flat:
... Print element,
...
For more information about [] 、...、 Newaxis, ndenumerate, indices, index exp, please refer to the NumPy example
Shape action
Change the shape of an array
The shape of an array depends on the number of elements on each axis:
>>> a= Np.floor (10*np.random.random (3,4))
>>> a
array ([[7., 5., 9., 3.], [7., 2
., 7., 8 .],
[6., 8., 3., 2.]]
>>> A.shape
(3, 4)
You can modify the shape of an array in several ways:
>>> A.ravel () # Flatten array Arrays
([7., 5., 9., 3., 7., 2., 7., 8., 6., 8., 3, 2.])
>>> a.shape= (6, 2)
>>> a.transpose ()
Array ([7., 9., 7., 7., 6., 3.], [5., 3.,
2., 8. 8., 2.]]
The order of the array elements that are flattened by Ravel () is usually "C-style", which is the fastest change in the index to the right, so the element a[0,0] is a[0,1]. If the array changes to another shape (reshape), the array is still "C-style." NumPy typically creates an array that holds the data in this order, so Ravel () usually does not need to create a copy of the call array. However, you might want to create a copy of an array if you slice other arrays or have unusual options. You can also have some optional parameter functions that let reshape () and Ravel () build a Fortran-style array, where the leftmost index changes the fastest.
The reshape function changes the shape of the calling array and returns the array, while the resize function changes the calling array itself.
>>> a
array ([[7., 5.],
[9., 3.],
[7., 2.]
,
[7
., 8.], [6., 8.], [3., 2.]] >>> a.resize ((2,6))
>>> a
array ([[7., 5., 9., 3., 7., 2.],
[7., 8., 6., 8., 3., 2.]]
If a dimension of 1 is specified in the reshape operation, the exact dimensions are calculated according to the actual situation
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.