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?
#1
Hi, I have the data(example below) with floats and the last column is a string, that will be used in logistic regression assignment.

5.1 3 1.1 Iris-versicolor

5.7 4.1 1.3 Iris-versicolor

6.3 6 2.5 Iris-virginica

5.8 5.1 1.9 Iris-virginica

This is requested: "Use the numpy loadtxt() function, along with the 'converters' argument to change the labels from strings to the floats 0 and 1. Give the argument encoding=“utf8”, otherwise your converter function will need to additionally convert from bytes to strings."

Only packages I can use are:
numpy
matplotlib.pyplot
from mpl_toolkits.mplot3d import Axes3D

This is given as a template:

converter = {}

data = np.loadtxt(path, , conv=converter)

Can someone help how to use this "converters"? Thank you!
Reply
#2
There is built-in help (help(np.loadtxt)) as well as online documentation for numpy.loadtxt.


Output:
converters : dict, optional A dictionary mapping column number to a function that will parse the column string into the desired value. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data (but see also `genfromtxt`): ``converters = {3: lambda s: float(s.strip() or 0)}``. Default: None.
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
#3
Yes, I checked documentation but I still don't know how to convert. I am really beginner at this
Reply
#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


Forum Jump:

User Panel Messages

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