1

I have a numpy array:

array([ 6., 13., 10.,  9.,  5.,  4.,  4.,  4.,  3.,  5.,  4., 20.,  6.,
       14.,  8.,  6.,  3.,  3.,  3.,  2.,  2.,  5.,  6., 11., 11., 11.,
        9.,  3.,  3.,  3.,  2.,  3.,  2.,  3.,  6., 11., 15., 12.,  5.,
        3.,  3.,  5.,  2.,  2.,  2.,  2.,  3.,  4.,  7.,  2.,  8.,  2.,
        3.,  2.,  1.,  3.,  1.,  1.,  5.,  5.,  4.,  2.,  5.,  2.,  3.,
        2.,  2.,  2.,  1.,  2.,  4., 11.,  5.,  4.,  3.,  4.,  3.,  3.,
        3.,  4.,  3.,  4.,  2.,  6.,  6.,  3.,  3.,  2.,  3.,  4.,  2.,
        3.,  2.,  4.,  2.,  5.,  4.,  3.,  2.,  2.,  3.,  2.,  2.,  2.,
        2.,  3.,  3.], dtype=float32)

...but I want to make it contain integers instead of floats. Can you help me to solve this?

Member2017
  • 431
  • 2
  • 8
  • 16
haenphe
  • 37
  • 3

2 Answers2

1

You can Numpy's built in method, astype:

a = np.ones((3,3))
print(a.dtype) #defaults to float64

a = a.astype(int) #changes to int
ccl
  • 2,378
  • 2
  • 12
  • 26
1
import numpy as np

arr = np.array([6., 13., 10., 9., 5., 4., 4., 4., 3., 5., 4., 20., 6.,1])
int_arr = arr.astype(np.int64)
Artyom Vancyan
  • 5,029
  • 3
  • 12
  • 34