NumPy sign()

numpy.sign() 方法用于确定数组中每个元素的符号。

示例

import numpy as np

# create an array
array1 = np.array([9, -3, -8, 0])

# determine the sign of each element in the array result = np.sign(array1)
print(result) # Output:[1 -1 -1 0]

sign() 语法

numpy.sign() 方法的语法如下:

numpy.sign(x, out = None, where = True, casting = 'same_kind', dtype = None)

sign() 参数

numpy.sign() 方法接受以下参数:

  • x - 需要确定元素符号的数组
  • out (可选) - 用于存储结果的输出数组
  • where (可选) - 一个布尔数组或条件,用于指定要更新哪些元素
  • casting (可选) - 转换数据类型时的转换行为
  • dtype (可选) - 返回输出的数据类型

sign() 返回值

numpy.sign() 方法返回一个与输入数组形状相同的数组,其中包含每个数组元素的符号。


示例 1:确定数组中每个元素的符号

import numpy as np

# create an input array
array1 = np.array([-2, 5, 0, -7])

# determine the sign of each element in the array result = np.sign(array1)
print(result)

输出

[-1  1  0 -1]

在上面的示例中,我们展示了如何使用 numpy.sign() 来确定数组中每个元素的符号。

结果数组包含负值的 -1,零值的 0,以及正值的 1


示例 2:sign() 中 out 和 where 的用法

import numpy as np

array1 = np.array([[-2, 0, 3], [5, -4, 0]])

# create an output array with the same shape as x
output = np.zeros_like(array1)  

# compute the sign of each element # where the element is non-zero, # and store the result in the output array np.sign(array1, where = array1 != 0, out = output)
print(output)

输出

[[-1  0  1]
 [ 1 -1  0]]

请注意上面程序中的以下代码:

np.sign(array1, where = array1 != 0, out = output)

此代码计算 array1 中每个元素的符号,但仅在元素非零的情况下(基于 where 条件)。

结果存储在 output 数组中。


示例 3:使用 sign() 中的可选 casting 参数

casting 参数指定了在转换数据类型时的转换行为。

casting 可以是:

  • 'no' - 完全不允许数据类型转换
  • 'equiv' - 只允许字节顺序的更改
  • 'safe' - 只允许能保留值精度的转换
  • 'same_kind' - 只允许安全转换或相同种类内的转换
  • 'unsafe' - 可以进行任何数据转换

让我们看一个例子。

import numpy as np

# array of floating-point numbers
array1 = np.array([1, -2, 0, 4, -5], dtype = np.float32)

# no casting is allowed # same data type as array1 is maintained array2 = np.sign(array1, casting = 'no')
# casting is allowed to equivalent # data types (floating-point numbers) array3 = np.sign(array1, casting = 'equiv')
# casting is allowed to safe data types that # preserve precision (floating-point numbers) array4 = np.sign(array1, casting = 'safe')
# casting is allowed to data types of # the same kind (floating-point numbers) array5 = np.sign(array1, casting = 'same_kind')
# casting is allowed to any data type # without checks (signed integers) array6 = np.sign(array1, casting = 'unsafe')
print("Array with 'no' casting:", array2) print("Array with 'equiv' casting:", array3) print("Array with 'safe' casting:", array4) print("Array with 'same_kind' casting:", array5) print("Array with 'unsafe' casting:", array6)

输出

Array with 'no' casting: [ 1. -1.  0.  1. -1.]
Array with 'equiv' casting: [ 1. -1.  0.  1. -1.]
Array with 'safe' casting: [ 1. -1.  0.  1. -1.]
Array with 'same_kind' casting: [ 1. -1.  0.  1. -1.]
Array with 'unsafe' casting: [ 1. -1.  0.   1. -1.]

示例 4:dtype 参数在 sign() 中的用法

import numpy as np

# original array of integers
array1 = np.array([1, -2, 0, 4, -5])

# compute the sign and specify the data type as float32 result = np.sign(array1, dtype = np.float32)
print(result)

输出

[ 1. -1.  0.  1. -1.]

在此示例中,原始数组 array1 包含整数,但通过指定 dtype = np.float32np.sign() 方法将结果转换为 np.float32

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战