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.


import_contacts

You may also like:

Related Posts

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Python append to array
    30 December

    How to Append to Array in Python

    Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusion In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how we can append […]

  • Python array size
    17 December

    Python array size: get size of array in Python

    Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusion In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. In Python, we […]

  • 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 […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.