gpt4 book ai didi

java - 使用 python 将 CSV 文件转换为 LIBSVM 兼容数据文件

转载 作者:太空狗 更新时间:2023-10-30 00:40:47 27 4
gpt4 key购买 nike

我正在使用 libsvm 做一个项目,我正在准备我的数据以使用该库。如何将 CSV 文件转换为 LIBSVM 兼容数据?

CSV File: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/datasets/data/iris.csv

在频率问题中:

How to convert other data formats to LIBSVM format?

It depends on your data format. A simple way is to use libsvmwrite in the libsvm matlab/octave interface. Take a CSV (comma-separated values) file in UCI machine learning repository as an example. We download SPECTF.train. Labels are in the first column. The following steps produce a file in the libsvm format.

matlab> SPECTF = csvread('SPECTF.train'); % read a csv file
matlab> labels = SPECTF(:, 1); % labels from the 1st column
matlab> features = SPECTF(:, 2:end);
matlab> features_sparse = sparse(features); % features must be in a sparse matrix
matlab> libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);
The tranformed data are stored in SPECTFlibsvm.train.
Alternatively, you can use convert.c to convert CSV format to libsvm format.

但我不想使用 matlab,我使用 python。

我也使用 JAVA 找到了这个解决方案

谁能推荐一种方法来解决这个问题?

最佳答案

您可以使用 csv2libsvm.pycsv 转换为 libsvm 数据

python csv2libsvm.py iris.csv libsvm.data 4 True

其中 4 表示 target indexTrue 表示 csv 有标题。

最后,你可以得到libsvm.data作为

0 1:5.1 2:3.5 3:1.4 4:0.2
0 1:4.9 2:3.0 3:1.4 4:0.2
0 1:4.7 2:3.2 3:1.3 4:0.2
0 1:4.6 2:3.1 3:1.5 4:0.2
...

来自 iris.csv

150,4,setosa,versicolor,virginica
5.1,3.5,1.4,0.2,0
4.9,3.0,1.4,0.2,0
4.7,3.2,1.3,0.2,0
4.6,3.1,1.5,0.2,0
...

关于java - 使用 python 将 CSV 文件转换为 LIBSVM 兼容数据文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23170152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com