生成器对象没有属性“数据”,问题加载了一些带有scipy的文件?

时间:2022-08-16 21:24:48

Im new with python and I'm triying to load .arff file with python this is what i tried:

我是python的新手,我正在尝试加载。arff文件和python,这就是我所尝试的:

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:

为什么会这样?我该如何避免呢?这是.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

3 个解决方案

#1


2  

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.

有两个可以安装的arff,您可能需要安装liac-arff,您现在安装了arff,它从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:

对于arff,你已经安装了don's pass一个文件对象直接加载文件:

dataset = arff.load('eg.arff')
for row in dataset:
    x = row.color
    print(x)
blue
green
red

#2


2  

The pypi page for arff shows how to use its load

arff的pypi页面展示了如何使用它的负载。

https://pypi.python.org/pypi/arff/0.9

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:

因为飞机救援消防。load是一个Python生成器,它不会立即加载文件。相反,你必须把它叫做“迭代”,如:

for row in arff.load(...)

wrapping it in list() has the same effect - calling the load repeatedly until it is done.

将其包装在list()中具有相同的效果——反复调用load直到完成。

#3


1  

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):

在python 3中,它似乎是列表(arff.load('…'))方法不返回带有arff模块(0.9)的属性,而是使用Row。_data(私有,但当它使用时,push):

for row in list(arff.load(fid)):
   print( row._data )

http://pydoc.net/Python/arff/0.9/arff/

http://pydoc.net/Python/arff/0.9/arff/

#1


2  

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.

有两个可以安装的arff,您可能需要安装liac-arff,您现在安装了arff,它从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:

对于arff,你已经安装了don's pass一个文件对象直接加载文件:

dataset = arff.load('eg.arff')
for row in dataset:
    x = row.color
    print(x)
blue
green
red

#2


2  

The pypi page for arff shows how to use its load

arff的pypi页面展示了如何使用它的负载。

https://pypi.python.org/pypi/arff/0.9

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:

因为飞机救援消防。load是一个Python生成器,它不会立即加载文件。相反,你必须把它叫做“迭代”,如:

for row in arff.load(...)

wrapping it in list() has the same effect - calling the load repeatedly until it is done.

将其包装在list()中具有相同的效果——反复调用load直到完成。

#3


1  

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):

在python 3中,它似乎是列表(arff.load('…'))方法不返回带有arff模块(0.9)的属性,而是使用Row。_data(私有,但当它使用时,push):

for row in list(arff.load(fid)):
   print( row._data )

http://pydoc.net/Python/arff/0.9/arff/

http://pydoc.net/Python/arff/0.9/arff/