Code
import MLJ:fit!,predict,predict_mode,predict_mean,machine
using MLJ,GLMakie,Random,DataFrames
= load_iris();
iris =DataFrame(iris);
iris =100
nums
:target] = [r.target == "virginica" ? 1.0 : 0.0 for r in eachrow(iris)]
iris[!, =coerce(iris, :target=> Multiclass )
iris=groupby(iris, :target)
gdf=iris[:,3:4],iris[:,:target]
X,y
=levels(y)
cats
function boundary_data(df,;n=nums)
=n2=n
n1=extrema(df[:,1])
xlow,xhigh=extrema(df[:,2])
ylow,yhigh= LinRange(xlow,xhigh,n1)
tx = LinRange(ylow,yhigh,n2)
ty = mapreduce(collect, hcat, Iterators.product(tx, ty));
x_test =MLJ.table(x_test')
x_testreturn tx,ty,x_test
end
=boundary_data(X)
tx,ty,x_test
using MLJ
= @load LogisticClassifier pkg=MLJLinearModels
LogisticClassifier #X, y = make_blobs(centers = 2)
= fit!(machine(LogisticClassifier(), X, y))
mach predict(mach, X)
fitted_params(mach)
=predict(mach, x_test)|>Array #返回分类概率值
probs=broadcast(pdf, probs, 1.0).|>(d->round(d,digits=2))|>d->reshape(d,nums,nums) #返回概率为1.0("virginica")的概率值 probs_res
[ Info: For silent loading, specify `verbosity=0`.
[ Info: Training machine(LogisticClassifier(lambda = 2.220446049250313e-16, …), …).
┌ Info: Solver: MLJLinearModels.LBFGS{Optim.Options{Float64, Nothing}, NamedTuple{(), Tuple{}}}
│ optim_options: Optim.Options{Float64, Nothing}
└ lbfgs_options: NamedTuple{(), Tuple{}} NamedTuple()
import MLJLinearModels ✔
100×100 Matrix{Float64}:
0.0 0.0 0.0 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0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