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.

Related Posts

  • Convert Bytearray to String in Python
    01 November

    Convert Bytearray to String in Python

    Table of ContentsHow to create a bytearray from a string?Using the str() Function to convert Bytearray to String in PythonUsing the decode() Function to convert Bytearray to String in PythonUsing the codecs Module to convert Bytearray to String in PythonConclusion Python supports different types of sequence objects to store data. One such object is a […]

  • Inverse matrix in Python
    24 August

    Inverse matrix in python

    Table of ContentsUsing the Gauss-Jordan method to find the inverse of a given matrix in Python.Using the numpy.linalg.inv() function to find the inverse of a given matrix in Python. The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. […]

  • Convert list to matrix in Python
    21 August

    Convert list to matrix in Python

    Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

  • Rotate image Python
    20 August

    Rotate image in Python

    Table of ContentsUsing the image.rotate() function from the Pillow Imaging Library (Fork) to rotate image in Python.Using NumPy library functions to rotate image in Python.Using the OpenCV and imutils library functions to rotate image in Python.Using matplotlib and SciPy libraries to rotate image in Python. Python supports the use of images and sometimes a given […]

  • 06 April

    Print Matrix in Python

    In this article you will learn "How to print a matrix in Python?"

  • 20 November

    How to initialize array in Python

    Table of ContentsUsing for loop, range() function and append() method of listIntialize empty arrayIntialize array with default valuesIntialize array with valuesUsing list-comprehensionUsing product (*) operatorUsing empty() method of numpy module In this article, we will see a different ways to initialize an array in Python. Array is collection of elements of same type. In Python, […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.