Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use numpy.genfromtxt when first column is string and the remaining columns are numbers?

Tags:

python

numpy

Basically, I have a bunch of data where the first column is a string (label) and the remaining columns are numeric values. I run the following:

data = numpy.genfromtxt('data.txt', delimiter = ',') 

This reads most of the data well, but the label column just gets 'nan'. How can I deal with this?

like image 410
Jim Avatar asked Sep 07 '12 14:09

Jim


People also ask

What is delimiter in NP Genfromtxt?

The delimiter argument Quite often, a single character marks the separation between columns. For example, comma-separated files (CSV) use a comma ( , ) or a semicolon ( ; ) as delimiter: >>> data = u"1, 2, 3\n4, 5, 6" >>> np. genfromtxt(StringIO(data), delimiter=",") array([[1., 2., 3.], [4., 5., 6.]])

Which argument should be passed into Genfromtxt If you have many column names to define from the data?

The only mandatory argument of genfromtxt is the source of the data. It can be a string, a list of strings, a generator or an open file-like object with a read method, for example, a file or io.


1 Answers

By default, np.genfromtxt uses dtype=float: that's why you string columns are converted to NaNs because, after all, they're Not A Number...

You can ask np.genfromtxt to try to guess the actual type of your columns by using dtype=None:

>>> from StringIO import StringIO >>> test = "a,1,2\nb,3,4" >>> a = np.genfromtxt(StringIO(test), delimiter=",", dtype=None) >>> print a array([('a',1,2),('b',3,4)], dtype=[('f0', '|S1'),('f1', '<i8'),('f2', '<i8')]) 

You can access the columns by using their name, like a['f0']...

Using dtype=None is a good trick if you don't know what your columns should be. If you already know what type they should have, you can give an explicit dtype. For example, in our test, we know that the first column is a string, the second an int, and we want the third to be a float. We would then use

>>> np.genfromtxt(StringIO(test), delimiter=",", dtype=("|S10", int, float)) array([('a', 1, 2.0), ('b', 3, 4.0)],        dtype=[('f0', '|S10'), ('f1', '<i8'), ('f2', '<f8')]) 

Using an explicit dtype is much more efficient than using dtype=None and is the recommended way.

In both cases (dtype=None or explicit, non-homogeneous dtype), you end up with a structured array.

[Note: With dtype=None, the input is parsed a second time and the type of each column is updated to match the larger type possible: first we try a bool, then an int, then a float, then a complex, then we keep a string if all else fails. The implementation is rather clunky, actually. There had been some attempts to make the type guessing more efficient (using regexp), but nothing that stuck so far]

like image 106
Pierre GM Avatar answered Sep 18 '22 16:09

Pierre GM