Convert float array to int array in Python

Python float array to int

What are the types of arrays?

An array is a common structure that stores multiple items of a similar type together in a single place in a contiguous memory location. We can create arrays of any one of the data types provided in Python.

Ways to convert float arrays to int in Python

In Python, the float values represent all the numbers containing a decimal point. The int data type represents integers.

This tutorial demonstrates the different ways to convert an array of floating-point numbers to an array of integers in Python.

Using the numpy.asarray() function

The numpy module has different functionalities to create and work on arrays. The asarray() function of the numpy module converts a given input into an array. The input here can be anything and ranges from lists to list of lists to ndarrays (N-dimensional arrays).

The dtype parameter holds the value of the data type of the array. It is an optional parameter and is inferred by the function by looking at the input data.

We can explicitly specify it as int to convert the float values to an integer by removing the value after the decimal point.

For example,

Output:

[[ 2 4 -8] [13 0 6]]

Using the numpy.astype() function

The astype() function copies the given array by casting it into a specific type. This method accepts five parameters, which are dtype, order, casting, subok, and copy.

We can specify the type as int to convert a float array to an integer array.

Output:

[[ 2 4 -8] [13 0 6]]

Using the numpy.int_() function

The np.int_ is a scalar numpy data type.

It can convert the given array into an array of integers by giving the original array as an argument.

For example,

Output:

[[ 2 4 -8] [13 0 6]]

Using the numpy.round_() function

The numpy module has many functions that can round decimal values in an array. The numpy.round_() function performs this and can round floating numbers to a given decimal place. By default, it rounds them to 0 decimal places.

We can use this method to round the decimal values to integers in an array.

For example,

Output:

[[ 3. 5. -8.] [14. 0. 7.]]

The methods discussed below will focus on rounding off numbers in different ways.

Using the numpy.rint() function

This function rounds the elements of an array to the nearest integer.

For example,

Output:

[[ 3. 5. -8.] [14. 0. 7.]]

Using the numpy.floor() function

The numpy.floor() function rounds the elements of the array to their floor values. The floor value is the largest integer smaller than the given value.

For example,

Output:

[[ 2. 4. -9.] [13. 0. 6.]]

Using the numpy.ceil() function

The numpy.ceil() function rounds the elements of the array to their ceil values. The ceil value is the smallest integer bigger than the given value.

For example,

Output:

[[ 3. 5. -8.] [14. 1. 7.]]

Using the numpy.trunc() function

This function discards the fractional values and returns only the integer of the given float value.

See the code below.

Output:

[[ 2. 4. -8.] [13. 0. 6.]]

Conclusion

We discuss several methods on how to convert a float array to int in Python. Some ways directly convert the array to int. The other techniques focus on rounding the values of the array to integer and removing the decimal part.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *