numpy 的20道练习题

numpy 的20道练习题

1、创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1

1
2
3
4
import numpy as np
a1 = np.full((10,),fill_value=0)
a1[4] = 1
a1
array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0])

2、创建一个元素为从10到49的ndarray对象

1
2
a2 = np.arange(10,49)
a2
array([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])

3、将第2题的所有元素位置反转

1
2
a3 = a2[::-1]
a3
array([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])

4、使用np.random.random创建一个10*10的ndarray对象,并打印出最大最小元素

1
2
a4 = np.random.random((10,10))
a4
array([[0.27617495, 0.56018129, 0.91427389, 0.20552192, 0.55395542,
        0.87127358, 0.72688637, 0.59354853, 0.06836087, 0.26286294],
       [0.53143422, 0.39668017, 0.18075297, 0.34972236, 0.37983533,
        0.73750756, 0.81768039, 0.04414151, 0.80292802, 0.85319097],
       [0.45443687, 0.71833627, 0.58470417, 0.08229853, 0.6215772 ,
        0.41912925, 0.28170197, 0.6713312 , 0.01013376, 0.47979796],
       [0.21899963, 0.6344341 , 0.38110491, 0.32508574, 0.99656881,
        0.56729262, 0.99187128, 0.1291357 , 0.63540099, 0.68087721],
       [0.56116832, 0.00262813, 0.70933433, 0.368366  , 0.67652735,
        0.15265231, 0.31740615, 0.01317118, 0.84311861, 0.52721208],
       [0.88555201, 0.35807316, 0.36079133, 0.10196417, 0.11629854,
        0.85312728, 0.77705123, 0.33644583, 0.81334978, 0.34222517],
       [0.89541775, 0.7151784 , 0.33882979, 0.34333758, 0.36575743,
        0.61631455, 0.55793113, 0.29363473, 0.35979546, 0.0590574 ],
       [0.80307099, 0.77297977, 0.47928142, 0.81420384, 0.05832932,
        0.31684658, 0.04692587, 0.91064749, 0.60189053, 0.05127781],
       [0.2221478 , 0.22036852, 0.63429338, 0.18189803, 0.76279387,
        0.05604423, 0.79107208, 0.43345859, 0.47536947, 0.76418155],
       [0.57080616, 0.34556535, 0.33374685, 0.07464311, 0.47428052,
        0.61214197, 0.20785603, 0.61789965, 0.15130654, 0.09097599]])
1
a4.max()
0.0026281340151133037
1
a4.min()
0.0026281340151133037

5、创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0

1
2
a5 = np.full((10,10),fill_value=1)
a5
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, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
1
2
a5[1:-1,1:-1] = 0
a5
array([[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
a51 = np.zeros(shape=(10,10),dtype=np.int8)
a51
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int8)
1
2
a51[[0,9]] = 1
a51
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)
1
2
a51[:,[0,9]] = 1
a51
array([[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]], dtype=int8)

6、创建一个每一行都是从0到4的5*5矩阵

1
2
3
4
a6 = [0,1,2,3,4]
a61 = np.array(a6 * 5)
a61 = a61.reshape(5,5)
a61
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])

7、创建一个范围在(0,1)之间的长度为12的等差数列

1
2
a7 = np.linspace(0,1,12)
a7
array([0.        , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
       0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
       0.90909091, 1.        ])

8、创建一个长度为10的随机数组并排序

1
2
3
a8 = np.random.random(10)
a8.sort()
a8
array([0.0272533 , 0.23322018, 0.27362658, 0.48904664, 0.56287217,
       0.67636183, 0.69700736, 0.69776967, 0.82492065, 0.95932115])
1
2
3
# argsort 从小到大排序并 返回索引值
a8.argsort()

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64)

9、创建一个长度为10的随机数组并将最大值替换为0

1
2
a9 = np.random.randint(0,10,size=10)
a9
array([5, 4, 2, 3, 6, 1, 7, 8, 1, 5])
1
2
max_key = np.argmax(a9)
max_key
7
1
2
a9[max_key] = 0
a9
array([5, 4, 2, 3, 6, 1, 7, 0, 1, 5])

10、如何根据第3列来对一个5*5矩阵排序?

1
2
a10 = np.random.randint(0,100,size=(5,5))
a10
array([[43, 60, 24, 74, 55],
       [92, 83, 83, 35, 41],
       [59, 11, 53,  2, 28],
       [70, 74, 60, 17, 72],
       [19, 41, 49, 58, 60]])
1
2
a10_1 = a10[:,2]
a10_1
array([24, 83, 53, 60, 49])
1
np.argsort(a10[:,2])
array([0, 4, 2, 3, 1], dtype=int64)
1
a10[np.argsort(a10[:,2])]
array([[43, 60, 24, 74, 55],
       [19, 41, 49, 58, 60],
       [59, 11, 53,  2, 28],
       [70, 74, 60, 17, 72],
       [92, 83, 83, 35, 41]])

11、给定一个4维矩阵,如何得到最后两维的和?

1
2
a11 = np.random.randint(0,10,size=(2,3,3,3))
a11
array([[[[1, 0, 8],
         [1, 4, 0],
         [1, 9, 9]],

        [[3, 6, 1],
         [6, 0, 7],
         [9, 3, 6]],

        [[0, 5, 9],
         [3, 3, 2],
         [1, 2, 1]]],

       [[[5, 9, 5],
         [6, 2, 9],
         [3, 1, 2]],

        [[2, 7, 6],
         [7, 5, 0],
         [8, 2, 3]],

        [[6, 6, 3],
         [3, 1, 2],
         [6, 0, 9]]]])
1
2
sum11 = a11.sum(axis=(2,3))
sum11
array([[33, 41, 26],
       [42, 40, 36]])
1
2
sum11_1 = a11.sum(axis=(-1,-2))
sum11_1
array([[33, 41, 26],
       [42, 40, 36]])

12、给定数组[1, 2, 3, 4, 5],如何得到在这个数组的每个元素之间插入3个0后的新数组?

1
2
3
a0 = [1,2,3,4,5]
a00 = np.zeros(shape=(5,4),dtype=int)
a00
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
1
2
a0 = np.array(a0).reshape(5,1)
a0
array([[1],
       [2],
       [3],
       [4],
       [5]])
1
2
a12 = np.concatenate([a0,a00],axis=1)
a12
array([[1, 0, 0, 0, 0],
       [2, 0, 0, 0, 0],
       [3, 0, 0, 0, 0],
       [4, 0, 0, 0, 0],
       [5, 0, 0, 0, 0]])

13、给定一个二维矩阵,如何交换其中两行的元素?

1
2
a13 = np.random.randint(0,10,(2,3))
a13
array([[3, 0, 4],
       [3, 1, 4]])
1
a13[[1,0]]
array([[3, 1, 4],
       [3, 0, 4]])

14、创建一个100000长度的随机数组,使用两种方法对其求三次方,并比较所用时间

1
2
a14 = np.random.random(size=100000)
a14
array([0.35576335, 0.97069543, 0.14785805, ..., 0.87797416, 0.46059072,
       0.06430419])
1
%timeit np.power(a14,3)
3.7 ms ± 149 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
1
%timeit a14**3
3.76 ms ± 407 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
1
2
a14_1 = np.dot(a14,a14)
%timeit a14_2 = np.dot(a14_1,a14)
88 µs ± 13.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

15、创建一个53随机矩阵和一个32随机矩阵,求矩阵积

1
2
a15_1 = np.random.randint(0,10,size=(5,3))
a15_1
array([[4, 0, 3],
       [9, 2, 9],
       [6, 7, 6],
       [8, 1, 1],
       [5, 7, 7]])
1
2
a15_2 = np.random.randint(0,10,size=(3,2))
a15_2
array([[4, 8],
       [4, 3],
       [7, 4]])
1
2
a15 = np.dot(a15_1,a15_2)
a15
array([[ 37,  44],
       [107, 114],
       [ 94,  93],
       [ 43,  71],
       [ 97,  89]])

16、矩阵的每一行的元素都减去该行的平均值

1
2
a16 = np.random.randint(0,10,(3,3))
a16
array([[3, 6, 5],
       [4, 7, 2],
       [8, 1, 0]])
1
2
a16_1 = a16.mean(axis=1).reshape(3,1)
a16_1
array([[4.66666667],
       [4.33333333],
       [3.        ]])
1
2
a16_2 = a16 - a16_1
a16_2
array([[-1.66666667,  1.33333333,  0.33333333],
       [-0.33333333,  2.66666667, -2.33333333],
       [ 5.        , -2.        , -3.        ]])

17、打印出以下函数(要求使用np.zeros创建8*8的矩阵):
[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]

1
2
a17 = np.ones(shape=(8,8),dtype=int)
a17
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],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1]])
1
2
a17[::2,::2] = 0
a17
array([[0, 1, 0, 1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1]])
1
2
a17[1::2,1::2] = 0
a17
array([[0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0]])

18、正则化一个5*5随机矩阵
正则的概念:假设a是矩阵中的一个元素,max/min分别是矩阵元素的最大最小值,则正则化后a = (a - min)/(max - min)

1
2
a18 = np.random.randint(0,10,size=(5,5))
a18
array([[6, 0, 4, 1, 5],
       [1, 6, 0, 7, 3],
       [0, 4, 4, 7, 9],
       [9, 3, 2, 3, 6],
       [8, 8, 7, 9, 1]])
1
2
amax,amin = a18.max(),a18.min()
amax,amin
(9, 0)
1
2
a18 = (a18 - amin)/(amax - amin)
a18
array([[0.66666667, 0.        , 0.44444444, 0.11111111, 0.55555556],
       [0.11111111, 0.66666667, 0.        , 0.77777778, 0.33333333],
       [0.        , 0.44444444, 0.44444444, 0.77777778, 1.        ],
       [1.        , 0.33333333, 0.22222222, 0.33333333, 0.66666667],
       [0.88888889, 0.88888889, 0.77777778, 1.        , 0.11111111]])

19、将一个一维数组转化为二进制表示矩阵。例如
[1,2,3]
转化为
[[0,0,1],
[0,1,0],
[0,1,1]]

1
2
a19 = np.array([1,2,3])
a19
array([1, 2, 3])
1
2
a19_1 = a19.reshape(-1,1)
a19_1
array([[1],
       [2],
       [3]])
1
2
a19_2 = 2**np.arange(3)
a19_2
array([1, 2, 4], dtype=int32)
1
2
a19_3 = a19_1 & a19_2
a19_3
array([[1, 0, 0],
       [0, 2, 0],
       [1, 2, 0]], dtype=int32)
1
a19_3 != 0
array([[ True, False, False],
       [False,  True, False],
       [ True,  True, False]])
1
2
a19_3[a19_3 != 0] = 1
a19_3
array([[1, 0, 0],
       [0, 1, 0],
       [1, 1, 0]], dtype=int32)
1
a19_3[:,::-1]
array([[0, 0, 1],
       [0, 1, 0],
       [0, 1, 1]], dtype=int32)

20、实现冒泡排序法

1
2
3
4
5
6
7
8
9
a20 = [30,12,22,14,15,16,90,32,45,60]

for i in range(len(a20)-1):
for j in range(len(a20)-i-1):
if a20[j] > a20[j+1]:
a20[j],a20[j+1] = a20[j+1],a20[j]

a20

[12, 14, 15, 16, 22, 30, 32, 45, 60, 90]

numpy 的20道练习题

http://fahsa.cn/python/task-numpy/

作者

Fahsa

发布于

2021-01-06

更新于

2021-01-06

许可协议

评论