Skip to content

Instantly share code, notes, and snippets.

@sdwfrost
Last active June 8, 2019 13:01
Show Gist options
  • Save sdwfrost/450cd038405a20970bce307cc44403b8 to your computer and use it in GitHub Desktop.
Save sdwfrost/450cd038405a20970bce307cc44403b8 to your computer and use it in GitHub Desktop.
Jupyter notebook for MLJ using Iris data and DecisionTree
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[34mSupervisedTask{} @ 3…62\u001b[39m\n"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using MLJ\n",
"iris = load_iris()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[34mSupervisedTask{} @ 3…62\u001b[39m\n"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"import MLJModels ✔\n",
"import DecisionTree ✔\n",
"import MLJModels.DecisionTree_.DecisionTreeClassifier ✔\n"
]
}
],
"source": [
"@load DecisionTreeClassifier"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeClassifier(pruning_purity = 1.0,\n",
" max_depth = 2,\n",
" min_samples_leaf = 1,\n",
" min_samples_split = 2,\n",
" min_purity_increase = 0.0,\n",
" n_subfeatures = 0.0,\n",
" display_depth = 5,\n",
" post_prune = false,\n",
" merge_purity_threshold = 0.9,)\u001b[34m @ 8…98\u001b[39m"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_model = DecisionTreeClassifier(max_depth=2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[34mMachine{DecisionTreeClassifier} @ 8…47\u001b[39m\n"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree = machine(tree_model, iris)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Training \u001b[34mMachine{DecisionTreeClassifier} @ 8…47\u001b[39m.\n",
"└ @ MLJ /home/simon/.julia/dev/MLJ/src/machines.jl:130\n"
]
},
{
"ename": "MethodError",
"evalue": "MethodError: no method matching build_tree(::CategoricalArrays.CategoricalArray{String,1,UInt32,String,CategoricalArrays.CategoricalString{UInt32},Union{}}, ::Array{Float64,2}, ::Float64, ::Int64, ::Int64, ::Int64, ::Float64)\nClosest candidates are:\n build_tree(!Matched::Array{T<:Float64,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any, ::Any; rng) where {S, T<:Float64} at /home/simon/.julia/dev/DecisionTree/src/regression/main.jl:27\n build_tree(!Matched::Array{T,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any, ::Any; rng) where {S, T} at /home/simon/.julia/dev/DecisionTree/src/classification/main.jl:83\n build_tree(!Matched::Array{T<:Float64,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any) where {S, T<:Float64} at /home/simon/.julia/dev/DecisionTree/src/regression/main.jl:27\n ...",
"output_type": "error",
"traceback": [
"MethodError: no method matching build_tree(::CategoricalArrays.CategoricalArray{String,1,UInt32,String,CategoricalArrays.CategoricalString{UInt32},Union{}}, ::Array{Float64,2}, ::Float64, ::Int64, ::Int64, ::Int64, ::Float64)\nClosest candidates are:\n build_tree(!Matched::Array{T<:Float64,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any, ::Any; rng) where {S, T<:Float64} at /home/simon/.julia/dev/DecisionTree/src/regression/main.jl:27\n build_tree(!Matched::Array{T,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any, ::Any; rng) where {S, T} at /home/simon/.julia/dev/DecisionTree/src/classification/main.jl:83\n build_tree(!Matched::Array{T<:Float64,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any) where {S, T<:Float64} at /home/simon/.julia/dev/DecisionTree/src/regression/main.jl:27\n ...",
"",
"Stacktrace:",
" [1] fit(::DecisionTreeClassifier, ::Int64, ::DataFrames.DataFrame, ::CategoricalArrays.CategoricalArray{String,1,UInt32,String,CategoricalArrays.CategoricalString{UInt32},Union{}}) at /home/simon/.julia/dev/MLJModels/src/DecisionTree.jl:110",
" [2] #fit!#3(::Array{Int64,1}, ::Int64, ::Bool, ::Function, ::Machine{DecisionTreeClassifier}) at /home/simon/.julia/dev/MLJ/src/machines.jl:131",
" [3] (::getfield(StatsBase, Symbol(\"#kw##fit!\")))(::NamedTuple{(:rows,),Tuple{Array{Int64,1}}}, ::typeof(fit!), ::Machine{DecisionTreeClassifier}) at ./none:0",
" [4] top-level scope at In[6]:2"
]
}
],
"source": [
"train, test = partition(eachindex(iris), 0.7, shuffle=true); # 70:30 split\n",
"fit!(tree, rows=train)\n",
"yhat = predict(tree, iris);\n",
"misclassification_rate(yhat, iris)"
]
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Julia 1.1.0",
"language": "julia",
"name": "julia-1.1"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment