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 38 39 40 41 42 43 44 45 46 47 48 49 50
| np.zeros((3, 4)) # 用法:zeros(shape, dtype=float, order='C') # 返回:返回来一个给定形状和类型的用0填充的数组
np.ones((2,3,4),dtype=np.int16) # 依据给定形状和类型(shape[, dtype, order])返回一个新的元素全部为1的数组
d = np.arange(10, 25, 5) # arange函数用于创建等差数组 # np.arange([start, ]stop, [step, ]dtype=None) # start:可忽略不写,默认从0开始;起始值 # stop:结束值;生成的元素不包括结束值 # step:可忽略不写,默认步长为1;步长 # dtype:默认为None,设置显示元素的数据类型 nd1 = np.arange(5)#array([0, 1, 2, 3, 4]) nd2 = np.arange(1,5)#array([1, 2, 3, 4])
np.arange(n) # 类似range()函数,返回ndarray类型,元素从0到n‐1
np.linspace(0, 2, 9) # numpy.linspace(start, stop[, num=50[, endpoint=True[, retstep=False[, dtype=None]]]]]) # 返回在指定范围内的均匀间隔的数字(组成的数组),也即返回一个等差数列 # start - 起始点 # stop - 结束点 # num - 元素个数,默认为50 # endpoint - 是否包含stop数值,默认为True,包含stop值;若为False,则不包含stop值 # retstep - 返回值形式,默认为False,返回等差数列组,若为True,则返回结果(array([`samples`, `step`])), # dtype - 返回结果的数据类型,默认无,若无,则参考输入数据类型。
e = np.full((2, 2), 7) # 创建一个由常数填充的数组,第一个参数是数组的形状,第二个参数是数组中填充的常数。
f = np.eye(2) # 创建单位矩阵,函数中的参数n,则创建n * n的单位矩阵
np.ones_like(a) # 根据数组a的形状生成一个全1数组
np.zeros_like(a) # 根据数组a的形状生成一个全0数组
np.full_like(a,val) # 根据数组a的形状生成一个数组,每个元素值都是val
np.random.random((2, 2)) # 按照参数中给出的形状来创建数组,创建随机数数组
np.empty((3, 2)) # 按照参数中给出的形状来创建数组,创建一个随机元素
|