A complete collection of Numpy practical exercises suitable for beginners to get started quickly

A complete collection of Numpy practical exercises suitable for beginners to get started quickly

Numpy is an extension library for scientific computing implemented in python, including:

  • 1. A powerful N-dimensional array object Array;
  • 2. A relatively mature (broadcast) function library;
  • 3. Toolkit for integrating C/C++ and Fortran code;
  • 4. Practical linear algebra, Fourier transform and random number generating function. It is more convenient to use numpy with the sparse matrix operation package scipy.

NumPy (Numeric Python) provides many advanced numerical programming tools, such as matrix data types, vector processing, and sophisticated arithmetic libraries. Built for rigorous number crunching. It is mostly used by many large financial companies, as well as core scientific computing organizations such as Lawrence Livermore, and NASA uses it to handle some tasks that were originally done using C++, Fortran or Matlab.

Contents of this article

1.Basic operations of Numpy

1.1 Convert list to matrix

1.2 Dimensions

1.3 Number of rows and columns ()

1.4 Number of elements

2.Numpy creates array

2.1 One-dimensional array creation

2.2 Multidimensional array creation

2.3 Create an all-zero array

2.4 Create all 1 data

2.5 Create an all-empty array

2.6 Create continuous arrays

2.7 reshape operation

2.8 Create continuous data

2.9 linspace reshape operation

3.Numpy basic operations

3.1 One-dimensional matrix operations

3.2 Multidimensional matrix operations

3.3 Basic calculations

4.Numpy indexing and slicing

5.Numpy array merging

5.1 Array merging

5.2 Transpose array to matrix

5.3 Merging multiple matrices

5.4 Merge example 2

6.Numpy array division

6.1 Construct a matrix with 3 rows and 4 columns

6.2 Equal division

6.3 Unequal division

6.4 Other segmentation methods

7.Numpy copy and =

7.1 =The assignment method will be associative

7.2 The copy() assignment method has no relevance

8.Broadcast mechanism

9. Commonly used functions

1.Basic operations of Numpy

1.1 Convert list to matrix

import numpy as np
array = np.array([
    [1,3,5],
    [4,6,9]
])

print(array)
[[1 3 5]
 [4 6 9]]

1.2 Dimensions

print('number of dim:', array.ndim)
number of dim: 2

1.3 Number of rows and columns ()

print('shape:',array.shape)
shape: (2, 3)

1.4 Number of elements

print('size:',array.size)
size: 6

2.Numpy creates array

2.1 One-dimensional array creation

import numpy as np
# 一维array
a = np.array([2,23,4], dtype=np.int32) # np.int默认为int32
print(a)
print(a.dtype)
[ 2 23  4]
int32

2.2 Multidimensional array creation

# 多维array
a = np.array([[2,3,4],
              [3,4,5]])
print(a) # 生成2行3列的矩阵
[[2 3 4]
 [3 4 5]]

2.3 Create an all-zero array

a = np.zeros((3,4))
print(a) # 生成3行4列的全零矩阵
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

2.4 Create all 1 data

# 创建全一数据,同时指定数据类型
a = np.ones((3,4),dtype=np.int)
print(a)
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]


C:\Users\48125\AppData\Local\Temp\ipykernel_18880\1001327096.py:2: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  a = np.ones((3,4),dtype=np.int)

2.5 Create an all-empty array

# 创建全空数组,其实每个值都是接近于零的数
a = np.empty((3,4))
print(a)
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

2.6 Create continuous arrays

# 创建连续数组
a = np.arange(10,21,2) # 10-20的数据,步长为2
print(a)
[10 12 14 16 18 20]

2.7 reshape operation

# 使用reshape改变上述数据的形状
b = a.reshape((2,3))
print(b)
[[10 12 14]
 [16 18 20]]

2.8 Create continuous data

# 创建线段型数据
a = np.linspace(1,10,20) # 开始端1,结束端10,且分割成20个数据,生成线段
print(a)
[ 1.          1.47368421  1.94736842  2.42105263  2.89473684  3.36842105
  3.84210526  4.31578947  4.78947368  5.26315789  5.73684211  6.21052632
  6.68421053  7.15789474  7.63157895  8.10526316  8.57894737  9.05263158
  9.52631579 10.        ]

2.9 linspace reshape operation

# 同时也可以reshape
b = a.reshape((5,4))
print(b)
[[ 1.          1.47368421  1.94736842  2.42105263]
 [ 2.89473684  3.36842105  3.84210526  4.31578947]
 [ 4.78947368  5.26315789  5.73684211  6.21052632]
 [ 6.68421053  7.15789474  7.63157895  8.10526316]
 [ 8.57894737  9.05263158  9.52631579 10.        ]]

3.Numpy basic operations

3.1 One-dimensional matrix operations

import numpy as np
# 一维矩阵运算
a = np.array([10,20,30,40])
b = np.arange(4)
print(a,b)
[10 20 30 40] [0 1 2 3]
c = a - b
print(c)
[10 19 28 37]
print(a*b) # 若用a.dot(b),则为各维之和
[  0  20  60 120]
# 在Numpy中,想要求出矩阵中各个元素的乘方需要依赖双星符号 **,以二次方举例,即:
c = b**2
print(c)
[0 1 4 9]
# Numpy中具有很多的数学函数工具
c = np.sin(a)
print(c)
[-0.54402111  0.91294525 -0.98803162  0.74511316]
print(b<2)
[ True  True False False]
a = np.array([1,1,4,3])
b = np.arange(4)
print(a==b)
[False  True False  True]

3.2 Multidimensional matrix operations

a = np.array([[1,1],[0,1]])
b = np.arange(4).reshape((2,2))
print(a)
[[1 1]
 [0 1]]
print(b)
[[0 1]
 [2 3]]
# 多维度矩阵乘法
# 第一种乘法方式:
c = a.dot(b)
print(c)
[[2 4]
 [2 3]]
# 第二种乘法:
c = np.dot(a,b)
print(c)
[[2 4]
 [2 3]]
# 多维矩阵乘法不能直接使用'*'号

a = np.random.random((2,4))

print(np.sum(a)) 
4.566073674715756
print(np.min(a))
0.28530553227025357
print(np.max(a))
0.8647092701217907
print("a=",a)
a= [[0.28530553 0.4239227  0.82876104 0.64553364]
 [0.43444521 0.35855996 0.72483633 0.86470927]]

If you need to perform a search operation on rows or columns,

You need to assign a value to axis in the above code.

When the value of axis is 0, the column will be used as the search unit.

When the value of axis is 1, the row will be used as the search unit.

print("sum=",np.sum(a,axis=1))
sum= [2.18352291 2.38255077]
print("min=",np.min(a,axis=0))
min= [0.28530553 0.35855996 0.72483633 0.64553364]
print("max=",np.max(a,axis=1))
max= [0.82876104 0.86470927]

3.3 Basic calculations

import numpy as np

A = np.arange(2,14).reshape((3,4))
print(A)
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
# 最小元素索引
print(np.argmin(A)) # 0
0
# 最大元素索引
print(np.argmax(A)) # 11
11
# 求整个矩阵的均值
print(np.mean(A)) # 7.5
7.5
print(np.average(A)) # 7.5
7.5
print(A.mean()) # 7.5
7.5
# 中位数
print(np.median(A)) # 7.5
7.5
# 累加
print(np.cumsum(A))
[ 2  5  9 14 20 27 35 44 54 65 77 90]
# 累差运算
B = np.array([[3,5,9],
              [4,8,10]])
print(np.diff(B))
[[2 4]
 [4 2]]
C = np.array([[0,5,9],
              [4,0,10]])
print(np.nonzero(B))
print(np.nonzero(C))
(array([0, 0, 0, 1, 1, 1], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64))
(array([0, 0, 1, 1], dtype=int64), array([1, 2, 0, 2], dtype=int64))
# 仿照列表排序
A = np.arange(14,2,-1).reshape((3,4)) # -1表示反向递减一个步长
print(A)
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
print(np.sort(A))
[[11 12 13 14]
 [ 7  8  9 10]
 [ 3  4  5  6]]
# 矩阵转置
print(np.transpose(A))
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
print(A.T)
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
print(A)
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
print(np.clip(A,5,9))
[[9 9 9 9]
 [9 9 8 7]
 [6 5 5 5]]

clip(Array,Array_min,Array_max)

Represent Array_min<X<Array_max X as the number in matrix A. If the above relationship is satisfied, the original number remains unchanged.

Otherwise, if X<Array_min, change X in the matrix to Array_min;

If X>Array_max, change X in the matrix to Array_max.

4.Numpy indexing and slicing

import numpy as np
A = np.arange(3,15)
print(A)
[ 3  4  5  6  7  8  9 10 11 12 13 14]
print(A[3])
6
B = A.reshape(3,4)
print(B)
[[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
print(B[2])
[11 12 13 14]
print(B[0][2])
5
print(B[0,2])
5
# list切片操作
print(B[1,1:3]) # [8 9] 1:3表示1-2不包含3
[8 9]
for row in B:
    print(row)
[3 4 5 6]
[ 7  8  9 10]
[11 12 13 14]
# 如果要打印列,则进行转置即可
for column in B.T:
    print(column)
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
[ 6 10 14]
# 多维转一维
A = np.arange(3,15).reshape((3,4))
# print(A)
print(A.flatten())
# flat是一个迭代器,本身是一个object属性
[ 3  4  5  6  7  8  9 10 11 12 13 14]
for item in A.flat:
    print(item)
3
4
5
6
7
8
9
10
11
12
13
14

Let’s summarize it together and look at the following slice value method (the corresponding color is the result of taking it out):

Insert image description here

5.Numpy array merging

5.1 Array merging

import numpy as np
A = np.array([1,1,1])
B = np.array([2,2,2])
print(np.vstack((A,B)))
# vertical stack 上下合并,对括号的两个整体操作。
[[1 1 1]
 [2 2 2]]
C = np.vstack((A,B))
print(C)
[[1 1 1]
 [2 2 2]]
print(A.shape,B.shape,C.shape)# 从shape中看出A,B均为拥有3项的数组(数列)
(3,) (3,) (2, 3)
# horizontal stack左右合并
D = np.hstack((A,B))
print(D)
[1 1 1 2 2 2]
print(A.shape,B.shape,D.shape)
# (3,) (3,) (6,)
# 对于A,B这种,为数组或数列,无法进行转置,需要借助其他函数进行转置
(3,) (3,) (6,)

5.2 Transpose array to matrix

print(A[np.newaxis,:]) # [1 1 1]变为[[1 1 1]]
[[1 1 1]]
print(A[np.newaxis,:].shape) # (3,)变为(1, 3)
(1, 3)
print(A[:,np.newaxis])
[[1]
 [1]
 [1]]

5.3 Merging multiple matrices

# concatenate的第一个例子
print("------------")
print(A[:,np.newaxis].shape) # (3,1)
------------
(3, 1)
A = A[:,np.newaxis] # 数组转为矩阵
B = B[:,np.newaxis] # 数组转为矩阵
print(A)
[[1]
 [1]
 [1]]
print(B)
[[2]
 [2]
 [2]]
# axis=0纵向合并
C = np.concatenate((A,B,B,A),axis=0)
print(C)
[[1]
 [1]
 [1]
 [2]
 [2]
 [2]
 [2]
 [2]
 [2]
 [1]
 [1]
 [1]]
# axis=1横向合并
C = np.concatenate((A,B),axis=1)
print(C)
[[1 2]
 [1 2]
 [1 2]]

5.4 Merge example 2

# concatenate的第二个例子
print("-------------")
a = np.arange(8).reshape(2,4)
b = np.arange(8).reshape(2,4)
print(a)
print(b)
print("-------------")
-------------
[[0 1 2 3]
 [4 5 6 7]]
[[0 1 2 3]
 [4 5 6 7]]
-------------
# axis=0多个矩阵纵向合并
c = np.concatenate((a,b),axis=0)
print(c)
[[0 1 2 3]
 [4 5 6 7]
 [0 1 2 3]
 [4 5 6 7]]
# axis=1多个矩阵横向合并
c = np.concatenate((a,b),axis=1)
print(c)
[[0 1 2 3 0 1 2 3]
 [4 5 6 7 4 5 6 7]]

6.Numpy array division

6.1 Construct a matrix with 3 rows and 4 columns

import numpy as np
A = np.arange(12).reshape((3,4))
print(A)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

6.2 Equal division

# 等量分割
# 纵向分割同横向合并的axis
print(np.split(A, 2, axis=1))
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
# 横向分割同纵向合并的axis
print(np.split(A,3,axis=0))
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]

6.3 Unequal division

print(np.array_split(A,3,axis=1))
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]])]

6.4 Other segmentation methods

# 横向分割
print(np.vsplit(A,3)) # 等价于print(np.split(A,3,axis=0))
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
# 纵向分割
print(np.hsplit(A,2)) # 等价于print(np.split(A,2,axis=1))
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]

7.Numpy copy and =

7.1 =The assignment method will be associative

import numpy as np
# `=`赋值方式会带有关联性
a = np.arange(4)
print(a) # [0 1 2 3]
[0 1 2 3]
b = a
c = a
d = b
a[0] = 11
print(a) # [11  1  2  3]
[11  1  2  3]
print(b) # [11  1  2  3]
[11  1  2  3]
print(c) # [11  1  2  3]
[11  1  2  3]
print(d) # [11  1  2  3]
[11  1  2  3]
print(b is a) # True
True
print(c is a) # True
True
print(d is a) # True
True
d[1:3] = [22,33]
print(a) # [11 22 33  3]
[11 22 33  3]
print(b) # [11 22 33  3]
[11 22 33  3]
print(c) # [11 22 33  3]
[11 22 33  3]

7.2 The copy() assignment method has no relevance

a = np.arange(4)
print(a) # [0 1 2 3]
[0 1 2 3]
b =a.copy() # deep copy
print(b) # [0 1 2 3]
[0 1 2 3]
a[3] = 44
print(a) # [ 0  1  2 44]
print(b) # [0 1 2 3]

# 此时a与b已经没有关联
[ 0  1  2 44]
[0 1 2 3]

8.Broadcast mechanism

The basic operation between numpy arrays is one-to-one, that is a.shape==b.shape, when the two are different, the broadcast mechanism will be automatically triggered, as in the following example:

from numpy import array
a = array([[ 0, 0, 0],
           [10,10,10],
           [20,20,20],
           [30,30,30]])
b = array([0,1,2])
print(a+b)
[[ 0  1  2]
 [10 11 12]
 [20 21 22]
 [30 31 32]]

Why is it like this?

Here we use tile to simulate the above operation and go back to a.shape==b.shapethe situation!

# 对[0,1,2]行重复3次,列重复1次
b = np.tile([0,1,2],(4,1))
print(a+b)
[[ 0  1  2]
 [10 11 12]
 [20 21 22]
 [30 31 32]]

At this point, let’s give a picture

Insert image description here

You can also look at this picture:
Insert image description here

Is it okay in any situation?

Of course not, trailing dimensions compatiblethe broadcast will only be triggered when there are two arrays, otherwise an error will be reported ValueError: frames are not aligned exception.

The above expression means that the tail dimensions must be compatible!

9. Commonly used functions

9.1 np.bincount()

x = np.array([1, 2, 3, 3, 0, 1, 4])
np.bincount(x)
array([1, 2, 1, 2, 1], dtype=int64)

Count the number of index occurrences: index 0 appears once, 1 appears twice, 2 appears once, 3 appears twice, and 4 appears once

Therefore, the number of index occurrences calculated through bincount is as follows:

How did you get the above?

For bincount calculation, the number of bins is 1 more than the maximum number in What do the numbers in represent? It represents the number of times its index value appears in x!

Taking the above x as an example, when we set the weights parameter, what is the result?

This assumes:

w = np.array([0.3,0.5,0.7,0.6,0.1,-0.9,1])

So what is the result after setting this w weight?

np.bincount(x,weights=w)
array([ 0.1, -0.6,  0.5,  1.3,  1. ])

How is it calculated?

First extract x and w:

x ---> [1, 2, 3, 3, 0, 1, 4]

w ---> [0.3,0.5,0.7,0.6,0.1,-0.9,1]
Index 0 appears at index=4 in x, then just access the index=4 position in w, w[4]=0.1

index=0Index 1 appears at the index=0 and index=5 positions in x, then just access the positions of and in w index=5, and then add the two together to calculate: w[0]+w[5]=-0.6
The rest can be done according to the above method!

Another parameter of bincount is minlength. This parameter is simple. It can be understood that when the number of bins given is more than the number of bins actually obtained from x, the ones not accessed later are set to 0.

Or take the above x as an example:

Here we directly set the minlength=7 parameter and output it!

np.bincount(x,weights=w,minlength=7)
array([ 0.1, -0.6,  0.5,  1.3,  1. ,  0. ,  0. ])

Compared with the above, there are two more 0s. How can there be more of these two?

As we know above, the number of bins is 5, and the index is from 0 to 4. Then when the minlength is 7, that is, the total length is 7, and the index is from 0 to 6. If there are two extra bits, just fill in the bits with 0!

9.2 np.argmax()

The function prototype is: numpy.argmax(a, axis=None, out=None).

The function returns the index of the maximum value along the axis.

x = [[1,3,3],
     [7,5,2]]
print(np.argmax(x))
3

For this example we know that 7 is the largest and the index position is 3 (this index is in increasing order)!

axis attribute

axis=0 means operating by column, that is, comparing the current column and finding the index of the maximum value!

x = [[1,3,3],
     [7,5,2]]
print(np.argmax(x,axis=0))
[1 1 0]

axis=1 means operating by row, that is, comparing the current row and finding the index of the maximum value!

x = [[1,3,3],
     [7,5,2]]
print(np.argmax(x,axis=0))
[1 1 0]

What if you encounter the largest repeated element?

Just return the first maximum value index!

For example:

x = np.array([1, 3, 2, 3, 0, 1, 0])
print(x.argmax())
1

9.3 The above merger example

Here we combine the above two functions, for example:

x = np.array([1, 2, 3, 3, 0, 1, 4])
print(np.argmax(np.bincount(x)))
1

The final result is 1, why?

First, np.bincount(x)the result obtained is: [1 2 1 2 1], and then based on the last repeated maximum value item encountered, the index of the first maximum value can be returned! The index of 2 is 1, so 1 is returned.

9.4 Find the accuracy

np.around([-0.6,1.2798,2.357,9.67,13], decimals=0)#取指定位置的精度
array([-1.,  1.,  2., 10., 13.])

Did you see that the negative number is carried to take the larger absolute value?

np.around([1.2798,2.357,9.67,13], decimals=1)
array([ 1.3,  2.4,  9.7, 13. ])
np.around([1.2798,2.357,9.67,13], decimals=2)
array([ 1.28,  2.36,  9.67, 13.  ])

As can be seen from the above, decimals means specifying the number of digits to retain significant numbers. When it exceeds 5, it will be carried (including 5 at this time)!

But what does it mean if this parameter is set to a negative number?

np.around([1,2,5,6,56], decimals=-1)
array([ 0,  0,  0, 10, 60])

Did you find that, when it exceeds 5 (not including 5), it will be carried! -1 means that only one digit can be rounded, so if it is changed to -2, then it has to look at two digits!

np.around([1,2,5,50,56,190], decimals=-2)
array([  0,   0,   0,   0, 100, 200])

Did you see? You have to look at two digits. If it exceeds 50, it will be rounded. If it is 190, it will look at the next two digits. If the last two digits are 90 or more than 50, it will be rounded, so it is 200!

Calculates the discrete difference along the Nth dimension along the specified axis

x = np.arange(1 , 16).reshape((3 , 5))
print(x)
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
np.diff(x,axis=1) #默认axis=1
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
np.diff(x,axis=0) 
array([[5, 5, 5, 5, 5],
       [5, 5, 5, 5, 5]])

Rounding

np.floor([-0.6,-1.4,-0.1,-1.8,0,1.4,1.7])
array([-1., -2., -1., -2.,  0.,  1.,  1.])

Did you see that rounding of negative numbers is the same as around above, to the left!

Take the upper limit

np.ceil([1.2,1.5,1.8,2.1,2.0,-0.5,-0.6,-0.3])
array([ 2.,  2.,  2.,  3.,  2., -0., -0., -0.])

Take the upper limit! Just find the largest integer of this decimal!

Find

Can you use np.where to fill values ​​less than 0 with 0? Numbers greater than 0 remain unchanged!

x = np.array([[1, 0],
       [2, -2],
     [-2, 1]])
print(x)
[[ 1  0]
 [ 2 -2]
 [-2  1]]
np.where(x>0,x,0)
array([[1, 0],
       [2, 0],
       [0, 1]])

One word per text

come on

Guess you like

Origin blog.csdn.net/weixin_47723732/article/details/132882659