Java 使用python将CSV文件转换为LIBSVM兼容的数据文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23170152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting CSV file to LIBSVM compatible data file using python
提问by user3378649
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?
我正在使用 libsvm 做一个项目,我正在准备我的数据以使用 lib。如何将 CSV 文件转换为 LIBSVM 兼容数据?
CSV File: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/datasets/data/iris.csv
CSV 文件:https: //github.com/scikit-learn/scikit-learn/blob/master/sklearn/datasets/data/iris.csv
In the frequencies questions:
在频率问题中:
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.
如何将其他数据格式转换为 LIBSVM 格式?
这取决于您的数据格式。一个简单的方法是在 libsvm matlab/octave 接口中使用 libsvmwrite。以 UCI 机器学习存储库中的 CSV(逗号分隔值)文件为例。我们下载 SPECTF.train。标签位于第一列。以下步骤生成 libsvm 格式的文件。
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.
but I don't wanna use matlab, I use python.
但我不想使用matlab,我使用python。
I found this solution as well using JAVA
我也使用JAVA找到了这个解决方案
Can anyone recommend a way to tackle this problem ?
任何人都可以推荐一种方法来解决这个问题吗?
回答by emeth
You can use csv2libsvm.pyto convert csv
to libsvm data
您可以使用csv2libsvm.py转换csv
为libsvm data
python csv2libsvm.py iris.csv libsvm.data 4 True
where 4 means target index
, and True
means csv
has a header.
其中 4 表示target index
,并且True
表示csv
有一个标题。
Finally, you can get libsvm.data
as
最后,你可以得到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
...
from iris.csv
从 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
...
回答by Memin
csv2libsvm.pydoes not work with Python3, and also it does not support label targets (string targets), I have slightly modified it. Now It should work with Python3 as well as label targets. I am very new to Python, so my code maybe not best practice, but I hope can help to someone.
csv2libsvm.py不适用于 Python3,也不支持标签目标(字符串目标),我对它稍作修改。现在它应该适用于 Python3 以及标签目标。我对 Python 很陌生,所以我的代码可能不是最佳实践,但我希望能对某人有所帮助。
#!/usr/bin/env python
"""
Convert CSV file to libsvm format. Works only with numeric variables.
Put -1 as label index (argv[3]) if there are no labels in your file.
Expecting no headers. If present, headers can be skipped with argv[4] == 1.
"""
import sys
import csv
import operator
from collections import defaultdict
def construct_line(label, line, labels_dict):
new_line = []
if label.isnumeric():
if float(label) == 0.0:
label = "0"
else:
if label in labels_dict:
new_line.append(labels_dict.get(label))
else:
label_id = str(len(labels_dict))
labels_dict[label] = label_id
new_line.append(label_id)
for i, item in enumerate(line):
if item == '' or float(item) == 0.0:
continue
elif item=='NaN':
item="0.0"
new_item = "%s:%s" % (i + 1, item)
new_line.append(new_item)
new_line = " ".join(new_line)
new_line += "\n"
return new_line
# ---
input_file = sys.argv[1]
try:
output_file = sys.argv[2]
except IndexError:
output_file = input_file+".out"
try:
label_index = int( sys.argv[3] )
except IndexError:
label_index = 0
try:
skip_headers = sys.argv[4]
except IndexError:
skip_headers = 0
i = open(input_file, 'rt')
o = open(output_file, 'wb')
reader = csv.reader(i)
if skip_headers:
headers = reader.__next__()
labels_dict = {}
for line in reader:
if label_index == -1:
label = '1'
else:
label = line.pop(label_index)
new_line = construct_line(label, line, labels_dict)
o.write(new_line.encode('utf-8'))