Working with numpy.trunc() function (7 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

In this tutorial, we’ll explore the numpy.trunc() function in detail, showcasing its versatility through 7 practical examples. Numpy, or Numerical Python, is a library essential for scientific computing in Python, providing support for large, multi-dimensional arrays and matrices. The trunc() function, a part of this rich library, is used to truncate the decimal part of the input, effectively returning the integer portion of a number.

Getting Started

Before diving into the examples, ensure that you have Numpy installed in your environment:

pip install numpy

Now, import Numpy in your script to proceed:

import numpy as np

Examples

1. Basic Usage of trunc()

To demonstrate the basic functionality, consider the following example where trunc() is used on a single number:

import numpy as np

result = np.trunc(8.9)
print(result)

Output:

8.0

2. Applying trunc() on a Numpy Array

Next, let’s see how trunc() works on an entire numpy array:

import numpy as np

arr = np.array([3.567, 4.5, -1.124, 9.9999])
result = np.trunc(arr)
print(result)

Output:

[ 3. 4. -1. 9.]

3. Working with Multi-dimensional Arrays

The trunc() function is not limited to one-dimensional arrays; it efficiently handles multi-dimensional data as well:

import numpy as np

matrix = np.array([[4.567, -5.1234], [9.234, -2.3456]])
result = np.trunc(matrix)
print(result)

Output:

[[ 4. -5.]
 [ 9. -2.]]

4. Combining trunc() with Mathematical Operations

We can also use trunc() as part of a more complex mathematical operation. Below is an example where we use trunc() after performing an addition of arrays:

import numpy as np

a = np.array([1.5, 2.5, 3.5])
b = np.array([1.2, 3.5, 5.2])
result = np.trunc(a + b)
print(result)

Output:

[ 2. 6. 8.]

5. Using trunc() in Financial Calculations

Truncation can be particularly useful in financial scenarios where you might need to discard the decimal points and focus on whole numbers. The following example simulates calculating the total amount in dollars (ignoring cents) after adding two transactions:

import numpy as np

transaction1 = np.array([235.99, 89.25])
transaction2 = np.array([100.75, 300.12])
result = np.trunc(transaction1 + transaction2)
print(result)

Output:

[336. 389.]

6. Integrating trunc() with other Numpy Functions

This example demonstrates how trunc() can be used in conjunction with other numpy functionalities. Here, we employ trunc() alongside np.mean() to find the average and then truncate it:

import numpy as np

arr = np.array([2.95, 3.55, 4.12, 5.76])
mean_result = np.mean(arr)
truncated_mean = np.trunc(mean_result)
print("Original Mean: ", mean_result, "\nTruncated Mean: ", truncated_mean)

Output:

Original Mean:  4.095000000000001 
Truncated Mean:  4.0

7. Advanced Usage: trunc() with Data Processing

In more advanced scenarios, trunc() can play a critical role in data processing tasks. For instance, it can be used to truncate figures in a dataset before further analysis or visualization. Below is an example of preprocessing data prior to calculations:

import numpy as np

# Example data representing temperatures over a week
weekly_temps = np.array([23.56, 24.67, 22.45, 25.23, 23.45, 26.67, 27.89])
# Truncate temperatures to simplify further computations
truncated_temps = np.trunc(weekly_temps)
print(truncated_temps)

Output:

[23. 24. 22. 25. 23. 26. 27.]

Conclusion

Throughout these examples, we’ve seen the flexibility of the numpy.trunc() function in handling various types of numerical data. Whether you’re working with singular values, arrays, or engage in more complex data processing, trunc() proves to be an invaluable tool for truncating numbers and simplifying data. Embrace numpy.trunc() in your next project to streamline computations and achieve clearer, more concise results.