For users who are accustomed to using Matlab, the cost of learning numpy as a python Toolkit should be small.
The main object of numpy is a multi-dimensional array. It is a table consisting of the same types of numbers. It can be indexed using tuples. This article lists the most common numpy operations.
1. Some attributes of the ndarray type
>>> from numpy import *>>> a=array([[1,2,3],[4,5,6]])>>> aarray([[1, 2, 3], [4, 5, 6]])>>>
The preceding figure shows how to initialize a numpy array. The type, dimension, number of elements, element type, and data of this array can be obtained through corresponding attributes.
# Element type ndarray. dtype >>> A. dtypedtype ('int64') >>>
# Dimension ndarray. Shape >>> A. shape (2, 3)
# Ndarray. size> A. size6
2. Create an array of custom sizes and change the shape of the array.
Default system type
>>> a=zeros((3,4))>>> aarray([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
Initialize the Data Type
>>> a=ones((5,4),dtype=int64)>>> aarray([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]])>>> a.dtypedtype('int64')
Change the shape reshape function of the array.
>>> a=arange(15)>>> aarray([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])>>> a.reshape((5,3))array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14]])
Tip: For the reshape function, you can specify only the number of rows or only the number of columns. The rest of the work is done by this function, which improves the operation flexibility.
>>> a.reshape((5,:-1))array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14]])>>> a.reshape((-1,5))array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
3. Array Index
1) index by specifying tuples
>>> a=floor(10*random.random((5,4)))>>> aarray([[ 4., 6., 4., 9.], [ 4., 7., 2., 1.], [ 4., 9., 7., 3.], [ 5., 4., 6., 0.], [ 4., 3., 2., 9.]])>>> a[(1,1)]7.0>>> a[(3,2)]6.0
2) index multiple elements at a time
Multiple rows are indexed. You can store the subscript in an array.
>>> Index = array ([1, 3, 4]) # Index 1, 3, and 4 >>> A [Index] array ([[4 ., 7 ., 2 ., 1.], [5 ., 4 ., 6 ., 0.], [4 ., 3 ., 2 ., 9.])
>>> index=array([[1,2],[1,3]])>>> b=a[index]>>> barray([[[ 4., 7., 2., 1.], [ 4., 9., 7., 3.]], [[ 4., 7., 2., 1.], [ 5., 4., 6., 0.]]])>>> b.shape(2, 2, 4)
All of the above operations are indexed on a row. How can I index the row and column?
>>> i=array([0,1,2,3])>>> j=array([3,2,1,0])>>> a[i,j]array([ 9., 2., 9., 5.])
A one-dimensional matrix is returned. rows are stored in I, and columns are stored in J.
Assume that the returned elements are stored in the 2*2 matrix.
>>> i=array([[0,1],[2,3]])>>> j=array([[3,2],[2,1]])>>> a[i,j]array([[ 9., 2.], [ 7., 4.]]
It also supports the following indexing methods, similar to MATLAB
>>> a[:,1]array([ 6., 7., 9., 4., 3.])>>> a[:,3]array([ 9., 1., 3., 0., 9.])>>> a[1,:]array([ 4., 7., 2., 1.])>>> a[:,1:3]array([[ 6., 4.], [ 7., 2.], [ 9., 7.], [ 4., 6.], [ 3., 2.]])
3. Matrix arithmetic operations
>>> Aarray ([[4 ., 6 ., 4 ., 9.], [4 ., 7 ., 2 ., 1.], [4 ., 9 ., 7 ., 3.], [5 ., 4 ., 6 ., 0.], [4 ., 3 ., 2 ., 9.]) >>> a. sum () 93.0 >>>. sum (axis = 0) # Add an array ([21 ., 29 ., 21 ., 22.]) >>> a. sum (axis = 1) # Add an array ([23 ., 14 ., 23 ., 15 ., 18.]) >>> a. min () 0.0 >>>. min (axis = 0) array ([4 ., 3 ., 2 ., 0.]) >>> a. min (axis = 1) array ([4 ., 1 ., 3 ., 0 ., 2.]) >>> a. max () 9.0 >>>. max (axis = 0) array ([5 ., 9 ., 7 ., 9.]) >>> a. max (axis = 1) array ([9 ., 7 ., 9 ., 6 ., 9.])
Arithmetic Operations between Arrays
A + B; A-B
For more information, see tentative numpy tutorial.
Basic numpy Learning