如何将ARFF文件导入PANDAS DF,然后再次将其转换为ARFF
我想用Scikit从ARFF文件中学习数据库,然后在Python-Weka-Wrapper3模型上使用预处理数据库,因此我需要一个函数将ARFF加载为DF或将ARFF转换为CSV,并将其转换为CSV,并后来再次在ARFF上下载已编辑的DF或将CSV转换为ARFF。
有些人推荐 https://github.com/renatopp/lenatopp/liac-arff )但是我不知道该库如何做到这一点。
因此,如果某人在Python3上知道任何功能或某些代码,我将重新启动。
就我而言,我尝试了此功能:
def arff2csv(arff_path, csv_path=None):
with open(arff_path, 'r') as fr:
attributes = []
if csv_path is None:
csv_path = arff_path[:-4] + 'csv' # *.arff -> *.csv
write_sw = False
with open(csv_path, 'w') as fw:
for line in fr.readlines():
if write_sw:
fw.write(line)
elif '@data' in line:
fw.write(','.join(attributes) + '\n')
write_sw = True
elif '@attribute' in line:
#print(line.split(' ')[2])
attributes.append(line.split(' ')[1]) # @attribute attribute_tag numeric
print("Convert {} to {}.".format(arff_path, csv_path))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想留在Scikit-Learn生态系统中,可以看一下图书馆,使用 python-weka-wrapper3 。
btw python-weka-wrapper3可以直接创建数据集来自numpy矩阵。示例:,
If you want to stay within the scikit-learn ecosystem, you could have a look at the sklearn-weka-plugin library, which uses python-weka-wrapper3 under the hood.
BTW python-weka-wrapper3 can create datasets directly from numpy matrices. Examples: [1], [2]
我在AV上找到了这个答案,以解决将ARFF数据格式加载到PANDAS DataFrame 的问题。
I found this answer on AV to solve my problem of loading the arff data format into pandas dataframe.