NumPy 函数 - sign(), signbit(), copysign()

2024年8月29日 | 阅读 10 分钟

大家好!我们已经知道 NumPy 包含许多非常方便的函数,用于在 Python 中执行数值运算。今天,让我们来学习一些名为 sign()、signbit() 和 copysign() 的函数。这些函数只能在 Python 中使用。在深入函数之前,让我简单介绍一下 NumPy 库或包。

Numpy

NumPy 由 Travis Oliphant 于 2005 年推出。他通过改进名为 Numarray 的旧模块或包创建了 NumPy,该包实际上被称为 Numeric Array。它主要用 C 语言编写。它有很多函数,可以使我们的工作更轻松。

随着数据科学的革命,NumPy、SciPy、Pandas 等数据分析库得到了显著扩展。Python 成为数据科学家首选的编程语言,因为它比其他语言的语法要简单得多。

NumPy 提供了一种简单有效的管理海量数据的方法。此外,NumPy 使矩阵乘法和数据操作变得极其简单。由于 NumPy 速度很快,因此非常适合处理大量数据。

以上就是关于 NumPy 库或包的内容。

现在,让我们来学习 NumPy 中的函数。

我们将使用的函数是:

  • sign()
  • signbit()
  • copysign()

Sign() 函数

sign() 是 NumPy 库中的一个函数。sign() 函数用于表示列表中或任何地方指定的数字的符号。现在,让我们来理解 sign() 函数的动态。

语法

输入参数

输入参数可以是:

  • 数组
  • 元组 (Tuples)
  • 列表
  • 整数值
  • 浮点数值
  • 双精度值

输出参数

输出参数取决于传递给 NumPy sign 参数的输入参数的类型。

输出值

如果给定值为正数且大于零,则输出值为 1。输入值是一个数字。该数字可以是整数或浮点值。

如果给定值为负数且小于零,则输出值为 -1。输入值是一个数字。该数字可以是整数或浮点值。

如果给定值为 0,则输出值为 0。零可以是 +0 / -0 / 0.0 / 任意值。

现在我们将了解 NumPy.sign 函数的工作原理。

示例 1

输出

[ 1.       -2.        5.       -9.        0.        0.        1.0598   -0.597041  0.      ]
The sign of the array arr is:
[ 1. -1.  1. -1.  0.  0.  1. -1.  0.]

示例 2

输出

Enter any Integer Number:4
The Integer Number is: 4
The sign value of 4 is:
1
The Output type of sign of num1 is <class 'numpy.int64'>
The sign value of 0 is 0
The sign value of -1439 is -1
The list taken is: [1, -2, 3, 0, 89, -6.23, 6.287, 0.04]
The Sign of list taken is:
[ 1. -1.  1.  0.  1. -1.  1.  1.]
The type of the Signed List is: <class 'numpy.ndarray'>
The Tuple taken is:(8, 2, -6, 21, 0, -0.25, 8.25)
The sign of the tuple taken is: [ 1.  1. -1.  1.  0. -1.  1.]
The type of the Signed Tuple is: <class 'numpy.ndarray'>
The Type of c1 is: <class 'complex'>
The Complex Number 1 is:(1+0j)
The Type of Complex Number 1 is:(1+0j)
The Complex Number 2 is:(-1+5j)
The Type of Complex Number 2 is:(-1+0j)
The Complex Number 3 is:(6-2j)
The Type of Complex Number 3 is:(1+0j)
The Complex Number 4 is:(-6-6j)
The Type of Complex Number 4 is:(-1+0j)
The type of sign of Complex Number: <class 'numpy.complex128'>

示例 3

输出

The dictionary taken is: {1: 1, 2: 4, 3: 9, 4: 16}
Traceback(most recent call last):
  File "main.py", line 8, in <module>
    x = np. sign(d)
TypeError: '<' not supported between instances of 'dict' and 'int'

从这个错误中,我们需要了解 NumPy 库中的 sign 函数不支持 Python 中的字典(Dictionary)和集合(Sets)。

以上就是 NumPy 库中 Sign() 函数的内容。

signbit() 函数

signbit() 是 NumPy 库中的一个函数。signbit() 函数用于表示列表中或任何地方指定的数字的负号。现在,让我们来理解 signbit() 函数的动态。

语法

输入参数

输入参数可以是:

  • 数组
  • 元组 (Tuples)
  • 列表
  • 整数值
  • 浮点数值
  • 双精度值

输出参数

输出参数取决于传递给 NumPy sign 参数的输入参数的类型。

signbit() 用于检查符号位是否已设置。

换句话说,我们可以说 signbit() 用于检查作为参数传递的数字是正数还是负数。

如果作为参数传递的数字大于或等于零,则输出为 False。

如果作为参数传递的数字小于零,则输出为 True。

这是 signbit() 函数的基本概述。

示例 1

输出

The Number taken is: 100
The statement whether signbit of the number is set or not:  False
The type of x is: <class 'numpy.bool_'>

The Number taken is: 0
The statement whether signbit of the number is set or not:  False
The type of x is: <class 'numpy.bool_'>

The Number taken is: -45100
The statement whether signbit of the number is set or not:  True
The type of x is: <class 'numpy.bool_'>

示例 2

输出

Enter any Integer Number:75
The Integer Number is: 75
The Sign Bit value of 75 is:
False
The Output type of sign of num1 is <class 'numpy.bool_'>
The Sign Bit value of 0 is 0
The Sign Bit value of -1439 is 1

The list taken is: [1, -2, 3, 0, 89, -6.23, 6.287, 0.04]
The Sign Bit Values of list taken is:
[False  True False False False  True False False]
The type of the Sign Bit List is: <class 'numpy.ndarray'>

The Tuple taken is:(8, 2, -6, 21, 0, -0.25, 8.25)
The Sign Bit Values of the tuple taken is: [False False  True False False  True False]
The type of the Sign Bit Tuple is: <class 'numpy.ndarray'>

同样,此函数也不支持字典和集合数据结构。

copysign() 函数

copysign() 是 NumPy 库中的一个函数。copysign() 函数用于表示列表中或任何地方指定的数字的负号。现在,让我们来理解 copysign() 函数的动态。

语法

输入参数

输入参数可以是:

  • 数组
  • 元组 (Tuples)
  • 列表
  • 整数值
  • 浮点数值
  • 双精度值

输出参数

输出参数取决于传递给 NumPy sign 参数的输入参数的类型。

此过程的主要功能是将值 2 的符号位交换到值 1。

请查看示例以了解 copysign() 函数的动态。

示例 1

输出

The First Integer taken is:  10
The Second Integer taken is:  -1
The Sign Bit of n1 is  False
The Sign Bit of n2 is True
The changed number after using Copy Sign is:  -10.0
The Sign Bit of Changed Number is: True
The type of Changed Number is: <class 'numpy.float64'>

The First List taken is:  [1, -2, 3, -4]
The Second List taken is:  [-5, 6, -7, 8]
The Sign Bits of l1 is  [False  True False  True]
The Sign Bits of l2 is [ True False  True False]
The changed list after using Copy Sign is:  [-1.  2. -3.  4.]
The Sign Bit of Changed List is: [ True False  True False]
The type of Changed Number is: <class 'numpy.ndarray'>

The First List taken is: (-1, 2, -3, 4)
The Second List taken is: (5, -6, 7, -8)
The Sign Bits of t1 is  [ True False  True False]
The Sign Bits of t2 is [False  True False  True]
The changed Tuple after using Copy Sign is:  [ 1. -2.  3. -4.]
The Sign Bit of Changed List is: [False  True False  True]
The type of Changed Number is: <class 'numpy.ndarray'>