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]]

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐