Javatpoint Logo
Javatpoint Logo

NumPy Functions - sign(),signbit(), copysign()

Hi! Everyone. We already know that numpy contains many functions which are very useful for performing operations on numbers in python. Today, let us learn about a few operations named sign(), signbit(), copysign() functions. These functions can only be applied in python only. Before, going into the functions let me brief a little about numpy library or also known as package.

Numpy

Numpy was introduced by Travis Oliphant in 2005. He created by improvising the old module or package named Numarray which was actually called as Numeric Array. It is mostly written in C Language. It has many functions which makes our work easy.

Data analysis libraries like NumPy, SciPy, Pandas, and others have expanded significantly as a result of the data science revolution. Python is the programming language of choice for data scientists because it has a syntax that is significantly simpler than those of other languages.

An easy and effective technique to manage the enormous amount of data is provided by NumPy. Additionally, NumPy makes matrix multiplication and data manipulation incredibly simple. It is appropriate to deal with a huge number of data since NumPy is quick.

This is all about Numpy Library or Package.

Now, let us learn about the functions in Numpy.

The functions which we are going to use are:

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

Sign() Function

The sign() is a function which is present in the Numpy Library. The sign() function is used to denote the sign of the number specified in the list or anywhere. Now, let us understand the dynamics of sign() functions.

Syntax

Input Parameters

The Input Parameters can be:

  • Arrays
  • Tuples
  • List
  • Integer Values
  • Float Values
  • Double Values

Output Parameters

The Output Parameters depends on the type of Input Parameters given to Numpy. sign parameters.

Output Values

The Output value is 1. if the given value is a positive number and also greater than zero and the input value is a Number. The Number can be an Integer or Floating Point Value

The Output value is -1. if the given value is a negative number and also less than zero and the input value is a Number. The Number can be an Integer or Floating Point Value

The Output value is 0 if the given value is 0. The zero may be +0 / -0 / 0.0 / anything.

We will understand now how the Numpy. sign function is working

Example 1

Output:

[ 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.]

Example 2

Output:

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'>

Example 3

Output:

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'

From this error occurred, we need to understand that sign function in NumPy Library does not support Dictionary and Sets in Python.

This is all about Sign() function in NumPy Library.

signbit() Function

The signbit() is a function which is present in the Numpy Library. The signbit() function is used to denote the negative sign of the number specified in the list or anywhere. Now, let us understand the dynamics of signbit() functions.

Syntax

Input Parameters

The Input Parameters can be:

  • Arrays
  • Tuples
  • List
  • Integer Values
  • Float Values
  • Double Values

Output Parameters

The Output Parameters depends on the type of Input Parameters given to Numpy. sign parameters.

The signbit() is used to find whether the signbit is set or not.

In other words we can say that, signbit() is used to find whether the parameter passed in positive or negative

If the number passed in as a parameter is greater than or equal to zero, then the output delivered is False

If the number passed in as a parameter is less than, then the output delivered is True.

This is the basic overview of the signbit() function.

Example 1

Output:

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_'>

Example 2

Output:

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'>

Even, this function also does not support Dictionary and Set Data Structures.

copysign() Function

The copysign() is a function which is present in the Numpy Library. The copysign() function is used to denote the negative sign of the number specified in the list or anywhere. Now, let us understand the dynamics of copysign() functions.

Syntax

Input Parameters

The Input Parameters can be:

  • Arrays
  • Tuples
  • List
  • Integer Values
  • Float Values
  • Double Values

Output Parameters

The Output Parameters depends on the type of Input Parameters given to Numpy. sign parameters.

The Primary function of this procedure is to exchange the signbits of value 2 to value 1.

Check out for examples to understand the dynamics of the copysign() function

Example 1

Output:

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'>






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA