如何在libsvm python中加载数据集(How to load dataset in libsvm python)
我在徘徊如何加载我的数据集,以便从libsvm python实现中读取它。 我的数据是250X500矩阵,第一列是数据集标签。 我使用以下代码来读取数据:
with open("dataset3.txt") as textFile:
lines = [line.split() for line in textFile]
Matrix = [[0 for x in xrange(len(lines[0]))] for x in xrange(len(lines))]
for y in range(0, len(lines)):
for x in range(0, len(lines[0])):
Matrix[y][x] = lines[y][x]
使用上面的代码,我将我的数据读入int的2d数组。 我怎样才能使用这个数组来执行svm训练和评估?
param = svm_parameter('-t 0 -c 4 -b 1')
m = svm_train(Matrix, param)
文本文件:
1 0 9 0 0 0 0 5 2 5 15 2 3 50 0 4 6 27 0 16 34 0 11 30 12 23 41 1 0 2 0 10 67 34 ...
-1 0 10 0 0 0 0 1 0 2 5 1 8 14 0 12 11 4 2 4 22 0 6 40 8 20 47 2 1 0 0 2 1 21 0 1 11 1 ...
...
Matrix = []
with open('dataset3.txt') as f:
row = []
for line in f:
data = line.split()
target = float(data[0]) # target value
str1 = str(target)
for i,j in enumerate(data):
if i==0:
continue
else:
str1 = str1 + " " + str(i) +":"+ str(j) +" "
row.append(str1)
I am wandering how can I load my dataset, in order to read it from libsvm python implementation. My data is a 250X500 matrix and the first column is dataset labels. I am using the following code in order to read the data:
with open("dataset3.txt") as textFile:
lines = [line.split() for line in textFile]
Matrix = [[0 for x in xrange(len(lines[0]))] for x in xrange(len(lines))]
for y in range(0, len(lines)):
for x in range(0, len(lines[0])):
Matrix[y][x] = lines[y][x]
With the code above I read my data into a 2d array of int. How can I use this array in order to perform svm train and evaluation??
param = svm_parameter('-t 0 -c 4 -b 1')
m = svm_train(Matrix, param)
Text file:
1 0 9 0 0 0 0 5 2 5 15 2 3 50 0 4 6 27 0 16 34 0 11 30 12 23 41 1 0 2 0 10 67 34 ...
-1 0 10 0 0 0 0 1 0 2 5 1 8 14 0 12 11 4 2 4 22 0 6 40 8 20 47 2 1 0 0 2 1 21 0 1 11 1 ...
...
Matrix = []
with open('dataset3.txt') as f:
row = []
for line in f:
data = line.split()
target = float(data[0]) # target value
str1 = str(target)
for i,j in enumerate(data):
if i==0:
continue
else:
str1 = str1 + " " + str(i) +":"+ str(j) +" "
row.append(str1)
原文:https://stackoverflow.com/questions/23872567
更新时间:2020-05-31 17:05
最满意答案
试试这个代码
with open('dataset3.txt') as f:
Matrix = [map(float, line.split()) for line in f]
for line in f中的行读取每一行。
line.split()分割成每个value
map(float, line.split())将value转换为float
更新
OP评论了不同的输入格式。
Matrix = []
with open('dataset3.txt') as f:
for line in f:
data = line.split()
target = float(data[0]) # target value
row = []
for i, (idx, value) in enumerate([item.split(':') for item in data[1:]]):
n = int(idx) - (i + 1) # num missing
for _ in range(n):
row.append(0) # for missing
row.append(float(value))
Matrix.append(row)
Try this code
with open('dataset3.txt') as f:
Matrix = [map(float, line.split()) for line in f]
for line in f reads each line.
line.split() splits into each value
map(float, line.split()) converts value to float
Updated
OP commented different input format.
Matrix = []
with open('dataset3.txt') as f:
for line in f:
data = line.split()
target = float(data[0]) # target value
row = []
for i, (idx, value) in enumerate([item.split(':') for item in data[1:]]):
n = int(idx) - (i + 1) # num missing
for _ in range(n):
row.append(0) # for missing
row.append(float(value))
Matrix.append(row)
2014-05-26
相关问答
我不知道在Android上运行libsvm的python包装器的简单方法(除了在C库中嵌入python,但这是一种过度杀伤)。 但是,我认为如果直接从C实现访问libsvm会更容易。 这是一个解决如何在Android上编译本机代码的问题: 如何使用Android.mk编译简单的本机代码? 使其工作的另一种方法是使用原始发行版中提供的Java实现: http://www.csie.ntu.edu.tw/~cjlin/libsvm/#java 另外还有一个替代Java实现,似乎比官方java发行版更快
...
您对命令行工具的使用看起来没问题。 如果您没有像训练数据那样扩展测试数据,那么预测将会因您发现而失败。 只需从http://www.csie.ntu.edu.tw/~cjlin/libsvm/获取libsvm的源代码,并将svm-scale.c中的扩展恢复逻辑合并到您的代码中。 要查看读取缩放参数的位置,请搜索: if(restore_filename)
实际缩放是在一个名为output()的函数中完成的。 显然可以直接返回值而不是打印结果。 BTW opencv中的libsvm版本相当
...
我是EnsembleSVM包的作者。 不幸的是,该软件包还没有R接口。 目前,您唯一的选择是通过测试数据上的命令行调用esvm-predict ,然后加载包含预测的文件(使用-o标志设置)。 请注意,整体不是精确的libsvm格式。 即使看起来很相似,也无法使用libsvm工具读取集合模型。 您可以使用R的system()通过命令行调用esvm-predict 。 对于它的价值,R,MATLAB和Python的接口目前正在开发中。 我们希望在今年年底之前准备好它们以及其他一些好东西。 I am t
...
只需使用libsvm的加载和保存功能即可 svm_save_model('libsvm.model', m)
m = svm_load_model('libsvm.model')
这来自libsvm包的python目录中包含的自述文件。 它似乎比网站有更好的功能描述。 Just use libsvm's load and save functions svm_save_model('libsvm.model', m)
m = svm_load_model('libsvm.model')
Thi
...
你可以使用“sys.path.append('thatdirectory')”然后导入 you could use "sys.path.append('thatdirectory')" then import
LIBSVM数据格式如下: :: ... ... ... 如您所见,这形成了一个矩阵[(IndexCount + 1)列,LineCount行]。 更确切地说是一个稀疏矩阵 。 如果你为每个 index指定一个值,你有一个密集矩阵,但是如果你只指定了一些像 <5:value> <8:value>的索引,那么只有索引5和8以及当然label会有自定义值,所有其他值都设置为0 。 这只是为了简化符号或节省空
...
如果检查文件svm-scale.c,您会发现扩展数据的公式是: value = y_lower + (y_upper-y_lower) * (value - y_min)/(y_max-y_min);
其中y_lower y_upper是y缩放限制 因此,您可以看到缩放值未得到解决,因为您假设“从最小值中减去值然后除以特定要素的范围” 。 如果要恢复实际值,只需撤消公式即可。 例: 如果您将covtype dataset站点中可用的许多数据集作为示例,例如这一个: covtype dataset
...
sklearn.datasets.load_svmlight_file将数据加载为scipy.sparse CSR矩阵,而matplotlib散点图则需要numpy数组。 您认为将稀疏数据实现为密集的numpy数组将适合内存,您可以在其上调用.toarray()方法。 散点图也只对2D阵列数据有意义。 sklearn.datasets.load_svmlight_file will load the data as a scipy.sparse CSR matrix while matplotli
...
您可能在保存模型的时间和尝试加载模型的时间之间更改了类定义(请参阅例如什么是serialVersionUID以及我为什么要使用它?以获得一个很好的解释)。 将serialVersionUID视为一种校验和,确保您不会加载过时的类版本。 除非那是你想要的,因为你比Java更了解你仍然可以使用旧模型 - 在这种情况下你可以手动设置该ID。 为了告诉Java您当前的分类器仍然是最新的,请添加 static final long serialVersionUID = -864263723119664692
...
试试这个代码 with open('dataset3.txt') as f:
Matrix = [map(float, line.split()) for line in f]
for line in f中的行读取每一行。 line.split()分割成每个value map(float, line.split())将value转换为float 更新 OP评论了不同的输入格式。 Matrix = []
with open('dataset3.txt') as f:
for lin
...