在 NumPy 中返回给定形状的新数组,但不初始化条目


要返回给定形状和类型的新数组,而不初始化条目,请在 Python NumPy 中使用 **numpy.empty()** 方法。dtype 是数组所需的输出数据类型,例如 numpy.int8。默认值为 numpy.float64。order 建议是否以行主序(C 样式)或列主序(Fortran 样式)在内存中存储多维数据。

函数 empty() 返回一个给定形状、dtype 和 order 的未初始化(任意)数据的数组。对象数组将初始化为 None。

NumPy 提供了全面的数学函数、随机数生成器、线性代数例程、傅里叶变换等等。它支持各种硬件和计算平台,并且与分布式、GPU 和稀疏数组库配合良好。

步骤

首先,导入所需的库 -

import numpy as np

要返回给定形状和类型的新数组,而不初始化条目,请在 Python NumPy 中使用 numpy.empty() 方法 -

arr = np.empty([3, 3, 3])

显示数组 -

print("Array...
",arr)

获取数组的类型 -

print("
Array type...
", arr.dtype)

获取数组的维度 -

print("
Our Array Dimensions...
", arr.ndim)

获取数组中的元素数量 -

print("
Number of elements...
", arr.size)

示例

import numpy as np

# To return a new array of given shape and type, without initializing entries, use the numpy.empty() method in Python Numpy
arr = np.empty([3, 3, 3])

# Display the array
print("Array...
",arr) # Get the type of the array print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimensions...
", arr.ndim) # Get the number of elements in the Array print("
Number of elements...
", arr.size)

输出

Array...
[[[4.64759419e-310 0.00000000e+000 6.90799318e-310]
[6.90799318e-310 6.90799319e-310 6.90799319e-310]
[6.90799152e-310 6.90785783e-310 6.90799318e-310]]

[[6.90799165e-310 6.90785780e-310 6.90785780e-310]
[6.90799317e-310 6.90799317e-310 6.90785780e-310]
[6.90799318e-310 6.90799314e-310 6.90799320e-310]]

[[6.90785780e-310 6.90799318e-310 6.90785780e-310]
[6.90799320e-310 6.90799317e-310 6.90785780e-310]
[6.90799320e-310 6.90799318e-310 1.10670705e-321]]]

Array type...
float64

Our Array Dimensions...
3

Number of elements...
27

更新于: 2022年2月10日

116 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.