pa-m / sklearn

bits of sklearn ported to Go #golang
MIT License
345 stars 38 forks source link

Question: svm does not work like python. #19

Open mattn opened 3 years ago

mattn commented 3 years ago

I'm playing with pa-m/sklearn and wrote simple code to classify iris with svm.

package main

import (
    "fmt"
    "time"

    "github.com/pa-m/sklearn/datasets"
    "github.com/pa-m/sklearn/metrics"
    modelselection "github.com/pa-m/sklearn/model_selection"
    "github.com/pa-m/sklearn/preprocessing"
    "github.com/pa-m/sklearn/svm"
    "gonum.org/v1/gonum/mat"
)

func main() {
    ds := datasets.LoadIris()
    X1 := ds.X
    yscaler := preprocessing.NewMinMaxScaler([]float64{-1, 1})
    Y1, _ := yscaler.FitTransform(ds.Y, nil)
    Xtrain, Xtest, Ytrain, Ytest := modelselection.TrainTestSplit(X1, Y1, 0.25, uint64(time.Now().UnixNano()))

    clf := svm.NewSVC()
    clf.C = 0.1
    clf.Kernel = "rbf"
    clf.Fit(Xtrain, Ytrain)

    _ = Xtest
    _ = Ytest
    result := mat.NewDense(Ytest.RawMatrix().Rows, 1, nil)
    clf.Predict(Xtest, result)
    fmt.Println(mat.Formatted(result))
    fmt.Printf("%.02f%%\n", metrics.AccuracyScore(result, Ytest, true, nil)*100)
}

This is based on Python code.

import sklearn.datasets
import sklearn.model_selection
import sklearn.metrics
import sklearn.svm

iris = sklearn.datasets.load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test =\
  sklearn.model_selection.train_test_split(X, y, test_size=0.25, random_state=1234)

clf = sklearn.svm.SVC(kernel='rbf', C=0.1)
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)
score = sklearn.metrics.accuracy_score(y_test, y_pred)
print(score)

Go code don't finish training. Is this something wrong?

qismyq commented 2 years ago

Hi,i am running 12 hours for svr fit ,it looks like can not finish,do you know how it works?My code likes yours

a2htray commented 2 years ago

I got the same problem today, And anyone knows the method to finish the fit task? @pa-m

a2htray commented 2 years ago

Resolve this problem by adjusting the parameter svr.Epsilon. In the svr.go file. There is a for loop condition, so we should to make the condition to false by domain knowledge.

mattn commented 2 years ago

Yes, SVR works fine but SVC does not.

a2htray commented 2 years ago

Hi, try to use the parameter Tol bigger.