如何将ARFF文件导入PANDAS DF,然后再次将其转换为ARFF

发布于 2025-01-28 04:48:30 字数 1155 浏览 3 评论 0 原文

我想用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))

I want to preprocess a data base with scikit learn from an arff file, and later use on an python-weka-wrapper3 model the preprocessed data base, so I need a function to load the arff as df or transform the arff to csv, and later again download the edited df on an arff or transform a csv to arff.

Some people recomend https://github.com/renatopp/liac-arff (liac-arff) but I don't know how to do that with this library.

So, if someone knows any function or some code well explained on python3 I'll apreciate.

In my case I tried with this function:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的位置 2025-02-04 04:48:30

如果您想留在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]

唠甜嗑 2025-02-04 04:48:30

我在AV上找到了这个答案,以解决将ARFF数据格式加载到PANDAS DataFrame 的问题。


from scipy.io import arff
import pandas

data = arff.loadarff('data_file_name.arff')
df = pandas.DataFrame(data[0])

I found this answer on AV to solve my problem of loading the arff data format into pandas dataframe.

https://discuss.analyticsvidhya.com/t/loading-arff-type-files-in-python/27419/2

from scipy.io import arff
import pandas

data = arff.loadarff('data_file_name.arff')
df = pandas.DataFrame(data[0])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文