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
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
numpy.empty是一个函数,用于创建指定形状和数据类型的空数组。它返回一个新的数组,该数组的元素值是未初始化的。可以通过指定形状、数据类型和数组的存储顺序来调用numpy.empty函数。默认情况下,数据类型为浮点型,存储顺序为'C'。 下面是使用numpy.empty函数的几个示例: 1. 生成一个2行3列的数组,默认数据类型为浮点型: ``` python import numpy as np np.empty((2,3)) ``` 输出:`array([[0., 0., 0.], [0., 0., 0.]])` 2. 生成一个2行3列的整数型数组: ``` python import numpy as np np.empty((2,3), dtype=int) ``` 输出:`array([[2000299360, 0, 0], [0, 0, 0]])`(具体数值可能会有所不同) numpy.empty函数可以用于创建指定形状和数据类型的空数组,但是这些数组的元素值是未初始化的,即随机值。如果需要创建指定元素值的数组,可以考虑使用其他函数,如numpy.zeros或numpy.ones。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [numpy.empty用法详解](https://blog.csdn.net/SaintTsy/article/details/122596417)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [python中numpy.empty函数](https://blog.csdn.net/csdn1e/article/details/110524870)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [python numpy的常用aip方法及操作!免费开源!!!!](https://download.csdn.net/download/weixin_38616435/14855031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值