0

我想创建一个 .arff 文件,显示我的 python 代码中最有用的 10 个单词。格式应该是这样的。

@attribute pattern1 {yes,no}
@attribute pattern2 {yes,no} 
......
.......
@attribute emotion {angry,disgusted,fearful,happy,sad,surprised}

@data
yes, no, no,......, yes, happy
no, no, no,....., no, angry
yes, yes, no,......, yes, sad

每行应包含 10 个“真”或“假”值的列表,然后是一种情绪。

这是我到目前为止所写的,但它没有按要求显示。请帮帮我。

f = open("emotions.txt", "w")
f.write('''@RELATION Emotions\n
    @ATTRIBUTE word{yes,no}
    @ATTRIBUTE class {angry,sad,happy,surprised,fearful,disgusted}
    @DATA\n''')
for word in varall:
f.write("%s\n" %word)
f.close()
4

1 回答 1

0

你应该看看这个库 它就是为这个问题而设计的,因为手工编码你的 arff 输出不是一个好主意。

对于您的属性,您将执行以下操作:

arff_writer = arff.Writer(fileName, relation='Emotions',  header_names=['pattern1','pattern2', ... 'emotion')
arff_writer.pytypes[arff.nominal] = '{angry,disgusted,fearful,happy,sad,surprised}'
arff_writer.write([arff.nominal('emotion')])

对于您的数据:

data = [[1,2,'a'], [3, 4, 'john']]
arff.dump(open(fileName, 'w'), data, relation="whatever", header_names)
于 2013-11-25T16:07:19.453 回答