Python Forum
How to use Converters in numpy loadtxt? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to use Converters in numpy loadtxt? (/thread-30855.html)



How to use Converters in numpy loadtxt? - kmasic18 - Nov-10-2020

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!


RE: How to use Converters in numpy loadtxt? - perfringo - Nov-10-2020

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.



RE: How to use Converters in numpy loadtxt? - kmasic18 - Nov-10-2020

Yes, I checked documentation but I still don't know how to convert. I am really beginner at this


RE: How to use Converters in numpy loadtxt? - perfringo - Nov-10-2020

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.