JuliaML / LIBSVM.jl

LIBSVM bindings for Julia
Other
88 stars 35 forks source link

Code in Readme.md does not work. #90

Closed kazucmpt closed 2 years ago

kazucmpt commented 2 years ago

I use julia 1.6.1 I installed LIBSVM.jl in Ubuntu as ] add LIBSVM

After that, I run the following source code in README.md.

using LIBSVM
using RDatasets

# Classification C-SVM
iris = dataset("datasets", "iris")
X = Matrix(iris[:, 1:4])
y = iris.Species

Xtrain = X[1:2:end, :]
Xtest  = X[2:2:end, :]
ytrain = y[1:2:end]
ytest  = y[2:2:end]

model = fit!(SVC(), Xtrain, ytrain)
ŷ = predict(model, Xtest)

But I get the following error.

ERROR: LoadError: MethodError: no method matching fit!(::SVC, ::Matrix{Float64}, ::CategoricalArrays.CategoricalVector{String, UInt8, String, CategoricalArrays.CategoricalValue{String, UInt8}, Union{}})
Closest candidates are:
  fit!(::Union{LIBSVM.AbstractSVC, LIBSVM.AbstractSVR}, ::AbstractMatrix{T} where T) at /home/****/.julia/packages/LIBSVM/5Z99T/src/ScikitLearnAPI.jl:68
  fit!(::Union{LIBSVM.AbstractSVC, LIBSVM.AbstractSVR}, ::AbstractMatrix{T} where T, ::Vector{T} where T) at /home/*****/.julia/packages/LIBSVM/5Z99T/src/ScikitLearnAPI.jl:68
  fit!(::LinearSVC, ::AbstractMatrix{T} where T, ::Vector{T} where T) at /home/******/.julia/packages/LIBSVM/5Z99T/src/ScikitLearnAPI.jl:100
Stacktrace:
 [1] top-level scope
   @ ~/***/***/***.jl:14
in expression starting at /home/*****/****/****/****.jl:14
ablaom commented 2 years ago

@kazucmpt Thanks for reporting.

It looks to me that you have mixed the README.md example code with some MLJ code?? The LIBSVM package does not have a fit! method.

The train/prediction code on the README looks like this:

# Train SVM on half of the data using default parameters. See documentation
# of svmtrain for options
model = svmtrain(Xtrain, ytrain)

# Test model on the other half of the data.
ŷ, decision_values = svmpredict(model, Xtest);

# Compute accuracy
@printf "Accuracy: %.2f%%\n" mean(ŷ .== ytest) * 100

Are you wanting to train this model in MLJ? I could provide guidance if you want.

kazucmpt commented 2 years ago

Thank you for your quick reply. Sorry, my initial code was wrong, however, the code still does not work. Anyway, I found a good answer to this problem. Now, the problem was solved.

https://stackoverflow.com/questions/62594396/how-should-i-train-a-svm-using-julia/70838091#70838091

ablaom commented 2 years ago

@iblis17 Perhaps this should be re-opened. I haven't checked, but it seems from the thread cited in the previous comment that there is a problem (and the same thread proposes a correction).

till-m commented 2 years ago

@ablaom the code snippet @kazucmpt posted is indeed part of the README.md, see here, however the code works for me.

ablaom commented 2 years ago

Thanks for the confirmation, @till-m !

wizofe commented 2 years ago

Are you wanting to train this model in MLJ? I could provide guidance if you want.

It would be very useful/helpful to provide some example code to use it via MLJ, let's say on the iris dataset as an example. ta!

ablaom commented 2 years ago
using MLJ
SVC = @load SVC pkg=LIBSVM  # load code defining model type
model = SVC()  # model instance

# split data
y, X = unpack(iris, ==(:class))

# fit data
mach  = machine(model, X, y) |> fit!

# predict
julia> predict(mach, X)[1]
CategoricalArrays.CategoricalValue{String, UInt32} "Iris-setosa"