numpy.empty用法详解

numpy.empty(shape, dtype=float, order='C', *, like=None),用来输出一个空数组

like:默认为numpy数组,可以根据你的需要更改数组类型

order:在内存中是按行主(C样式)顺序还是按列主(Fortran样式)顺序存储多维数据。

shape:可以是int或是元组,也可以是列表

如:(3,4)or 3 or [3,4]

sample = np.empty([5,4])

结果:[[6.23042070e-307 7.56587584e-307 1.37961302e-306 6.23053614e-307]
            [6.23053954e-307 9.34609790e-307 8.45593934e-307 9.34600963e-307]
            [7.56601165e-307 1.69118787e-306 9.34609790e-307 1.33511562e-306]
            [6.89805151e-307 7.56592338e-307 6.89807188e-307 1.37961302e-306]
            [1.78019082e-306 2.22522597e-306 1.33511969e-306 8.34426888e-308]]

sample = np.empty([5,4],int)

结果:[[4128860 6029375 3801158 6357084]
            [6357102 7274595 6553710 6029409]
            [6881388 6029410 7209061 7274595]

            [6881380 6750318 6029427 6881378]
            [3473511 7340078     121 7536737]]

默认输出是浮点数,可以更改输出数据类型可能会有个疑问:为什么输出的不是一整个空数组呢?

文档原话: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.

说白了,一方面为了运行更快,另一方面我要是全设为0,你回头发现数字不对怕你查不出是哪里有问题

尽管他看上去不是一个空数组但是本质还是一个空数组,你直接在拿一个你需要的数组再扔进去就行

sample = np.empty([5,4],int)
np.random.seed(0)
sample = np.random.randint(0,20,(5,4))
print(sample)

结果:

[[12 15  0  3]
 [ 3  7  9 19]
 [18  4  6 12]
 [ 1  6  7 14]
 [17  5 13  8]]

  • 15
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值