NumPy 中的數值正負元素逐個顯示


要顯示數值負數,請在 Python NumPy 中使用 **np.negative()** 方法。out 是將結果儲存到的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果沒有提供或為 None,則返回一個新分配的陣列。元組(只能作為關鍵字引數)的長度必須等於輸出的數量。

條件在輸入上進行廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他位置,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。

步驟

首先,匯入所需的庫:

import numpy as np

建立一個數組:

arr = np.array([-3, 6, 9, -12, -24, 30, -55])

顯示陣列:

print("Array...
", arr)

獲取陣列的型別:

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

獲取陣列的維度:

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

獲取陣列的形狀:

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

要顯示數值正數,請在 Python NumPy 中使用 np.positive() 方法:

print("
Numerical positive...
",np.positive(arr))

要顯示數值負數,請在 Python NumPy 中使用 np.negative() 方法:

print("
Numerical negative...
",np.negative(arr))

示例

import numpy as np

# Create an array
arr = np.array([-3, 6, 9, -12, -24, 30, -55])

# Display the array
print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimension...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # To display the Numerical positive, use the np.positive() method in Python Numpy print("
Numerical positive...
",np.positive(arr)) # To display the Numerical negative, use the np.negative() method in Python Numpy print("
Numerical negative...
",np.negative(arr))

輸出

Array...
[ -3 6 9 -12 -24 30 -55]

Our Array type...
int64

Our Array Dimension...
1

Our Array Shape...
(7,)

Numerical positive...
[ -3 6 9 -12 -24 30 -55]

Numerical negative...
[ 3 -6 -9 12 24 -30 55]

更新於:2022年2月7日

838 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

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