-1

I am using np,argmax to extract the position of the largest element in an array. I need to be able to convert this position - e.g. [x] into a string "[x]" so that I can write this to a text file.

However it either doesn't write anything to the file, throws a "TypeError: write() argument must be str, not numpy.ndarray" or throws "write() argument must be str, not bytes" error.

largest_elem_pos = np.argmax(array, axis=-1)
f = open("output.txt", "w")
f.write(largest_elem_pos)

Everything I've tried so far doesn't seem to work. I'm probably being an idiot here, any help would be appreciated!

Thanks in advance

DTR98
  • 1
  • 3

2 Answers2

0

very close --> largest_elem_pos = str(np.argmax(array, axis=-1))

  • This doesn't work, I think it is still writing np.argmax as bytes, and so not actually showing anything in the file. Thankyou though! – DTR98 Feb 05 '21 at 12:47
  • it works in my anaconda notebook. what python version/ide are you using? Did you reopen the file? what kind of values are in your array? – Bernar van Tongeren Feb 05 '21 at 13:17
  • I'm using python 3.8.5 with pycharm. I did reopen the file, but nothing showing. I should have made this more clear in the question - it's an ndarray [[ 4.1987420e-28 1.7401544e-23 7.2083184e-21 1.9580778e-29 1.0000000e+00 2.0808266e-30 8.1046953e-26 9.8910074e-30]]. – DTR98 Feb 05 '21 at 13:29
  • did you separate values with comma's inside your array? – Bernar van Tongeren Feb 05 '21 at 14:28
  • Might be something wrong with your array then, because when I use simple integers it works ;). good luck! tip: try making a simple try-out array and get that working first so you can see how it differs from your array. – Bernar van Tongeren Feb 05 '21 at 14:28
  • The values were separated yes, I think the issue was that argmax was still of type bytes even after being cast to str??. Either that or it was something to do with the dimension of my numpy array. I ended up just saving the original ndarray as text in my txt file(as suggested by user3483203 above) and then creating a new list from each line in the file where I could then cast the strings to floats again, and extract the index of the max element from that new list. I'm terrible at explaining things so I will post the solution with a better explanation as soon as I can. Thanks for all your help! – DTR98 Feb 05 '21 at 14:43
0

It sounds like you may be running into trouble because the array is larger than 1D, so largest_elem_pos is an array. If so, you could use

np.savetxt('output.txt',largest_elem_pos)

Or, if you need other things in this file and need to handle read/write yourself, you could use

f.write(largest_elem_pos.tostring().decode())

as .tostring() actually also creates a bytestring, so you need .decode() to convert to a string.

However, I also wasn't able to reproduce the bytes issue, even using an (8,1)-shaped array. In this case, I get a TypeError: write() argument must be str, not numpy.ndarray, the second example you gave.

Tyler
  • 43
  • 9