数据分析与可视化 实践基础练习一(NumPy)
一、Numpy相关函数或属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 1. np.array() 2. np.arange(n) 3. np.ones(shape)
4. np.zeros(shape) 5. np.full(shape,val) 6. np.eye(n)
7. np.diag(n) 8. np.linspace() 9. np.logspace()
10. np.random.randint() 11. .reshape(shape) 12. .resize(shape)
13. .swapaxes(ax1,ax2) 14. .flatten() 15. .astype()
16. .tolist() 17. np.hstack() 18. np.vstack()
19. np.concatenate() 20. np.hsplit() 21. np.vsplit()
22. np.split() 23. .tanspose() 24. .T
|
二、练习题目
基础题:
1、创建一个元素为从10到49的ndarray对象D1;
2、将D1的所有元素位置反转;
3、使用np.random.random创建一个10 x 10的ndarray对象,并打印出最大最小元素;
4、创建一个10 x 10的ndarray对象,且矩阵边界全为1,里面全为0;
5、创建一个范围在(0,1)之间的长度为12的等差数列;
6、创建一个长度为10的随机数组并排序;
7、给定一个4维矩阵,如何得到最后两维的和?
8、如何将数组a = np.arange(10).reshape(2,-1)和数组b = np.repeat(1, 10).
reshape(2,-1)水平堆叠?
补充题:
9、有arr = np.arange(12).reshape(3,4),若定义mask = np.array([1,0,1],dtype = np.bool),则arr[mask,1] 对应的元素是?
10、有arr = np.arange(12).reshape(3,4),则arr[(0,1),(1,3)]对应的值是?
11、将数组arr中所有的奇数置为-1对应的语句是?
12、a = np.arange(8).reshape(2,4),np.hsplit(a,2)返回值是?
13、找出数组np.array([1,2,1,1,3,4, 3,1,1,2,1,1,2])中第五个1出现的位置的代码;
三、题解
(1)基础题(1 - 8)
1 2 3 4 5
|
import numpy as np D1 = np.arange(10, 50) print('D1:', D1)
|
D1: [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
1 2 3 4 5 6
|
import numpy as np D1 = np.arange(10, 50) a = D1[: : -1] print('a:', a)
|
a: [49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10]
1 2 3 4 5 6 7
|
import numpy as np a = np.random.random( (10, 10) )
print('MaxValue:', a.max()) print('MinValue:', a.min())
|
MaxValue: 0.9627995218664797
MinValue: 0.005820752655498862
1 2 3 4 5 6
|
import numpy as np a = np.zeros( (10,10) ) a[0, : ] = a[9, : ] = a[ : , 0] = a[ : , 9] = 1 print(a)
|
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
1 2 3 4 5
|
import numpy as np a = np.linspace(0, 1, 12) print(a)
|
[0. 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545
0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1. ]
1 2 3 4 5 6
|
import numpy as np a = np.random.random(10) print('a:', a) print(np.sort(a))
|
a: [0.46602273 0.68013528 0.39960639 0.59603541 0.30224607 0.43446988
0.84969599 0.80521228 0.6243939 0.43140521]
[0.30224607 0.39960639 0.43140521 0.43446988 0.46602273 0.59603541
0.6243939 0.68013528 0.80521228 0.84969599]
1 2 3 4 5 6
|
import numpy as np a = np.ones( (1, 2, 3, 4) ) print('a:', a) print('Sum:', a.sum(axis = (2, 3)) )
|
a: [[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]]
Sum: [[12. 12.]]
1 2 3 4 5 6 7 8 9 10
|
import numpy as np a = np.arange(10).reshape(2, -1) print('a:', a) b = np.repeat(1, 10).reshape(2, -1) print('b:', b)
c= np.hstack( (a, b) ) print('c:', c)
|
1 2 3 4 5 6
| a: [[0 1 2 3 4] [5 6 7 8 9]] b: [[1 1 1 1 1] [1 1 1 1 1]] c: [[0 1 2 3 4 1 1 1 1 1] [5 6 7 8 9 1 1 1 1 1]]
|
(2)补充题型(9 - 13)
1 2 3 4 5 6
|
import numpy as np a = np.arange(12).reshape(3, 4) mask = np.array([1,0,1],dtype = np.bool) print(a[mask,1])
|
1 2 3 4 5 6
|
import numpy as np a = np.arange(12).reshape(3, 4) print(a[(0,1),(1,3)]) print(a[1:2,(0, 3)])
|
1 2 3 4 5 6 7
|
import numpy as np a = np.random.randint(1,12, (3,4) ) print(a) a[a % 2 != 0] = -1 print(a)
|
1 2 3 4 5 6
| [[ 8 10 9 2] [ 5 6 4 8] [10 6 7 7]] [[ 8 10 -1 2] [-1 6 4 8] [10 6 -1 -1]]
|
1 2 3 4 5 6
|
import numpy as np a = np.arange(8).reshape(2,4) print(a) print(np.hsplit(a, 2))
|
1 2 3 4 5 6
| [[0 1 2 3] [4 5 6 7]] [array([[0, 1], [4, 5]]), array([[2, 3], [6, 7]])]
|
1 2 3 4 5 6
|
import numpy as np a = np.array([1,2,1,1,3,4,3,1,1,2,1,1,2]) index = np.where(a == 1)[0][4] print(index)
|
四、参考资料
视频:
【一个10分钟的numpy入门教程】
【Python Numpy入门精华】
文章:
NumPy官方文档
Numpy经典题目简答
Numpy使用sort和argsort函数进行排序
Python3:numpy模块中的argsort()函数