生成器"对象没有属性“数据",使用scipy加载某些文件时遇到问题? [英] generator' object has no attribute 'data', problems loading some file with scipy?

查看:185
本文介绍了生成器"对象没有属性“数据",使用scipy加载某些文件时遇到问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我正在尝试用python加载.arff文件,这是我尝试的方法:

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

问题是以下输出:

    data = np.array(dataset.data)
AttributeError: 'generator' object has no attribute 'data'

为什么会这样?以及我应该如何避免呢?这是.arff:

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

推荐答案

可以安装两个arff,您可能需要安装 liac-arff ,您当前已安装arff,它会从arff.load返回生成器.

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']]}

对于已安装的Arff,不要传递文件对象,只需直接加载文件即可:

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

这篇关于生成器"对象没有属性“数据",使用scipy加载某些文件时遇到问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆