我爱编程

3-4 创建numpy数组及矩阵

2018-06-21  本文已影响0人  SRFHolmes

其他创建numpy.array的方法

np.zeros(10)
#十个元素的零数组或十阶零矩阵

np.zeros(10).dtype
#dtype('float64')默认浮点型

np.zeros(shape=(3,5),dtype=int)
#3*5矩阵 整型

np.ones(shape=(3,5),dtype=int) 
#全1矩阵

np.full(shape=(3,5),fill_value=666)
#3*5 全为666 整型 

arange

[i for i in range(0,20,2)]
#2为步长(2为间隔) 步长不能为浮点型 不包括20

np.arange(0,20,0.2)
#步长可为浮点型

linspace

np.linspace(0,20,10)
#等长的分出十个点 包括20

random

 np.random.randint(0,10)
#0-9随机数

 np.random.randint(0,1,10)
#10个元素的数组

 np.random.randint(4,8,size=(3,5))
#3*5矩阵  

#随机种子 生成相同矩阵
np.random.seed(6)
np.random.randint(4,8,size=(3,5))
'''
array([[6, 5, 7, 4, 6],
      [5, 7, 6, 4, 6],
      [5, 5, 7, 5, 5]])
'''

np.random.seed(6)
np.random.randint(4,8,size=(3,5))
'''
array([[6, 5, 7, 4, 6],
      [5, 7, 6, 4, 6],
      [5, 5, 7, 5, 5]])
'''

np.random.random()
#默认生成0-1间均匀分布的浮点数\

numpy.random.randn(d0, d1, …, dn)
#是从标准正态分布中返回一个或多个样本值。 
numpy.random.rand(d0, d1, …, dn)
#随机样本位于[0, 1)中

np.random.random(10)
np.random.random((3,5))

np.random.normal()
#默认方差为1  均值为0

np.random.normal(10,100)
#均值为10 方差为100

np.random.normal(0,1,(3,5))

np.random(.normal)?
help(np.random) 
#调出用法

上一篇 下一篇

猜你喜欢

热点阅读