问题
I'm having a strange issue with the package numpy.genfromtxt. I use it to read a data file with a number of columns (available here) but these are not being unpacked even when unpack
is set to True
.
Here's a MWE
:
import numpy as np
f_data = np.genfromtxt('file.dat', dtype=None, unpack=True)
print f_data[3]
(237, 304.172, 2017.48, 15.982, 0.005, 0.889, 0.006, -2.567, 0.004, 1.205, 0.006)
(I use dtype=None
because the file can have strings scattered around)
As you can see it returns a line instead of an unpacked column.
If I use np.loadtxt
it works as expected:
f_data = np.loadtxt('file.dat', unpack=True)
print f_data[3]
[ 16.335 16.311 15.674 15.982 16.439 15.903 15.313 18.35 15.643 14.081 16.578 11.477]
What am I doing wrong here?
回答1:
Is this what you want?
In [448]: i=3
...: d=np.genfromtxt(fname, None) #d is a recorded array (or structured array)
...: d['f%d'%i] #Addressing Array Columns by Name
Out[448]: array([ 16.335, 16.311, 15.674, 15.982, 16.439, 15.903])
see:
http://wiki.scipy.org/Cookbook/Recarray
http://docs.scipy.org/doc/numpy/user/basics.rec.html#module-numpy.doc.structured_arrays
EDIT:
I tested d=np.genfromtxt('a.x', dtype=None, unpack=True)
on the following data:
144 a578.06 873.72 16.335 0.003
#-------^--------
180 593.41 665.748 16.311 0.003
147 868.769 908.472 15.674 0.003
237 asdf.172 2017.48 15.982 0.005
#-------^--------
with dtype=None
, unpack indeed fails:
In [538]: d=np.genfromtxt('a.x', dtype=None, unpack=True)
...: print d[3]
...: print d[1]
(237, 'asdf.172', 2017.48, 15.982, 0.005)
(180, '593.41', 665.748, 16.311, 0.003)
while with default dtype
or dtype=str
, unpack works:
In [539]: d=np.genfromtxt('a.x', unpack=True)
...: print d[3]
...: print d[1]
[ 16.335 16.311 15.674 15.982 16.439 15.903]
[ nan 593.41 868.769 nan 1039.71 385.864]
In [540]: d=np.genfromtxt('a.x', dtype=str, unpack=True)
...: print d[3]
...: print d[1]
['16.335' '16.311' '15.674' '15.982' '16.439' '15.903']
['a578.06' '593.41' '868.769' 'asdf.172' '1039.71' '385.864']
回答2:
Change the
dtype=None
to
dtype=str
And remove unpacking, as this will transpose the data. And for good practice add delimiter :)
来源:https://stackoverflow.com/questions/21937979/numpy-genfromtxt-is-not-unpacking