Python Forum
How to use Converters in numpy loadtxt?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use Converters in numpy loadtxt?
#4
O tempora, o mores! Nowadays someone who is doing 'logistic regression' with numpy, matplotlib and mpl_toolkits is a beginner...

But on more serious note - did you read the documentation till end? The last example is probably something you are looking for:

>>> s = StringIO('10.01 31.25-\n19.22 64.31\n17.57- 63.94')
>>> def conv(fld):
...     return -float(fld[:-1]) if fld.endswith(b'-') else float(fld)
...
>>> np.loadtxt(s, converters={0: conv, 1: conv})
array([[ 10.01, -31.25],
       [ 19.22,  64.31],
       [-17.57,  63.94]])
You could write similar helper function to convert last column values to 0 or 1 based on condition.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: How to use Converters in numpy loadtxt? - by perfringo - Nov-10-2020, 01:54 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020