Write a NumPy program to compute an element-wise indication of the sign for all elements in a given array
- برمجة بايثون
- 2021-09-13
- mhanasmh00489829403
الأجوبة
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]
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال