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?


import_contacts

You may also like:

Related Posts

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63da74a921b2c685258208/] [crayon-63da74a921b34718534065/] We used numpy.empty() […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusion Fill array with random numbers […]

  • 17 May

    Add Month to datetime in Python

    Table of ContentsWays to add month to datetime in PythonUsing the datetime.datetime constructor to add month to datetime in Python.Using the dateutil.relativedelta object to add month to datetime in PythonUsing the pandas.DateOffset() function to add month to datetime in PythonUsing the arrow.shift() function to add month to datetime in PythonConclusion In Python, we can represent […]

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

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.