问题
Im new with python and I'm triying to load .arff file with python this is what i tried:
import arff , numpy as np
file1 = open('/Users/user/Desktop/example.arff')
dataset = arff.load(file1)
print dataset
data = np.array(dataset.data)
print data
The problem is the following output:
data = np.array(dataset.data)
AttributeError: 'generator' object has no attribute 'data'
Why is this happening? and how should i avoid it?. This is the .arff:
@relation foo
@attribute width numeric
@attribute height numeric
@attribute color {red,green,blue,yellow,black}
@data
5.0,3.25,blue
4.5,3.75,green
3.0,4.00,red
回答1:
There are two arff's that can be installed, you probably need to install liac-arff, you currently have arff
installed which returns a generator from arff.load
.
file1 = open('example.arff', "rb")
dataset = arff.load(file1)
print(dataset)
{u'attributes': [(u'width', u'NUMERIC'), (u'height', u'NUMERIC'), (u'color', [u'red', u'green', u'blue', u'yellow', u'black'])], u'relation': u'foo', u'description': u'', u'data': [[5.0, 3.25, u'blue'], [4.5, 3.75, u'green'], [3.0, 4.0, u'red']]}
For the arff you have installed don's pass a file object just load the file directly:
dataset = arff.load('eg.arff')
for row in dataset:
x = row.color
print(x)
blue
green
red
回答2:
The pypi
page for arff
shows how to use its load
https://pypi.python.org/pypi/arff/0.9
>>> import arff
>>> for row in arff.load('example.arff'):
... print(row.hair_color)
... print(row[-1])
...
>>> print(list(arff.load('example.arff')))
[[Row(hair_color='blonde', age=17.2, patno=1),
Row(hair_color='blue', age=27.2, patno=2),
Row(hair_color='blue', age=18.2, patno=3)]
Since arff.load
is a Python generator
, it does not load the file immediately. Rather you have to call it 'iteratively', as in the:
for row in arff.load(...)
wrapping it in list()
has the same effect - calling the load
repeatedly until it is done.
回答3:
As of python 3 it seems like the list(arff.load('...')) method doesn't return attributes with the arff module (0.9) instead use Row._data (private but when it sticks, push):
for row in list(arff.load(fid)):
print( row._data )
http://pydoc.net/Python/arff/0.9/arff/
来源:https://stackoverflow.com/questions/27338678/generator-object-has-no-attribute-data-problems-loading-some-file-with-scipy