使用python将CSV文件转换为LIBSVM兼容的数据文件 [英] Converting CSV file to LIBSVM compatible data file using python

查看:3220
本文介绍了使用python将CSV文件转换为LIBSVM兼容的数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am doing a project using libsvm and I am preparing my data to use the lib. How can I convert CSV file to LIBSVM compatible data?


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

在频率问题中:


如何将其他数据格式转换为LIBSVM格式?

How to convert other data formats to LIBSVM format?

这取决于您的数据格式。一个简单的方法是在libsvm matlab / octave接口中使用libsvmwrite。以UCI机器学习库中的CSV(逗号分隔值)文件为例。我们下载SPECTF.train。标签在第一列。以下步骤以libsvm格式生成文件。

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。

but I don't wanna use matlab, I use python.

我使用 JAVA

任何人都可以推荐一种方法来解决这个问题?

Can anyone recommend a way to tackle this problem ?

推荐答案

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

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

其中4表示目标索引 True 表示 csv

where 4 means target index, and True means csv has a header.

最后,您可以将 libsvm.data 作为 p>

Finally, you can get libsvm.data as

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
...

这篇关于使用python将CSV文件转换为LIBSVM兼容的数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆