Table of Contents
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,
1 2 3 4 5 6 |
import numpy as np X = np.array([[2.66, 4.78, -8.333], [13.99999, 0.000001, 6.5482]]) X = np.asarray(X, dtype = 'int') print(X) |
Output:
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.
1 2 3 4 5 6 |
import numpy as np X = np.array([[2.66, 4.78, -8.33], [13.99999, 0.000001, 6.5482]]) X = X.astype(int) print(X) |
Output:
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,
1 2 3 4 5 |
import numpy as np X = np.array([[2.66, 4.78, -8.33], [13.99999, 0.000001, 6.5482]]) print(np.int_(X)) |
Output:
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,
1 2 3 4 5 6 |
import numpy as np X = np.array([[2.66, 4.78, -8.333], [13.99999, 0.000001, 6.5482]]) X = np.round_(X) print(X) |
Output:
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,
1 2 3 4 5 6 |
import numpy as np X = np.array([[2.66, 4.78, -8.333], [13.99999, 0.000001, 6.5482]]) X = np.rint(X) print(X) |
Output:
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,
1 2 3 4 5 6 |
import numpy as np X = np.array([[2.66, 4.78, -8.333], [13.99999, 0.000001, 6.5482]]) X = np.floor(X) print(X) |
Output:
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,
1 2 3 4 5 6 |
import numpy as np X = np.array([[2.66, 4.78, -8.333], [13.99999, 0.000001, 6.5482]]) X = np.ceil(X) print(X) |
Output:
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.
1 2 3 4 5 6 |
import numpy as np X = np.array([[2.66, 4.78, -8.333], [13.99999, 0.000001, 6.5482]]) X = np.trunc(X) print(X) |
Output:
Further reading:
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.