numpy100道练习题
原题目可以在此处找到,链接:https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises.md
本文只摘选了一部分习题进行讲解,题号和原题号对应。
- 创建一个三乘三乘三的随机矩阵/Create a 3x3x3 array with random values (★☆☆)
很多场景需要随机值,用来测定方法或者模拟实际。numpy.random模块给出了这样功能。
import numpy as np
Z = np.random.random((3,3,3))
print(Z)
- Create a 10x10 array with random values and find the minimum and maximum values (★☆☆) 创建10*10的随机值矩阵,并找出最大值和最小值
import numpy as np
Z = np.random.random((10,10))
Zmin, Zmax = Z.min(), Z.max()
print(Zmin, Zmax)
- Create a random vector of size 30 and find the mean value (★☆☆)创建一个长度为30的矢量,并找出均值
import numpy as np
Z = np.random.random(30)
m = Z.mean()
print(m)