0

I have the following source code:

npW_x = np.array(my_np_array)
npW_round_111 = np.around(npW_x, decimals=0)
sum_x_111 = np.sum(npW_round_111, axis=1)

np.savetxt("file1.txt", sum_x_111)

File output

3.200000000000000000e+01
5.500000000000000000e+01
3.300000000000000000e+01
4.900000000000000000e+01
5.200000000000000000e+01
5.500000000000000000e+01
3.800000000000000000e+01
5.200000000000000000e+01
5.100000000000000000e+01
3.100000000000000000e+01
3.100000000000000000e+01
3.200000000000000000e+01
5.100000000000000000e+01
... ... ... ... ... ...

The expected output is as follows:

3
6
3
5
5
6
4
5
5
3
3
3
5
... ... ... ... ... ...

How can I do this?

user366312
  • 16,949
  • 65
  • 235
  • 452
  • 1
    You can supply the `dtype` of your choice - `np.sum(npW_round_111, axis=1,dtype=np.int64)` – Vaebhav Sep 20 '21 at 05:54
  • I think you can use the astype(int) - https://stackoverflow.com/questions/10873824/how-to-convert-2d-float-numpy-array-to-2d-int-numpy-array – nkacolz Sep 20 '21 at 05:55
  • 1
    Note, you're not going to get that "expected" output. `3.200000000000000000e+01` is 32.0, not 3.0. The `e+01` means "times 10 to the 1st power". – user2357112 Sep 20 '21 at 06:01
  • 2
    Unduping because this isn't just a dtype issue. There *is* a dtype issue, but it's not the only issue - `numpy.savetxt` defaults to `fmt='%.18e'` even if you pass it an array of integer dtype. – user2357112 Sep 20 '21 at 06:03

3 Answers3

1
In [385]: npW_x = np.random.rand(4,5)*10
     ...: npW_round_111 = np.around(npW_x, decimals=0)
     ...: sum_x_111 = np.sum(npW_round_111, axis=1)
     ...: 
In [386]: sum_x_111
Out[386]: array([38., 25., 24., 30.])

save with default fmt (Read and reread the np.savetxt docs!)

In [387]: np.savetxt('test',sum_x_111)
In [388]: cat test
3.800000000000000000e+01
2.500000000000000000e+01
2.400000000000000000e+01
3.000000000000000000e+01

save with int format:

In [389]: np.savetxt('test',sum_x_111, fmt='%10d')
In [390]: cat test
        38
        25
        24
        30

conversion in memory to int:

In [391]: In [391]: sum_x_111.astype(int)
Out[391]: array([38, 25, 24, 30])
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

You can use astype() function for this

I'm considering npW_x to be the array which needs to be converted into int array.

npW_x = np.array(my_np_array)
npW_x = npW_x.astype('int32')

Now, it should be converted into an integer numpy array.

Edit:

I have added the full code, so that you can try

npW_x = np.array(my_np_array)
npW_x = npW_x.astype('int32')
sum_x_111 = np.sum(npW_x, axis=1)
np.savetxt("file1.txt", sum_x_111)

Hope it resolved your problem.

0

arr = np.asarray([3.200000000000000000e+01,
5.500000000000000000e+01,
3.300000000000000000e+01,
4.900000000000000000e+01,
5.200000000000000000e+01,
5.500000000000000000e+01,
3.800000000000000000e+01,
5.200000000000000000e+01,
5.100000000000000000e+01,
3.100000000000000000e+01,
3.100000000000000000e+01,
3.200000000000000000e+01,
5.100000000000000000e+01])

arr

array([32. , 55. , 33. , 49. , 52. , 55. , 38. , 52. , 51. , 31. , 31. ,
       32. ,  5.1])
np.around(np.where(arr//10, arr/10, arr)).astype('int')

get

array([3, 6, 3, 5, 5, 6, 4, 5, 5, 3, 3, 3, 5])
padu
  • 689
  • 4
  • 10