Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code in Readme.md does not work. #90

Closed
kazucmpt opened this issue Jan 24, 2022 · 7 comments · Fixed by #92
Closed

Code in Readme.md does not work. #90

kazucmpt opened this issue Jan 24, 2022 · 7 comments · Fixed by #92

Comments

@kazucmpt
Copy link

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
Copy link
Contributor

ablaom commented Jan 25, 2022

@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
Copy link
Author

kazucmpt commented Jan 25, 2022

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
Copy link
Contributor

ablaom commented Jan 25, 2022

@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).

@iblislin iblislin reopened this Jan 26, 2022
@till-m
Copy link
Member

till-m commented Feb 9, 2022

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

@ablaom
Copy link
Contributor

ablaom commented Feb 9, 2022

Thanks for the confirmation, @till-m !

@wizofe
Copy link

wizofe commented Feb 20, 2022

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
Copy link
Contributor

ablaom commented Mar 3, 2022

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants