简体   繁体   中英

trying to create a .arff file using python

I want to create a .arff file that displays 10 most useful words from my python code. The format should be like this.

@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

Each of the lines should contain a list of 10 "true" or "false" values, followed by an emotion.

This is what I have written so far but it is not displaying as required. pls help me out.

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

you should check out this library It was designed just for this problem, since handcoding your arff output is not a good idea.

For your attributes you'll do something like this:

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

And for your data:

data = [[1,2,'a'], [3, 4, 'john']]
arff.dump(open(fileName, 'w'), data, relation="whatever", header_names)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM