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?

4
  • 1
    You can supply the dtype of your choice - np.sum(npW_round_111, axis=1,dtype=np.int64) Commented Sep 20, 2021 at 5:54
  • I think you can use the astype(int) - stackoverflow.com/questions/10873824/… Commented Sep 20, 2021 at 5: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". Commented Sep 20, 2021 at 6: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. Commented Sep 20, 2021 at 6:03

3 Answers 3

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])
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

doesn't work...
May i know what error did you get?
The file1.txt contains values with decimal points. E.g. 6.000000000000000000e+00 6.000000000000000000e+00, etc.
i have added the full code. do tell me if it didnt work
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])

3 Comments

doesn't work...
What is the error?
The file1.txt contains values with decimal points. E.g. 6.000000000000000000e+00 6.000000000000000000e+00, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.