This article illustrates the basic use of NUMPY. Published on my public account: Pythonic

For a better reading experience

Python data analysis package | NumPy – 02

1. One-dimensional arrays

Create an array

In [77] :import numpy as np
In [78]: np.array([1.2.3])
Out[78]: array([1.2.3])
Copy the code

Numpy creates arrays of all ones, zeros, and random numbers

In [79]: np.ones(3)
Out[79]: array([1..1..1.])

In [80]: np.zeros(3)
Out[80]: array([0..0..0.])
 In [81]: np.random.random(3) Out[81]: array([0.95802737.0.47883203.0.12742189]) Copy the code

An array of addition

In [83]: data = np.array([1.2])
In [84]: ones = np.ones(2)

In [85]: data
Out[85]: array([1.2])
 In [86]: ones Out[86]: array([1..1.]) In [87]: data + ones Out[87]: array([2..3.]) Copy the code

Array subtraction, multiplication, and division

In [88]: data - ones
Out[88]: array([0..1.])

In [89]: data * ones
Out[89]: array([1..2.])
 In [90]: data / ones Out[90]: array([1..2.]) Copy the code

The operation of arrays and constants, broadcasting

In [91]: data * 1.6This computational logic is called broadcasting
Out[91]: array([1.6.3.2])
Copy the code

Array index

In [99]: data = np.array([1.2.3])
In [100]: data
Out[100]: array([1.2.3])

In [101]: data[0]
Out[101] :1  In [102]: data[1] Out[102] :2   In [104]: data[0:2] Out[104]: array([1.2])  In [105]: data[1:] Out[105]: array([2.3]) Copy the code

Array Simple statistical aggregation

Some functions of statistical calculation:

Function Name DescriptionNp. Sum computes the sum of elementsNp. Prod computes the product of elementsNp. mean Computes the average value of elementsNp.std calculates the standard deviation of elementsNp. var computes the variance of elementsNp.min find the minimum valueNp. Max Find the maximum valueNp. argmin finds the index of the minimum valueNp. argmax finds the maximum indexNp. median computes the median of elementsPercentile np.nanpercentile computes statistics based on element sortingNp. any verifies whether any element is trueNp. all verifies that all elements are trueCopy the code
In [106]: data.max()
Out[106] :3

In [107]: data.min()
Out[107] :1
 In [108]: data.sum() Out[108] :6 Copy the code


2. Two-dimensional arrays

  • To build an array

In [2] :import numpy as np
In [3]: np.array([[1.2], [3.4]])
Out[3] :array([[1.2].       [3.4]])
Copy the code

Ones, Zeros, and Random create arrays

In [4]: np.ones((3.2))# array of all ones
Out[4] :array([[1..1.].       [1..1.].       [1..1.]])
 In [5]: np.zeros((3.2))# an array of zeros Out[5] :array([[0..0.]. [0..0.]. [0..0.]])  In [6]: np.random.random((3.2))# Random number array Out[6] :array([[0.99160431.0.94423442]. [0.77457438.0.442339]. [0.18536075.0.44602502]]) Copy the code

Simple array operations (+)

In [7]: data =  np.array([[1.2], [3.4]])
In [8]: ones = np.ones((2.2))
In [9]: data
Out[9] :array([[1.2]. [3.4]])  In [10]: ones Out[10] :array([[1..1.]. [1..1.]])  In [11]: data + ones Out[11] :array([[2..3.]. [4..5.]]) Copy the code

Simple array arithmetic (broadcast algorithm)

In [12]: ones_row = np.ones(2)
In [13]: data + ones_rowThe broadcast algorithm is also used here
Out[13] :array([[2..3.].       [4..5.]])
Copy the code

Matrix operation (DOT)

As in linear algebra, an array of shape (x, n) is multiplied by an array of shape (n, y) to produce an array of shape (x, y). The number of columns in the first array equals the number of rows in the second array.

In [20]: data = np.array([1.2.3])
In [21]: power_of_ten = np.array([1.10.100.1000.10000.100000]).reshape(3.2)

In [22]: data
Out[22]: array([1.2.3])
 In [23]: power_of_ten Out[23] :array([[ 1.10]. [ 100.1000]. [ 10000.100000]])  In [24]: data.dot(power_of_ten) Out[24]: array([ 30201.302010])  In [25]: data.shape Out[25] : (3.)Array of 1 rows and 3 columns  In [26]: power_of_ten.shape Out[26] : (3.2)#3 row, two column array  In [27]: data.dot(power_of_ten).shape Out[27] : (2.)# multiply an array of 1 rows and 2 columns Copy the code

Two-dimensional array index

In [32]: data = np.arange(1.7).reshape((3.2))
In [33]: data
Out[33] :array([[1.2].       [3.4]. [5.6]]) n [34]: data[0.1] Out[34] :2  In [35]: data[1:3] Out[35] :array([[3.4]. [5.6]])  In [36]: data[0:2.0]#[0:2,0] indicates the range of rows before the comma and the range of columns after the comma Out[36]: array([1.3]) Copy the code

A two-dimensional array behaves like a one-dimensional array

In [38]: data.max()
Out[38] :6

In [39]: data.min()
Out[39] :1
 In [40]: data.sum() Out[40] :21 Copy the code

A two-dimensional array performs statistical operations along an axis (passing in an axis parameter)

In [43]: data = np.array([[1.2], [5.3], [4.6]])
In [44]: data
Out[44] :array([[1.2].       [5.3]. [4.6]])  In [46]: data.max() Out[46] :6  In [47]: data.min() Out[47] :1  In [48]: data.sum() Out[48] :21  In [49]: data.max(axis=0)Find the maximum value of each column (axis 0) Out[49]: array([5.6])  In [50]: data.max(axis=1)Find the maximum value of each row (1 axis) Out[50]: array([2.5.6]) Copy the code

  • Rank of 2-d array rotation (row to column or 1-axis to 0-axis)

In [51]: data = np.arange(1.7).reshape(3.2)
In [52]: data
Out[52] :array([[1.2].       [3.4]. [5.6]])  In [53]: data.T Out[53] :array([[1.3.5]. [2.4.6]]) Copy the code

Shape two dimensional array shape

In [54]: data = np.arange(1.7)
In [55]: data
Out[55]: array([1.2.3.4.5.6])
In [56]: data.reshape(2.3)
Out[56] :array([[1.2.3]. [4.5.6]])  In [57]: data.reshape(3.2) Out[57] :array([[1.2]. [3.4]. [5.6]]) Copy the code


3. Three-dimensional arrays

Creating a THREE-DIMENSIONAL array

In [65]: np.ones((4.3.2))# The picture below can show the meaning vividly
Out[65] :array([[[1..1.].        [1..1.].        [1..1.]],
  [[1..1.]. [1..1.]. [1..1.]],   [[1..1.]. [1..1.]. [1..1.]],   [[1..1.]. [1..1.]. [1..1.]]]) Copy the code

In [66]: np.arange(1.9).reshape(2.2.2)# The image below is more graphic
Out[66] :array([[[1.2].        [3.4]],

 [[5.6]. [7.8]]]) Copy the code


The resources

Jalammar. Making. IO/visual – nump… Python data science manual B stand up: www.bilibili.com/video/BV134…


For a better reading experience

Python data analysis package | NumPy – 02


Welcome to our official account:Pythonic biological people