haifengl / smile

Statistical Machine Intelligence & Learning Engine
https://haifengl.github.io
Other
5.97k stars 1.13k forks source link

svm #748

Closed Yaqiao-Li closed 10 months ago

Yaqiao-Li commented 11 months ago

Hi,

I tried to build a linear SVM model using Smile (v3.0.2) with the following code. It doesn't work when I tried to get predicted probability or score.


double [][] train_data = ...;
double [] train_label = ...; // two classes: 1 and -1

double [][] test_data = ...;
double [][] pred_result = ...;

Classifier<double[]> model = SVM.fit(train_data,train_label,1.0,1E-3);
model.predict(test_data, pred_result);
[INFO ] smile.base.svm.LASVM[finish:442] - 79269 samples, 288 support vectors, 207 bounded
Exception in thread "main" java.lang.UnsupportedOperationException: java.lang.UnsupportedOperationException: soft classification with a hard classifier
        at smile.classification.Classifier.predict(Classifier.java:191)

I also tried to use model.score function and I got the following error:

[INFO ] smile.base.svm.LASVM[finish:442] - 79269 samples, 290 support vectors, 225 bounded
Exception in thread "main" java.lang.UnsupportedOperationException
        at smile.classification.Classifier.score(Classifier.java:106)

I also want to get the weights of each feature after the model is trained.

In addition, is there a way to set class weights similar to the parameter class_weight in scikit-learn?

haifengl commented 11 months ago

You need to call PlattScaling.fit() first to calculate probabilities.