Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Introduce BalancedBagging #2

Merged
merged 15 commits into from
Sep 27, 2023
Merged
2 changes: 2 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
matrix:
version:
- '1.8'
- '1'

os: [ubuntu-latest, windows-latest, macOS-latest]
arch:
- x64
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/Manifest.toml
.CondaPkg/*
.CondaPkg
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ authors = ["Essam <[email protected]> and contributors"]
version = "1.0.0-DEV"

[deps]
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
MLJBase = "a7f614a8-145f-11e9-1d2a-a57a1082229d"
MLJModelInterface = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
MLJBase = "0.21"
Expand All @@ -17,9 +20,10 @@ julia = "1.6"
[extras]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Imbalance = "c709b415-507b-45b7-9a3d-1767c89fde68"
MLJLinearModels = "6ee0df7b-362f-4a72-a706-9e79364fb692"
MLJModels = "d491faf4-2d78-11e9-2867-c94bc002c0b7"
MLJLinearModels = "6ee0df7b-362f-4a72-a706-9e79364fb692"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Imbalance", "DataFrames", "MLJLinearModels", "MLJModels"]
test = ["Test", "Imbalance", "DataFrames", "MLJLinearModels", "MLJModels", "Tables"]
165 changes: 165 additions & 0 deletions examples/BalancedBagging.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ENV[\"JULIA_PKG_SERVER\"] = \"\"\n",
"using Pkg\n",
"Pkg.activate(@__DIR__)\n",
"Pkg.instantiate()\n",
"\n",
"\n",
"using MLJBalancing\n",
"using Imbalance\n",
"using MLJ\n",
"using Random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Load Data"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((Column1 = [0.9695150609084499, 0.012898301755861596, 0.7555027304121053, 0.3467415729179013, 0.35969402837473463, 0.2601876747805505, 0.9522580699968279, 0.06304475092339623, 0.18909001622655808, 0.19934942931986965 … 0.021532597906190776, 0.8482825697641306, 0.10773487816863903, 0.32189982199036116, 0.12662208474317038, 0.28529465447429614, 0.2907506630258835, 0.36872799387588473, 0.061489791166806085, 0.45645058368583713], Column2 = [0.06546916714160167, 0.7243956502957003, 0.5183099801474415, 0.7555562860508294, 0.11226218114407538, 0.9135150277876691, 0.8739421974558176, 0.2268482788660101, 0.580604436651146, 0.4142252330250549 … 0.6517425913240111, 0.01713263102740481, 0.7175499403837856, 0.7362894157420817, 0.24893665902538054, 0.41499951381631595, 0.2159527717429719, 0.8966879835264249, 0.87252430655793, 0.41461921031276117], Column3 = [0.5939320702328891, 0.19329886972497456, 0.04656947038518311, 0.22095698685781184, 0.678807659662497, 0.12720198818430306, 0.6795750371448686, 0.9314917999820301, 0.22920734893984274, 0.5148148980955375 … 0.55049773593343, 0.038576459283091946, 0.27765727942909757, 0.2753072414696357, 0.8823620780359746, 0.44831794170895023, 0.9073846432163745, 0.4648550947905655, 0.311984726769037, 0.25829997798611304], Column4 = [0.12253944650540982, 0.8259140842535423, 0.4034477332184384, 0.5279399406265695, 0.5579944087437719, 0.24650366028608328, 0.6874897000162434, 0.23391406844015605, 0.5641254897013973, 0.6250622796341656 … 0.21708181942178983, 0.35224683896541464, 0.8444113778983325, 0.4547214584884428, 0.13508852017592232, 0.9510137735662383, 0.5723463533029658, 0.626377972762265, 0.7854013810594317, 0.15394691114473347], Column5 = [0.47958743625921163, 0.45779753417165514, 0.6367059235247621, 0.8601116026079643, 0.3334020182022719, 0.41593698717526373, 0.13208968772625174, 0.16951044109747648, 0.8137887839507706, 0.4429229861115882 … 0.01308976221980429, 0.48597926808091163, 0.20768781798463476, 0.30045611276046247, 0.15759293576302558, 0.975806377881983, 0.19451065500145392, 0.9638103356367584, 0.3594043445295293, 0.7792867217495332], Column6 = [3.0, 3.0, 1.0, 3.0, 1.0, 2.0, 3.0, 2.0, 3.0, 3.0 … 3.0, 2.0, 1.0, 2.0, 1.0, 2.0, 2.0, 3.0, 3.0, 1.0], Column7 = [2.0, 2.0, 2.0, 2.0, 1.0, 2.0, 2.0, 2.0, 1.0, 1.0 … 2.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0]), CategoricalArrays.CategoricalValue{Int64, UInt32}[0, 0, 0, 0, 0, 0, 0, 0, 1, 0 … 0, 0, 1, 0, 1, 0, 0, 0, 0, 0])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"X, y = generate_imbalanced_data(100, 5; cat_feats_num_vals = [3, 2], \n",
" probs = [0.9, 0.1], \n",
" type = \"ColTable\", \n",
" rng=42)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Construct `BalancedBaggingClassifier` Model"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BalancedBaggingClassifier(\n",
" model = LogisticClassifier(\n",
" lambda = 2.220446049250313e-16, \n",
" gamma = 0.0, \n",
" penalty = :l2, \n",
" fit_intercept = true, \n",
" penalize_intercept = false, \n",
" scale_penalty_with_samples = true, \n",
" solver = nothing), \n",
" T = 10, \n",
" rng = Xoshiro(0xa379de7eeeb2a4e8, 0x953dccb6b532b3af, 0xf597b8ff8cfd652a, 0xccd7337c571680d1))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"LogisticClassifier = @load LogisticClassifier pkg=MLJLinearModels verbosity=0\n",
"logistic_model = LogisticClassifier()\n",
"model = BalancedBaggingClassifier(classifier=logistic_model, T=10, rng=Random.Xoshiro(42))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Train & Evaluate the Model"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Training machine(LogisticClassifier(lambda = 2.220446049250313e-16, …), …).\n",
"└ @ MLJBase /Users/essam/.julia/packages/MLJBase/ByFwA/src/machines.jl:492\n",
"┌ Info: Solver: MLJLinearModels.LBFGS{Optim.Options{Float64, Nothing}, NamedTuple{(), Tuple{}}}\n",
"│ optim_options: Optim.Options{Float64, Nothing}\n",
"│ lbfgs_options: NamedTuple{(), Tuple{}} NamedTuple()\n",
"└ @ MLJLinearModels /Users/essam/.julia/packages/MLJLinearModels/zSQnL/src/mlj/interface.jl:72\n"
]
},
{
"data": {
"text/plain": [
"100-element CategoricalDistributions.UnivariateFiniteVector{Multiclass{2}, Int64, UInt32, Float64}:\n",
" UnivariateFinite{Multiclass{2}}(0=>0.928, 1=>0.0722)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.845, 1=>0.155)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.749, 1=>0.251)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.902, 1=>0.0977)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.804, 1=>0.196)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.864, 1=>0.136)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.851, 1=>0.149)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.954, 1=>0.0458)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.853, 1=>0.147)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.86, 1=>0.14)\n",
" ⋮\n",
" UnivariateFinite{Multiclass{2}}(0=>0.671, 1=>0.329)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.73, 1=>0.27)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.843, 1=>0.157)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.941, 1=>0.0594)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.872, 1=>0.128)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.92, 1=>0.0797)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.929, 1=>0.0714)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.791, 1=>0.209)\n",
" UnivariateFinite{Multiclass{2}}(0=>0.827, 1=>0.173)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mach = machine(logistic_model, X, y)\n",
"fit!(mach)\n",
"pred = predict(mach, X)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.8.5",
"language": "julia",
"name": "julia-1.8"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.8.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading