在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)

輸出

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

更新於:2022年2月10日

2K+ 瀏覽量

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.