import MLJ:predict,predict_mode
using MLJ,GLMakie,DataFrames,Random
Random.seed!(1222)
TaskLocalRNG()
import MLJ:predict,predict_mode
using MLJ,GLMakie,DataFrames,Random
Random.seed!(1222)
TaskLocalRNG()
function circle_data()
= make_circles(400; noise=0.1, factor=0.3)
X, y = DataFrame(X)
df = y
df.y return df
end
function moons_data()
= make_moons(400; noise=0.1)
X, y = DataFrame(X)
df = y
df.y return df
end
function blob_data()
= make_blobs(400, 2; centers=2, cluster_std=[1.0, 2.0])
X, y = DataFrame(X)
df = y
df.y return df
end
#cat=df1.y|>levels|>unique
=[:green, :purple] colors
2-element Vector{Symbol}:
:green
:purple
function plot_origin_data(df)
=Figure()
fig=Axis(fig[1,1])
axlocal cat=df.y|>levels|>unique
local colors=[:green, :purple]
for (i,c) in enumerate(cat)
=df[y.==c,:]
dscatter!(ax, d[:,1],d[:,2],color=(colors[i],0.6))
#@show d
end
figend
=100
numsfunction boundary_data(df,;n=nums)
=n2=n
n1=extrema(df[:,:x1])
xlow,xhigh=extrema(df[:,:x2])
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
function plot_desc_boudary(fig,ytest,i;df=df1,row=1)
=boundary_data(df)
tx,ty,xs,ys, xtestlocal ax=Axis(fig[row,i],title="$(names[i])")
contourf!(ax, tx,ty,ytest,levels=length(cat),colormap=:phase)
for (i,c) in enumerate(cat)
=df[y.==c,:]
dscatter!(ax, d[:,1],d[:,2],color=(colors[i],0.6))
end
hidedecorations!(ax)
end
plot_desc_boudary (generic function with 1 method)
using CatBoost.MLJCatBoostInterface
= @load SVC pkg=LIBSVM
SVC = @load KNNClassifier pkg=NearestNeighborModels
KNNClassifier = @load DecisionTreeClassifier pkg=DecisionTree
DecisionTreeClassifier = @load RandomForestClassifier pkg=DecisionTree
RandomForestClassifier = @load CatBoostClassifier pkg=CatBoost
CatBoostClassifier = @load BayesianLDA pkg=MultivariateStats
BayesianLDA = @load AdaBoostStumpClassifier pkg=DecisionTree
Booster
=[KNNClassifier,DecisionTreeClassifier,RandomForestClassifier,CatBoostClassifier,BayesianLDA,SVC]
models=["KNN","DecisionTree","RandomForest","CatBoost","BayesianLDA","SVC"]
namesfunction _fit(df::DataFrame,m)
=df[:,1:2],df[:,3]
X,y=boundary_data(df;n=nums)
_,_,xtestlocal predict= m==MLJLIBSVMInterface.SVC ? MLJ.predict : MLJ.predict_mode
=m()
model= machine(model, X, y)|>fit!
mach =predict(mach, xtest)
yhat=yhat|>Array|>d->reshape(d,nums,nums)
ytestreturn ytest
end
function plot_desc_boudary(fig,ytest,i;df=df1,row=1)
=boundary_data(df)
tx,ty,_local y=df.y
local ax=Axis(fig[row,i],title="$(names[i])")
=y|>levels|>unique
catcontourf!(ax, tx,ty,ytest,levels=length(cat),colormap=:redsblues)
for (i,c) in enumerate(cat)
=df[y.==c,:]
dscatter!(ax, d[:,1],d[:,2],color=(colors[i],0.6))
end
hidedecorations!(ax)
end
function plot_comparsion(testdata,df;row=1)
for (i,data) in enumerate(testdata)
plot_desc_boudary(fig,data,i;df=df,row=row)
end
figend
[ Info: For silent loading, specify `verbosity=0`.
[ Info: For silent loading, specify `verbosity=0`.
[ Info: For silent loading, specify `verbosity=0`.
[ Info: For silent loading, specify `verbosity=0`.
[ Info: For silent loading, specify `verbosity=0`.
[ Info: For silent loading, specify `verbosity=0`.
[ Info: For silent loading, specify `verbosity=0`.
import MLJLIBSVMInterface ✔
import NearestNeighborModels ✔
import MLJDecisionTreeInterface ✔
import MLJDecisionTreeInterface ✔
import CatBoost ✔
import MLJMultivariateStatsInterface ✔
import MLJDecisionTreeInterface ✔
plot_comparsion (generic function with 1 method)
=Figure(resolution=(2100,1000))
figfunction plot_comparsion(testdata,df,row=1)
for i in eachindex(testdata)
plot_desc_boudary(fig,testdata[i],i;df=df,row=row)
end
figend
=circle_data()
df1
=[_fit(df1,m) for (i,m) in enumerate(models)]
ytest1
=moons_data()
df2=[_fit(df2,m) for (i,m) in enumerate(models)]
ytest2
=blob_data()
df3=[_fit(df3,m) for (i,m) in enumerate(models)]
ytest3
=[df2,df1,df3]
dfs=[ytest2,ytest1,ytest3]
ytests
=Figure(resolution=(2100,1000))
fig
for (df, data,i) in zip(dfs,ytests,[1,2,3])
plot_comparsion(data,df;row=i)
end
fig
[ Info: Training machine(KNNClassifier(K = 5, …), …).
[ Info: Training machine(DecisionTreeClassifier(max_depth = -1, …), …).
[ Info: Training machine(RandomForestClassifier(max_depth = -1, …), …).
[ Info: Training machine(CatBoostClassifier(iterations = 1000, …), …).
[ Info: Training machine(BayesianLDA(method = gevd, …), …).
[ Info: Training machine(SVC(kernel = RadialBasis, …), …).
[ Info: Training machine(KNNClassifier(K = 5, …), …).
[ Info: Training machine(DecisionTreeClassifier(max_depth = -1, …), …).
[ Info: Training machine(RandomForestClassifier(max_depth = -1, …), …).
[ Info: Training machine(CatBoostClassifier(iterations = 1000, …), …).
[ Info: Training machine(BayesianLDA(method = gevd, …), …).
[ Info: Training machine(SVC(kernel = RadialBasis, …), …).
[ Info: Training machine(KNNClassifier(K = 5, …), …).
[ Info: Training machine(DecisionTreeClassifier(max_depth = -1, …), …).
[ Info: Training machine(RandomForestClassifier(max_depth = -1, …), …).
[ Info: Training machine(CatBoostClassifier(iterations = 1000, …), …).
[ Info: Training machine(BayesianLDA(method = gevd, …), …).
[ Info: Training machine(SVC(kernel = RadialBasis, …), …).