Code
import MLJ:fit!,predict,predict_mode,predict_mean,machine
using MLJ,GLMakie,Random,DataFrames
iris = load_iris();
iris =DataFrame(iris);
nums=100
iris[!, :target] = [r.target == "virginica" ? 1.0 : 0.0 for r in eachrow(iris)]
iris=coerce(iris, :target=> Multiclass )
gdf=groupby(iris, :target)
X,y=iris[:,3:4],iris[:,:target]
cats=levels(y)
function boundary_data(df,;n=nums)
n1=n2=n
xlow,xhigh=extrema(df[:,1])
ylow,yhigh=extrema(df[:,2])
tx = LinRange(xlow,xhigh,n1)
ty = LinRange(ylow,yhigh,n2)
x_test = mapreduce(collect, hcat, Iterators.product(tx, ty));
x_test=MLJ.table(x_test')
return tx,ty,x_test
end
tx,ty,x_test=boundary_data(X)
using MLJ
LogisticClassifier = @load LogisticClassifier pkg=MLJLinearModels
#X, y = make_blobs(centers = 2)
mach = fit!(machine(LogisticClassifier(), X, y))
predict(mach, X)
fitted_params(mach)
probs=predict(mach, x_test)|>Array #返回分类概率值
probs_res=broadcast(pdf, probs, 1.0).|>(d->round(d,digits=2))|>d->reshape(d,nums,nums) #返回概率为1.0("virginica")的概率值[ 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.0 0.0 0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