How to convert a float array into integer array?

I’m converting my float array into an integer array. The code that I wrote contained some errors and didn’t provide the correct output. Can you provide me with any alternative methods of doing the same? or provide me with the errors my code contains?

import numpy as np
x = x.astype('int64')

AttributeError: 'float' object has no attribute 'astype'

In the code, I’m creating a random array and converting its dtype into int64. But, It is showing me an error.

Hello @safa, the problem with your code lies in the first line (x = np.random.randn()), as it creates a single random floating-point object, and the astype() method cannot be applied to a single float. To fix this, you can create a list of numbers and pass it to the np.array() function to convert it into an array. Then, you can use the astype() method to change the data type of the array. Here is the corrected code:

This code creates an array of float32 datatype and converts it to int32 using astype(), I hope it helps!