Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a only integers numpy 2D array on a txt file

I know how to write the txt file using numpy.savetxt() but I can't get it to write the file using just integers. I have the following code:

new_picks = new_picks.astype(int)
np.savetxt(fname='newPicksData.txt', X=new_picks.astype(int))

This is how the matrix that I'm getting looks like:

2.900000000000000000e+01 3.290000000000000000e+02 1.000000000000000000e+00
4.300000000000000000e+01 1.080000000000000000e+02 1.000000000000000000e+00
4.300000000000000000e+01 1.950000000000000000e+02 1.000000000000000000e+00
5.600000000000000000e+01 1.510000000000000000e+02 1.000000000000000000e+00
5.600000000000000000e+01 9.700000000000000000e+01 1.000000000000000000e+00
7.000000000000000000e+01 2.840000000000000000e+02 1.000000000000000000e+00
3.500000000000000000e+01 3.170000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 2.110000000000000000e+02 1.000000000000000000e+00
6.400000000000000000e+01 1.180000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 3.700000000000000000e+01 1.000000000000000000e+00
1.300000000000000000e+01 1.950000000000000000e+02 1.000000000000000000e+00
1.300000000000000000e+01 1.680000000000000000e+02 1.000000000000000000e+00
1.300000000000000000e+01 2.780000000000000000e+02 1.000000000000000000e+00
4.900000000000000000e+01 2.200000000000000000e+01 1.000000000000000000e+00
4.900000000000000000e+01 1.040000000000000000e+02 1.000000000000000000e+00
4.900000000000000000e+01 7.500000000000000000e+01 1.000000000000000000e+00
5.400000000000000000e+01 2.610000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 2.600000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 1.150000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 5.400000000000000000e+01 1.000000000000000000e+00
1.300000000000000000e+01 5.400000000000000000e+01 1.000000000000000000e+00
4.900000000000000000e+01 5.400000000000000000e+01 1.000000000000000000e+00

What I'm looking for is something like this:

29 329 1
43 108 1
43 195 1
56 151 1
56 97 1
like image 603
Diego Aguado Avatar asked Aug 27 '15 11:08

Diego Aguado


2 Answers

You have to add an extra parameter in

savetxt(fname='newPicksData.txt', X=new_picks.astype(int), fmt ='%.0f\n')

that is just the formating of the actual number.

like image 188
Anjum.... Avatar answered Nov 14 '22 22:11

Anjum....


You have to specify the format separate using the fmt argument, see the documentation here http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html

You have to use the following syntax to get what you want:

np.savetxt(fname='newPicksData.txt', X=new_picks.astype(int),fmt="%i")

Omitting that argument, defaults it to fmt='%.18e', which is exactly what you see in the output that you posted in the question.


Furthermore, do you really need the astype(int) part in your code? The np.savetext command can format strings/doubles perfectly fine into int without changing the data yourself. This can be demonstrated with the below code chunk:

import numpy
x = numpy.array([1.3,2.1,3.9,4.2,5.5,6.1])
numpy.savetxt(fname='newPicksData.txt', X=x, fmt="%i")

The listed code chunk then yields the following text file:

1
2
3
4
5
6
like image 42
Bas Jansen Avatar answered Nov 14 '22 22:11

Bas Jansen