將NumPy陣列的值的符號更改為標量的逐元素符號
要將陣列值的符號更改為標量的逐元素符號,請在Python NumPy中使用**numpy.copysign()**方法。copysign()的第一個引數是要更改其符號的值(陣列元素)。第二個引數是要複製到第一個引數值的符號。
out是將結果儲存到的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果沒有提供或為None,則返回一個新分配的陣列。元組(只能作為關鍵字引數)的長度必須等於輸出的數量。
條件在輸入上進行廣播。在條件為True的位置,out陣列將設定為ufunc結果。在其他位置,out陣列將保留其原始值。請注意,如果透過預設的out=None建立未初始化的out陣列,則其中條件為False的位置將保持未初始化狀態。
步驟
首先,匯入所需的庫:
import numpy as np
建立一個數組:
arr = np.array([10, 87, -45, -7.9, 6.5, 89])
顯示陣列:
print("Array...
", arr)獲取陣列的型別:
print("
Our Array type...
", arr.dtype)
獲取陣列的維度:
print("
Our Array Dimensions...
",arr.ndim)獲取陣列中元素的數量:
print("
Number of elements...
", arr.size)
要將陣列值的符號更改為標量的逐元素符號,請在Python NumPy中使用numpy.copysign()方法。copysign()的第一個引數是要更改其符號的值(陣列元素)。第二個引數是要複製到第一個引數值的符號:
print("
Result...
",np.copysign(arr, -1))示例
import numpy as np
# Create an array
arr = np.array([10, 87, -45, -7.9, 6.5, 89])
# 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 Dimensions...
",arr.ndim)
# Get the number of elements in the Array
print("
Number of elements...
", arr.size)
# To change the sign of array values to that of a scalar, elementwise, use the numpy.copysign() method in Python Numpy
# The 1st parameter of the copysign() is the value (array elements) to change the sign of.
# The 2nd parameter is the sign to be copied to 1st parameter value.
print("
Result...
",np.copysign(arr, -1))輸出
Array... [ 10. 87. -45. -7.9 6.5 89. ] Our Array type... float64 Our Array Dimensions... 1 Number of elements... 6 Result... [-10. -87. -45. -7.9 -6.5 -89. ]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP