numpy 小示例

import numpy as np 

生成 3*4 的由  0 组成的二维数组

>>> np.zeros((3,4))

array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

生成  2*3*4   的由  1 组成的三维数组

>>>np.ones((2,3,4),dtype=np.int16)

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]]], dtype=int16)

生成 10 ~25 间的 间隔为5的数列

>>>np.arange(10,25,5)

array([10, 15, 20])

生成 0~2间的等差数列,元素个数 为 9 

>>>np.linspace(0,2,9)

array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  ])

以给定数 7 ,生成 指定形状2*2的矩阵 

>>> np.full((2,2),7)
array([[7, 7],
[7, 7]])

生成对角矩阵, 长与宽相等 ,默认中间对角线(左上到右下)全是1,其余为0

>>np.eye(2)

array([[1., 0.],
[0., 1.]])

生成数值在1以内的指定形状 2*2 的随机矩阵

>>> np.random.random((2,2))
array([[0.36248815, 0.60881839],
[0.10805521, 0.92618553]])

 

生成指定形状的空数组,数组内数据,可能是随机的

empty, unlike zeros, does not set the array values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution.

>>np.empty((3,2))

array([[1.39069238e-309, 1.39069238e-309],
[1.39069238e-309, 1.39069238e-309],
[1.39069238e-309, 1.39069238e-309]])

 

转载于:https://www.cnblogs.com/Ting-light/p/9210279.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值