在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 3D array without initializing entries, use the numpy.empty() method in Python Numpy # The 1st parameter is the Shape of the empty array 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)
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
Array... [[[4.65419892e-310 0.00000000e+000 6.92674569e-310] [6.92674569e-310 6.92674570e-310 6.92674570e-310] [6.92674403e-310 6.92661033e-310 6.92674569e-310]] [[6.92674415e-310 6.92661031e-310 6.92661031e-310] [6.92674568e-310 6.92674568e-310 6.92661031e-310] [6.92674569e-310 6.92674565e-310 6.92674570e-310]] [[6.92661031e-310 6.92674569e-310 6.92661031e-310] [6.92674571e-310 6.92674568e-310 6.92661031e-310] [6.92674570e-310 6.92674569e-310 1.10670705e-321]]] Array type... float64 Our Array Dimensions... 3 Number of elements... 27
广告