This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

The environment

  • Ubuntu 18.04 64 – bit
  • Numpy 1.19.2

Introduction to the

Numpy is an open source library for numerical computation in Python. It is used to store and manipulate multi-dimensional arrays. The core data structure is NDARray. This article shares the basics of using Ndarray, starting with numpy installed

pip install numpy
Copy the code

Two-dimensional ndarray

To construct a two-dimensional array (that is, a matrix), you need to know the number of rows, columns and the data type of each element of the array. In Numpy, the common ones are uint8, Int32, float32, float64 and so on

Construct a uint8 type 2 d array with 2 rows and 3 columns all 0

(base) xugaoxiang@1070Ti:~$ ipython
Python 3.7.6 (default, Jan  8 2020, 19:59:22)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numpy as np

In [2]: np.zeros((2, 3), np.uint8)
Out[2]:
array([[0, 0, 0],
       [0, 0, 0]], dtype=uint8)

In [3]:

Copy the code

If you want to construct an array of all ones, you can use the np.ones() method

In [5]: np.ones((2, 3), np.uint8)
Out[5]:
array([[1, 1, 1],
       [1, 1, 1]], dtype=uint8)
Copy the code

Initialize with constant data

In [8]: np.array([[1, 2, 3], [4, 5, 6]], np.float64)
Out[8]:
array([[1., 2., 3.],
       [4., 5., 6.]])

Copy the code

The three dimensional ndarray

A three-dimensional array can be understood as each element is a two-dimensional array. For example, a 2x2x3 uint8 three-dimensional array is two 2×3 two-dimensional arrays

In [11]: np.array([[[1, 2, 3], [4, 5, 6]], [[9, 8, 7], [6, 5, 4]]], np.uint8)
Out[11]:
array([[[1, 2, 3],
        [4, 5, 6]],

       [[9, 8, 7],
        [6, 5, 4]]], dtype=uint8)

In [12]:

Copy the code

With that in mind, higher dimensional arrays are easy to understand

Ndarray common attributes and element value acquisition

The Shape property can get the size, for example

In [12]: t = np.zeros([2, 3], np.uint8) In [13]: t.shape Out[13]: (2, 3) In [14]: In [14]: t3 = np.array([[[1, 2, 3], [4, 5, 6]], [[9, 8, 7], [6, 5, 4]]],np.uint8 ... : ) In [15]: t3.shape Out[15]: (2, 2, 3) In [16]:Copy the code

The dtype attribute retrieves the data type of the element, for example

In [17]: t.dtype
Out[17]: dtype('uint8')

In [18]: t3.dtype
Out[18]: dtype('uint8')

In [19]:

Copy the code

Gets the value of an element in an array, which can be obtained by an index that starts at 0, for example in two dimensions

In [20]: t = np.array([[1, 2, 3], [4, 5, 6]], np.uint8)

In [21]: t[1, 1]
Out[21]: 5

In [22]: t[0, 2]
Out[22]: 3

In [23]:

Copy the code

Get a row of data, as shown in the t variable above

In [36]: t[0,:] Out[36]: array([1, 2, 3], dtype=uint8) In [37]: t[1,:] Out[37]: Array ([4, 5, 6], dtype=uint8) t[2,:] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-38-79ac94a5bef6> in <module> ----> 1 t[2,:] IndexError: index 2 is out of bounds for axis 0 with size 2 In [39]:Copy the code

Get a column of data, for example, the t variable above

In [40]: t[:,0]
Out[40]: array([1, 4], dtype=uint8)

In [41]: t[:,1]
Out[41]: array([2, 5], dtype=uint8)

In [42]: t[:,2]
Out[42]: array([3, 6], dtype=uint8)

In [43]: t[:,3]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-43-cd95ba35026b> in <module>
----> 1 t[:,3]

IndexError: index 3 is out of bounds for axis 1 with size 3

In [44]:

Copy the code

In addition, you can also get an interval, as shown below

In [45]: t[0:1,1:3] Out[45]: array([[2, 3]], dtype=uint8) In [46]:Copy the code