Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert NumPy Array of Floats to an Array of Integers

  • 25 Jul, 2025
  • Com 0
Numpy Array of Floats to Array of Integers

Numpy astype() method is the most common and efficient way to convert a numpy array of floats to an array of integers in Python. It provides explicit control over the data type, and you can cast it to any compatible type.

Using Numpy astype() method to convert an array of floats to an array of integers

 

import numpy as np

float_array = np.array([3.14, 2.71, 1.62])

print(float_array.dtype)
# Output: float64

integer_array = float_array.astype(int)

print(integer_array)
# Output: [3 2 1]

print(integer_array.dtype)
# Output: int64

As shown in the above output, the astype(int) method truncates any decimal part and returns an array of integers. Some algorithms, like indexing or classification, require large integers instead of floating-point.

Rounding to the nearest integer

If you want to avoid truncation, you can use the np.rint() method that rounds floats to the nearest integer before conversion.

Now, you may wonder why we need to use the ndarray.astype() function after the np.rint() method, as it seems we already have an integer.

Well, np.rint() method does not really return an integer, even though it looks like a whole number, it is still a floating value. After using ndarray.astype() method, we have an array of integers.

Rounding to nearest integer before conversion

 

import numpy as np

float_array = np.array([3.14, 2.71, 1.62])

print(float_array.dtype)
# Output: float64

rounded_nearest_integer_array = np.rint(float_array).astype(int)

print(rounded_nearest_integer_array)
# Output: [3 3 2]

print(rounded_nearest_integer_array.dtype)
# Output: int64

You can see that we already rounded up to the nearest integer and then converted to type int64.

Rounding up

The np.ceil() method rounds up each element to its nearest integer and returns a float array. Then, we can convert that array of floating-point values to integer values using the .astype() method.

Rounding up before converting to an integer

 

import numpy as np

float_array = np.array([3.14, 2.71, 1.62])

print(float_array.dtype)
# Output: float64

rounded_up_integer_array = np.ceil(float_array).astype(int)

print(rounded_up_integer_array)
# Output: [4 3 2]

print(rounded_up_integer_array.dtype)
# Output: int64

Since we are using the ceiling function, 3.14 becomes 4, 2.71 becomes 3, and 1.62 becomes 2. The final array is an integer array, and we verified its type using the .dtype attribute.

Converting a 2D numpy array of floats to int

Let’s define a 2D numpy array and convert it to a 2D integer array. You don’t need to do anything extra; just use the .astype(int) method on the input numpy array.

Converting a 2D numpy array of floats to int

import numpy as np

float_arr = np.array([[11.21, 19.21],
                      [46.21, 18.21],
                      [29.21, 21.21]])
print(float_arr)

print('After converting numpy float array to int array')

int_arr = float_arr.astype(int)

print(int_arr)

print("The data type of int_array is: ")
print(int_arr.dtype)

# Output:

# [[11.21 19.21]
#  [46.21 18.21]
#  [29.21 21.21]]

# After converting numpy float array to int array

# [[11 19]
#  [46 18]
#  [29 21]]

# The data type of int_array is:

# int64

Alternate approaches

Approach 1: Using np.int_()

The np.int_ type truncates the decimal parts of floats, similar to using the .astype(int) method.

Using np.int_() to convert numpy float to integer

 

import numpy as np

float_array = np.array([[11.21, 19.21],
                        [46.21, 18.21],
                        [29.21, 21.21]])
print(float_array)

print('After converting numpy float array to int array')
int_array = np.int_(float_array)
print(int_array)

print("The data type of int_array is: ")
print(int_array.dtype)

# Output:

# [[11.21 19.21]
#  [46.21 18.21]
#  [29.21 21.21]]

# After converting numpy float array to int array

# [[11 19]
#  [46 18]
#  [29 21]]

# The data type of int_array is:
# int64

Approach 2: Using numpy.asarray() with the dtype

Pass your float array along with dtype=’int’ as arguments to the np.asarray() method.

This dtype parameter will create a NumPy array of the specified integer type.

Usign np.asarray() method

 

import numpy as np

# Create a numpy array of float numbers
float_array = np.array([3.14, 2.71, 1.62])

int_array = np.asarray(float_array, dtype=np.int32)

print(int_array)

# Output: [3 2 1]

That’s all!

Post Views: 33
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Check If an Element Exists in a Python Set
Python os.rename(): Renaming a File or Directory

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend