Closed Jianqing001 closed 2 years ago
use
model.fit(X, y)
instead
Ok am finally with a laptop. And i just checked out your issue.
The issue isn't with the API but rather with the eltype
of y
. Since LogisticRegression
is a Classifier the eltype
of the target vector y
can only be an instance of Integer
type. eg. Int
or Bool
So the following rough code works
julia> using RDatasets: dataset
julia> iris = dataset(“datasets”, “iris”);
julia> X = convert(Array, iris[!, [:SepalLength, :SepalWidth, :PetalLength, :PetalWidth]]);
julia> y = convert(Array, iris[!, :Species]);
julia> uniq_y = unique(y)
3-element Array{String,1}:
"setosa"
"versicolor"
"virginica"
julia> y_int = replace(y, (uniq_y .=> range(0, stop=length(uniq_y)-1))...)
150-element Array{Any,1}:
0
0
0
0
0
0
0
0
0
0
0
0
0
⋮
2
2
2
2
2
2
2
2
2
2
2
2
julia> using ScikitLearn
julia> @sk_import linear_model: LogisticRegression
julia> model = LogisticRegression(fit_intercept=true, max_iter = 200)
PyObject LogisticRegression(max_iter=200)
julia> fit!(model, X, y)
PyObject LogisticRegression(max_iter=200)
julia> predict(model, X)
150-element Array{Any,1}:
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
⋮
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
I hope this was helpful
Feel free to close this issue if satisfied
@OkonSamuel I don't understand how he could get this error though:
julia> fit!(model, X, y);
UndefVarError: fit! not defined
@Jianqing001 maybe you imported (using ...
) some other module that also defined fit!
before?
@OkonSamuel I don't understand how he could get this error though:
Another thing that could lead to his error is importing ScikitLearn as shown below
using ScikitLearn: @sk_import
Closing as an answered question...
julia> using RDatasets: dataset
julia> iris = dataset(“datasets”, “iris”);
julia> first(iris, 5) … julia> X = convert(Array, iris[!, [:SepalLength, :SepalWidth, :PetalLength, :PetalWidth]]) … julia> y = convert(Array, iris[!, :Species]) … julia> using ScikitLearn
julia> @sk_import linear_model: LogisticRegression PyObject <class ‘sklearn.linear_model._logistic.LogisticRegression’>
julia> model = LogisticRegression(fit_intercept=true, max_iter = 200) PyObject LogisticRegression(max_iter=200) julia> fit!(model, X, y); UndefVarError: fit! not defined
Stacktrace:
[2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091