¿Cómo calcular el valor negativo numérico para todos los elementos en una array NumPy dada?

En este artículo, veremos cómo calcular el valor negativo de todos los elementos en una array NumPy determinada. Entonces, el valor negativo es en realidad el número que, cuando se suma a cualquier número, se convierte en 0. 

Ejemplo: 

Si tomamos un número como 4, entonces -4 es su número negativo porque cuando sumamos -4 a 4 obtenemos la suma como 0. Ahora tomemos otro ejemplo, supongamos que tomamos un número -6 Ahora, cuando le sumamos +6 entonces la suma se vuelve cero. por lo tanto, +6 es el valor negativo de -6. Ahora supongamos que tenemos una array de números: 
 

A = [1,2,3,-1,-2,-3,0]
So, the negative value of A is 
A'=[-1,-2,-3,1,2,3,0].

Entonces, para encontrar el valor numérico negativo de un elemento, debemos usar la función numpy.negative() de la biblioteca NumPy.

Sintaxis: numpy.negative(arr, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘negative’ )

Retorno: [ndarray o escalar] Array o escalar devuelta = -(entrada arr o escalar)

Ahora, veamos los ejemplos:

Ejemplo 1:

Python3

# importing library
import numpy as np
 
# creating a array
x = np.array([-1, -2, -3,
              1, 2, 3, 0])
 
print("Printing the Original array:",
      x)
 
# converting array elements to
# its corresponding negative value
r1 = np.negative(x)
 
print("Printing the negative value of the given array:",
      r1)

 Producción:

Printing the Original array: [-1 -2 -3  1  2  3  0] 
Printing the negative value of the given array: [ 1  2  3 -1 -2 -3  0] 
 

Ejemplo 2:

Python3

# importing library
import numpy as np
 
# creating a numpy 2D array
x = np.array([[1, 2],
              [2, 3]])
 
print("Printing the Original array Content:\n",
      x)
 
# converting array elements to
# its corresponding negative value
r1 = np.negative(x)
 
print("Printing the negative value of the given array:\n",
      r1)

 
Producción: 

Printing the Original array Content:
[[1 2]
[2 3]]
Printing the negative value of the given array:
[[-1 -2]
[-2 -3]]

Publicación traducida automáticamente

Artículo escrito por hupphurr y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *