# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .jl
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Julia 1.0.5
#     language: julia
#     name: julia-1.0
# ---

using RDatasets,Distributions,DecisionTree,MLBase
iris=dataset("datasets","iris")
features =convert(Array,iris[:,1:4])
labels=convert(Array,iris[:,5])

train=rand(Bernoulli(0.75),nrow(iris))
#convert(UInt8, x)
test=Bool[]
for i in 1:nrow(iris)
 push!(test,!train[i])
end

model=build_tree(labels[train],features[train,:])

model=prune_tree(model,0.7)
print_tree(model)

predictions = apply_tree(model,features[test,:])

# Performance Check

species=["setosa","virginica","versicolor"]
lm=labelmap(species)
labels_test=labelencode(lm,labels[test])
predictions_test=labelencode(lm, predictions)

println(cor(labels_test,predictions_test)) #correlation coefficient
R2(labels_test,predictions_test) #R_squared test

for i in 1:length(labels_test)
    println(labels_test[i]," - " ,predictions_test[i])
end

# we can see a single error of prediction in this set 2-3

r=roc(labels_test,predictions_test)

println(true_positive_rate(r))
println(true_negative_rate(r))
println(precision(r))
println(recall(r))

#we can repeat this for a range of different purities to get more precise results
accuracy_cv =DataFrame()
accuracy_cv[:purity]=collect(0.1:0.025:1.0)
accuracy_cv[:accuracy]= map(x->mean(nfoldCV_tree(labels[train],features[train,:],x,10)),accuracy_cv[:purity])




StarcoderdataJupyterScriptsDedupFiltered