如何将数据转换libsvm格式文件

原文:http://blog.sina.com.cn/s/blog_5c2f929b0100qse8.html

有三种工具可用
1.网上有一个xls文FormatDataLibsvm.xls具有宏命令,可以利用其中的宏命令来实现。对于属性数据只有一二百的,这种工具简单方便。

2.对于一两千的就需要借助代码之类的工具了。
其实仔细看cjlin的libsvm网站http://www.csie.ntu.edu.tw/~cjlin/libsvm/,从网站的Libsvm FAQ中有提到的格式转换的方法
Q: How to convert other data formats to LIBSVM format?

It depends on your data format. We have a simple C code to transfer space/colon separated format to libsvm format. Please contact us if needed.

Alternatively, a simple way is to use libsvmwrite in the libsvm matlab/octave interface. Take a CSV (colon separated format) 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.
按照上面的命令,代入自己的数据则可以达到效果。

3.可以利用weka来转换,用weka打开csv文件,再将文件重新保存为libsvm格式。方便简单,经我测试的结果也是一致的。这个方法很好,多列属性也可行。

原文地址:https://www.cnblogs.com/zhizhan/p/5073505.html