返回一個具有給定形狀和型別的新陣列,該陣列填充了 NumPy 中的類陣列


要返回一個具有給定形狀和型別的新陣列,並用填充值填充,請在 Python NumPy 中使用 **numpy.full()** 方法。第一個引數是新陣列的形狀。第二個引數將填充值設定為類陣列。

dtype 是陣列所需的數 據型別。order 建議是將多維資料以 C 連續或 Fortran 連續(行或列方式)順序儲存在記憶體中。

NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等等。它支援各種硬體和計算平臺,並且可以很好地與分散式、GPU 和稀疏陣列庫配合使用。

步驟

首先,匯入所需的庫:

import numpy as np

要返回一個具有給定形狀和型別的新陣列,並用填充值填充,請使用 numpy.full() 方法。第二個引數將填充值設定為類陣列:

arr = np.full((2, 2), fill_value = [998, 999])

顯示陣列:

print("Array...
",arr)

獲取陣列的型別:

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

獲取陣列的維數:

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

獲取陣列的形狀:

print("
Our Array Shape...
",arr.shape)

獲取陣列的元素個數:

print("
Elements in the Array...
",arr.size)

示例

import numpy as np

# To return a new array of given shape and type, filled with a fill value, use the numpy.full() method in Python Numpy
# The 1st parameter is the shape of the new array
# The 2nd parameter sets the fill value as array-like
arr = np.full((2, 2), fill_value = [998, 999])

# Displaying our array
print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size)

輸出

Array...
[[998 999]
[998 999]]

Array datatype...
int64

Array Dimensions...
2

Our Array Shape...
(2, 2)

Elements in the Array...
4

更新於:2022年2月10日

瀏覽量 144

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.