创建ndarray
In [11]: np.array([6,3,1,33])
Out[11]: array([ 6, 3, 1, 33])
In [12]: data=[[1,2,3,4],[23,4,5,6]]
In [13]: np.array(data)
Out[13]:
array([[ 1, 2, 3, 4],
[23, 4, 5, 6]])
In [14]: x2 = np.arange(0, 12).reshape(4, 3)
In [15]: x2
Out[15]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In [16]: np.random.randint(0,10,3) #返回随机整数,范围区间为[low,high),包含low,不包含high
Out[16]: array([5, 0, 4])
In [17]: np.random.rand(3,4) #rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1
Out[17]:
array([[0.89143681, 0.10993371, 0.37815754, 0.11943034],
[0.28362654, 0.84848725, 0.93038445, 0.65963801],
[0.14620899, 0.18424 , 0.37719692, 0.39323956]])
In [18]: np.random.randn(3,4) #randn函数返回一个或一组样本,具有标准正态分布。
Out[18]:
array([[ 1.98716137, -1.27115759, -0.14016885, 0.28118953],
[-0.37525637, -0.77492992, 0.94574926, -0.61111582],
[-0.60284583, -0.89058114, -0.95894492, 1.9641937 ]])
In [19]: np.random.random(10) (-1~1)之间的数
Out[19]:
array([0.86132014, 0.85226162, 0.69632745, 0.38047287, 0.3608202 ,
0.97399465, 0.56642493, 0.67933993, 0.49214488, 0.89506708])