[英]Why is LIBSVM in Python asking me for floating values?
I'm implementing an SVM classification problem using LIBSVM in python. 我正在使用python中的LIBSVM实现SVM分类问题。 I have a numpy array of consisting of 1.0 and -1.0 called train_labels and the corresponding features in another numpy array called train_data.
我有一个由1.0和-1.0组成的numpy数组,名为train_labels,另一个名为train_data的numpy数组中有相应的特性。 Since LIBSVM does not accept numpy arrays, I convert them to lists using the code below.
由于LIBSVM不接受numpy数组,我使用下面的代码将它们转换为列表。
train_labels = train_labels.tolist()
train_data = train_data.tolist()
However, when I put them on svm_problem as: 但是,当我把它们放在svm_problem上时:
prob = svm_problem(train_labels,train_data)
I'm getting then error 我收到了错误
File "C:\Anaconda\lib\site-packages\svm.py", line 109, in __init__
for i, yi in enumerate(y): self.y[i] = yi
TypeError: a float is required
I've already tried converting them to float using train_labels = train_labels.astype(np.float)
before converting to list but I'm still getting the same error. 在转换为list之前,我已经尝试使用
train_labels = train_labels.astype(np.float)
将它们转换为float,但我仍然遇到相同的错误。
Using tolist() method for converting numpy array to lists before putting them on LIBSVM commands is working when I've tried them in the console. 使用tolist()方法将numpy数组转换为列表然后将它们放在LIBSVM命令上,当我在控制台中尝试它们时,它正在工作。
Does anyone know why I'm getting this error? 有谁知道为什么我收到这个错误? And how can I solve it?
我该如何解决?
guys! 伙计们! After some more idling in my code, I've found what's wrong.
在我的代码中更多闲置之后,我发现了什么是错的。
The line train_labels = train_labels.tolist()
is converting my array to a list of lists with one element in the sublist. 行
train_labels = train_labels.tolist()
将我的数组转换为列表列表,子列表中包含一个元素。 I think this is because train_labels is an nx 1 array to start with (it is read from a csv file). 我认为这是因为train_labels是一个nx 1数组(从csv文件中读取)。
So I need to reshape it first before converting to list. 所以我需要在转换到列表之前先重塑它。 And it works.
它有效。
train_labels = train_labels.reshape(N_train).tolist()
I hope it's ok to still ask this question here for those starting to use LIBSVM in the future. 我希望在这里仍然可以向那些开始使用LIBSVM的人提出这个问题。
Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.