“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

What is Numpy?

Numpy is Python’s open source scientific computing toolkit and an advanced numerical programming tool

  • Powerful N – dimensional array objects: NDARray
  • You can operate on array structured data (without iterating through loops)
  • Random number, linear algebra, Fourier transform and other functions

How to install?

Install the Anaconda scientific computing environment

Anaconda is recommended because it integrates many common libraries and makes it easier to configure the environment.

Download address: https://www.anaconda.com/download/

Specific installation steps, here no longer repeat, do not understand the friends can be discussed in the exchange group, can also refer to the following blog:

cuiqingcai.com/5059.html

Install Numpy

Method 1: After Anaconda is installed, Numpy can be used directly without secondary installation.

PIP install numpy is used to install anaconda.

Notebooks to install Jupyter notebooks

Method 1: After installing the Anaconda, the notebooks are ready for immediate use without the need for a second notebook.

PIP install Jupyter is used to install anaconda.

Numpy basic data structures

The import

It is recommended to use from numpy import NP

The use of from numpy import * is not recommended because numpy contains a large number of functions with the same name as Python’s built-in functions.

Generate ndarray

We can use array to generate arrays. For example:

import numpy as np
ar = np.array([[1.2.3.4], [1.2.3.4]])
print(ar, type(ar))

>>>
[[1 2 3 4]
  [1 2 3 4]] <class 'numpy.ndarray'>
Copy the code

There are other functions besides np.array to create new arrays. Here are a few commonly used ones:

arange # Array version of Python range
asarray # convert input to NDARray
ones Generate an array of all ones based on the given shape and type
ones_like # Generate an array of all ones of the same shape from the given array
zeros Generate an array of all zeros based on the given shape and type
zeros_like # Generate an array of all ones of the same shape from the given array
eye Generate an N by N eigenmatrix (diagonal = 1, rest = 0)
linspance # return num evenly spaced samples calculated on the interval [start, stop]
Copy the code

Zeros, ZerOS_Like, and Linspance are examples:

arr = np.zeros((3.5))
print(arr)

>>>
[[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

s = np.array([list(range(10)),list(range(10.20)))print(s)
print(np.zeros_like(s))

>>>
[[ 0  1  2  3  4  5  6  7  8  9]
  [10 11 12 13 14 15 16 17 18 19]]
[[0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0]]

print(np.linspace(10.20,num = 21)) # is generated between 10 and 21
print(np.linspace(10.20,num = 21, endpoint = False)) #endpoint defaults to True, False does not include the values on the left
print(np.linspace(10.20,num = 21, retstep = True))# restep displays the step size> > > [10.  10.5 11.  11.5 12.  12.5 13.  13.5 14.  14.5 15.  15.5 16.  16.5
 17.  17.5 18.  18.5 19.  19.5 20. ]
[10.         10.47619048 10.95238095 11.42857143 11.9047619  12.38095238
 12.85714286 13.33333333 13.80952381 14.28571429 14.76190476 15.23809524
 15.71428571 16.19047619 16.66666667 17.14285714 17.61904762 18.0952381
 18.57142857 19.04761905 19.52380952]
(array([10. , 10.5.11. , 11.5.12. , 12.5.13. , 13.5.14. , 14.5.15. ,
       15.5.16. , 16.5.17. , 17.5.18. , 18.5.19. , 19.5.20. ]), 0.5)
Copy the code

In addition to the usual array generation functions, here are some common methods:

import numpy as np
ar = np.array([[1.2.3.4], [1.2.3.4]])
print(ar, type(ar))
print(ar.ndim)# return the number of dimensions of the array
print(ar.shape)# dimension of array, return several rows and several columns
print(ar.size)Number of elements in the array
print(ar.dtype)The type of the element
print(ar.itemsize)The size of the element in the array> > > [[1 2 3 4]
 [1 2 3 4]] <class 'numpy.ndarray'>
2
(2.48)int64
8
Copy the code

Numpy general function

Array shape Change (.t /.resize())

T is the transpose function, which has no effect on one-dimensional arrays

# .T
import numpy as np
ar1 = np.arange(10)
ar2 = np.zeros((2.5))
print(ar1.T)
print(ar2.T)# transpose> > > [0 1 2 3 4 5 6 7 8 9]
[[0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]]
Copy the code

0.0 (), Directly changes the shape of the array, but the number of elements must be the same before and after the change

ar1 = np.arange(10)
print(ar1.reshape(2.5))
print(np.reshape(np.arange(16), (2.8))) > > > [[0 1 2 3 4]
 [5 6 7 8 9]]
[[ 0  1  2  3  4  5  6  7]
 [ 8  9 10 11 12 13 14 15]]
Copy the code

.resize()

print(np.resize(np.arange(16), (3.5)))  # resize When the number of elements in the following array is smaller than the number of previously generated elements, the sequence is iterated
print(np.resize(np.arange(12), (3.5)))  When the number of elements in the following array is greater than the number of elements in the previous generation, it is randomly populated> > > [[0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11  0  1  2]]
Copy the code
Array copy

Similar to deep and shallow copying in Python:

Type conversion of an array

.astype() can convert the types of elements in an array. In Numpy there are several types of elements (too many to write them all) :

int8, uint8 # signed and unsigned 8-bit integers
int16, uint16 # signed and unsigned 16-bit integers
int32, uint32 # signed and unsigned 32 integer integers
int64, uint64 # signed and unsigned 64 integer integers
float16 # semi precision
float32 # single precision
float64 # double
bool # Boolean.Copy the code

Here’s an example of a type conversion:

ar1 = np.arange(10,dtype=float)
ar2 = ar1.astype(np.int64)
print(ar1,ar2)

>>>
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] [0 1 2 3 4 5 6 7 8 9]
Copy the code
Stacking arrays

Arrays are stacked hstack(),vstack(), and stack(), for example:

a = np.arange(10)
b = np.arange(10.20)
print(ar1,ar2)
# Horizontal linking
print(np.hstack((a,b)))
# vertical link
a = np.array([[1], [2], [3]])
b = np.array([['a'], ['b'], ['c']])
print(np.vstack((a,b)))
# Arbitrary stack
a = np.arange(10)
b = np.arange(10.20)
print(np.stack((a,b),axis=1)) # Vertical stack
print(np.stack((a,b))) # Horizontal stack> > > > [0 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18 19]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
[['1']
 ['2']
 ['3']
 ['a']
 ['b']
 ['c']]
[[ 0 10]
 [ 1 11]
 [ 2 12]
 [ 3 13]
 [ 4 14]
 [ 5 15]
 [ 6 16]
 [ 7 17]
 [ 8 18]
 [ 9 19]]
[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]]
Copy the code
An array of split

Array split is also divided into horizontal split and vertical split.

# array split
ar = np.arange(16).reshape(4.4)
print(ar)
print(np.hsplit(ar,2)) # Vertical split
print(np.vsplit(ar,2)) # Horizontal split> > > [[0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[array([[ 0.1],
       [ 4.5],
       [ 8.9],
       [12.13]]), array([[ 2.3],
       [ 6.7],
       [10.11],
       [14.15]])]
[array([[0.1.2.3],
       [4.5.6.7]]), array([[ 8.9.10.11],
       [12.13.14.15]])]
Copy the code
Common computing function

The calculation functions used here are the same as those used in Python and will not be discussed here.

# Compute function
np.mean() # average
np.max(a)A maximum #
np.min(a)# the minimum
np.gtd() # standard deviation
np.var() # variance
np.sum(a)Axis =0 axis=1 axis=1
Copy the code

Strengthen practice

  1. Generate a one-dimensional array, a two-dimensional array, and look at its shape
  2. Generate a one-dimensional array with a start value of 5, an end value of 15 and a sample number of 10
  3. Create an array of 20 elements and change them to two shapes :(4,5),(5,6)
  4. Create an array of (4,4) and change its element type to character
  5. Create a two-dimensional array AR, start value is 0, end value is 15, use the array operation method to get the result: result = AR * 10 +100, and calculate the mean value of result and sum