Quentin
Quentin

Reputation: 3290

scikit OneClassSvm sparse matrix return (very) different result than dense

I'm using a OneClassSVM classifier with dense matrix, the results are pretty good. I'd like to include some texts in my features and use a sparse matrix, however I get really differents (and wrong) result while using a sparse matrix, I don't understand why

Here is an example :

import io
from pandas import pandas
import scipy
from sklearn import svm

t="""A,B,C,D,E,F
     11,1,2,3,4,5
     11,1,2,0,3,6
     11,1,2,3,2,5
     11,2,0,3,1,7
     11,4,2,3,0,5"""
t_test="""A,B,C,D,E,F
          12,1,3,0,1,5
          14,2,2,3,2,8
          12,1,2,3,4,5
          18,2,3,1,3,2"""
df = pandas.read_csv(io.StringIO(t), dtype=float)
df_test = pandas.read_csv(io.StringIO(t_test), dtype=float)

cl = svm.OneClassSVM(nu=0.1, kernel='rbf', gamma=0.16)
cl.fit(df)
print(cl.decision_function(df_test))

cl = svm.OneClassSVM(nu=0.1, kernel='rbf', gamma=0.16)
cl.fit(scipy.sparse.csr_matrix(df.values))
print(cl.decision_function(scipy.sparse.csr_matrix(df_test)))

Result :

#Dense
[[-0.094537  ]
 [-0.13060355]
 [-0.02208006]
 [-0.14990236]]
#sparse
[[ -4.67612004e-311]
 [ -6.79156324e-311]
 [ -5.92318332e-311]
 [ -6.94061414e-311]]

I also tried scikit OneClassSVM example with a sparse matrix and the result is pretty bad : https://gist.github.com/Avricot/68775656ab77217e5569

Here are the results : Dense matrix (as expected): enter image description here Sparse matrix (incorrect): enter image description here

What's wrong with sparse matrix ?

Upvotes: 0

Views: 959

Answers (1)

Ibraim Ganiev
Ibraim Ganiev

Reputation: 9390

It's a bug https://github.com/scikit-learn/scikit-learn/issues/5095 It was fixed in 5093 pull request. If you want - you can wait until someone merge it into master, or use it now:

git clone https://github.com/scikit-learn/scikit-learn.git
cd scikit-learn/
git fetch origin refs/pull/5093/head:pn_5093
git checkout pn_5093
python3 setup.py install --user

Upvotes: 2

Related Questions