在 NumPy 中返回一個新的三維陣列,不初始化條目,並將資料以列主序儲存


要返回一個新的三維陣列,而不初始化條目,請在 Python NumPy 中使用 **numpy.empty()** 方法。第一個引數是空陣列的形狀。使用“order”引數更改順序。我們將 order 設定為“F”,即 Fortran 樣式,這意味著將資料以列主序儲存在記憶體中。

dtype 是陣列所需的輸出資料型別,例如 numpy.int8。預設值為 numpy.float64。order 表示是否以行主序(C 樣式)或列主序(Fortran 樣式)順序將多維資料儲存在記憶體中。

函式 empty() 返回一個具有給定形狀、dtype 和順序的未初始化(任意)資料的陣列。物件陣列將初始化為 None。

步驟

首先,匯入所需的庫 -

import numpy as np

使用 Python NumPy 中的 numpy.empty() 方法返回一個新的三維陣列,而不初始化條目。使用“order”引數更改順序 -

arr = np.empty([3, 3, 3], order = 'F')

顯示陣列 -

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 Three-Dimensional array, without initializing entries, use the numpy.empty() method in Python Numpy
# The 1st parameter is the Shape of the empty array
# The order is changed using the "order" parameter
# We have set the order to "F" i.e. Fortran-style, that means to store the data in column-major order in memory
arr = np.empty([3, 3, 3], order = 'F')

# 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.64748890e-310 6.91009533e-310 6.90996148e-310]
[6.91009686e-310 6.91009685e-310 6.91009688e-310]
[6.91009520e-310 6.91009686e-310 6.91009688e-310]]

[[0.00000000e+000 6.90996148e-310 6.91009686e-310]
[6.91009687e-310 6.91009685e-310 6.91009685e-310]
[6.90996151e-310 6.91009682e-310 6.91009686e-310]]

[[6.91009686e-310 6.90996148e-310 6.90996148e-310]
[6.91009687e-310 6.90996148e-310 6.90996148e-310]
[6.91009686e-310 6.91009688e-310 1.10670705e-321]]]

Array type...
float64

Our Array Dimensions...
3

Number of elements...
27

更新於: 2022年2月10日

93 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.