Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the class labels from an sklearn.svm.LinearSVC object

How can we get the class labels (e.g., ['business','lifestyle','sports','tech']) from a classifier object? The classifier method predict is able to produce the labels, so I guess it should be stored somewhere inside the classifier object.

I can't find it in the documentation (http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html)

Anyone knows how to get the class labels?

Thanks!

like image 871
justhalf Avatar asked Aug 22 '13 08:08

justhalf


2 Answers

There is a classes_ field.

>>> from sklearn import svm
>>> clt = svm.SVC()
>>> clt.fit( [[1],[2],[3]], ["a","b","a"] )
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='rbf', max_iter=-1, probability=False, shrinking=True, tol=0.001,
  verbose=False)
>>> clt.classes_
array(['a', 'b'], 
      dtype='|S2')
like image 108
lejlot Avatar answered Nov 14 '22 22:11

lejlot


I found it, it's hidden in the classes_ attribute of the object. Found it after reading the source code.

like image 45
justhalf Avatar answered Nov 15 '22 00:11

justhalf