Q:

Write a NumPy program to compute an element-wise indication of the sign for all elements in a given array

0

Write a NumPy program to compute an element-wise indication of the sign for all elements in a given array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
x = np.array([1, 3, 5, 0, -1, -7, 0, 5])
print("Original array;")
print(x)
r1 = np.sign(x)
r2 = np.copy(x)
r2[r2 > 0] = 1
r2[r2 < 0] = -1
assert np.array_equal(r1, r2)
print("Element-wise indication of the sign for all elements of the said array:")
print(r1)

Sample Output:

Original array;
[ 1  3  5  0 -1 -7  0  5]
Element-wise indication of the sign for all elements of the said array: 
[ 1  1  1  0 -1 -1  0  1]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now