Maria Elijah

Tired of LeetCode? ðŸ˜©

Learn the 24 patterns to solve any coding interview question without getting lost in a maze of LeetCode-style practice problems. Practice your skills in a hands-on, setup-free coding environment. ðŸ’ª

`numpy.sign()`

methodThe ** sign() method** in Pythonâ€™s

`NumPy`

library returns the sign of each number of the input array. It is done element-wise.- If an element in the array
`x`

is greater than`0`

, the method returns`1`

for that particular element. - If an element in the array
`x`

is less than`0`

, the method returns`-1`

for that particular element. - If an element in the array
`x`

is equal to`0`

, the method returns`0`

for that particular element. - For complex elements, if
`x.real!= 0`

, the`sign()`

method returns`sign(x.real) + 0j`

. Otherwise, it returns`sign(x.imag) + 0j`

.

Note: In Python, a list of lists can be used to generate a two-dimensional (2D) array.

```
numpy.sign(x,out=None, where=True)
```

: This represents the input array.`x`

: This specifies where the result is stored. This is an optional parameter.`out`

: This represents the condition in which the input gets broadcasted. This is an optional parameter.`where`

The `numpy.sign()`

method returns the sign of each number of the input array.

The following code demonstrates how to use the `numpy.sign()`

method for 2D arrays.

# Import numpyimport numpy as np# Create 2D array using np.arrayx = np.array([[-7, -3.4 , 1.2 + 1j], [2, 3 + 1j, -9.5]])# Find the sign of each element in the array# Use np.sign()result = np.sign(x)print(result)

- Line 2: We import the
`numpy`

library. - Lines 4: We create a 2D array called
`x`

consisting of complex and non-complex elements. - Line 7: We use the
`np.sign()`

method to compute the sign of each number of the`x`

input array. - Line 9: We print out the result.

RELATED TAGS

CONTRIBUTOR

Maria Elijah

Tired of LeetCode? ðŸ˜©

Learn the 24 patterns to solve any coding interview question without getting lost in a maze of LeetCode-style practice problems. Practice your skills in a hands-on, setup-free coding environment. ðŸ’ª

Keep Exploring

Related Courses