numpy.trunc() en Python

El numpy.trunc() es una función matemática que devuelve el valor truncado de los elementos de la array. El tronco del escalar x es el entero i más cercano que, más cerca de cero que x. Esto simplemente significa que la parte fraccionaria del número con signo x es descartada por esta función.

Sintaxis: numpy.trunc(x[, out]) = ufunc ‘trunc’)
Parámetros:

a : [array_like] Array de entrada

Retorno:
el truncado de cada elemento, con tipo de datos flotante

Código #1: Trabajando

# Python program explaining
# trunc() function
  
import numpy as np
  
in_array = [.5, 1.5, 2.5, 3.5, 4.5, 10.1]
print ("Input array : \n", in_array)
  
truncoff_values = np.trunc(in_array)
print ("\nRounded values : \n", truncoff_values)
  
  
in_array = [.53, 1.54, .71]
print ("\nInput array : \n", in_array)
  
truncoff_values = np.trunc(in_array)
print ("\nRounded values : \n", truncoff_values)
  
in_array = [.5538, 1.33354, .71445]
print ("\nInput array : \n", in_array)
  
truncoff_values = np.trunc(in_array)
print ("\nRounded values : \n", truncoff_values)

Producción :

Input array : 
 [0.5, 1.5, 2.5, 3.5, 4.5, 10.1]

Rounded values : 
 [  0.   1.   2.   3.   4.  10.]

Input array : 
 [0.53, 1.54, 0.71]

Rounded values : 
 [ 0.  1.  0.]

Input array : 
 [0.5538, 1.33354, 0.71445]

Rounded values : 
 [ 0.  1.  0.]

Código 2: Trabajando

# Python program explaining
# trunc() function
  
import numpy as np
  
in_array = [1.67, 4.5, 7, 9, 12]
print ("Input array : \n", in_array)
  
truncoff_values = np.trunc(in_array)
print ("\nRounded values : \n", truncoff_values)
  
  
in_array = [133.000, 344.54, 437.56, 44.9, 1.2]
print ("\nInput array : \n", in_array)
  
truncoff_values = np.trunc(in_array)
print ("\nRounded values upto 2: \n", truncoff_values)

Producción :

Input array : 
 [1.67, 4.5, 7, 9, 12]

Rounded values : 
 [  1.   4.   7.   9.  12.]

Input array : 
 [133.0, 344.54, 437.56, 44.9, 1.2]

Rounded values upto 2: 
 [ 133.  344.  437.   44.    1.]

 
Referencias: https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.trunc.html#numpy.trunc
.

Publicación traducida automáticamente

Artículo escrito por Mohit Gupta_OMG 🙂 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 *