22

Quick SVM question for scikit-learn. When you train an SVM, it's something like

from sklearn import svm
s = svm.SVC()
s.fit(training_data, labels)

Is there any way for labels to be a list of a non-numeric type? For instance, if I want to classify vectors as 'cat' or 'dog,' without having to have some kind of external lookup table that encodes 'cat' and 'dog' into 1's and 2's. When I try to just pass a list of strings, I get ...

ValueError: invalid literal for float(): cat

So, it doesn't look like just shoving strings in labels will work. Any ideas?

follyroof
  • 3,430
  • 2
  • 28
  • 26

3 Answers3

21

Passing strings as classes directly is on my todo, but it is not supported in the SVMs yet. For the moment, we have the LabelEncoder that can do the book keeping for you.

[edit]This should work now out of the box[/edit]

Andreas Mueller
  • 27,470
  • 8
  • 62
  • 74
15

The recent version of sklearn is able to use string as the labels. For example:

from sklearn.svm import SVC
clf = SVC()
x = [[1,2,3], [4,5,6]]
y = ['dog', 'cat']
clf.fit(x,y)

yhat = clf.predict([[1,2,5]])
print yhat[0]
tqjustc
  • 3,624
  • 6
  • 27
  • 42
0

In the latest version, you can pass the strings in labels. But in the previous version, you need to encode the labels first with LabelEncoder.

from sklearn import preprocessing
from sklearn import svm
le = preprocessing.LabelEncoder()
new_labels = le.fit_transform(["cat", "cat", "dog", "bat"])
le.inverse_transform([0, 0, 1, 2])
s = svm.SVC()
s.fit(training_data, labels)

To get the original labels back use

le.inverse_transform([0, 0, 1, 2])

output will be

["cat", "cat", "dog", "bat"]