Skip to content

Commit

Permalink
Add class_weight optimization example from #183
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterMcGushion committed Aug 26, 2019
1 parent f116a77 commit 0e24dae
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
9 changes: 8 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<td>Multiple-run-averaging in cross-validation</td>
</tr>
<tr>
<th rowspan=1 nowrap>SKLearn</th>
<th rowspan=2 nowrap>SKLearn</th>
<td nowrap>Classification</td>
<td nowrap>
<a href="sklearn_examples/classification.ipynb">NB</a>
Expand All @@ -125,6 +125,13 @@
</td>
<td>Consecutive Experiments with different models</td>
</tr>
<tr>
<td nowrap>Class Weight</td>
<td nowrap>
<a href="sklearn_examples/class_weight.py">Script</a>
</td>
<td>Optimize class_weight in multi-classification - Iris dataset</td>
</tr>
<tr>
<th rowspan=2 nowrap>XGBoost</th>
<td nowrap>Classification</td>
Expand Down
43 changes: 43 additions & 0 deletions examples/sklearn_examples/class_weight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""This example demonstrates how to optimize `class_weight` values, but may be applied to other
hyperparameters that are inside some nested object. Although this example uses SKLearn's
`RandomForestClassifier`, similar `class_weight` kwargs in other libraries can be optimized in the
same way, such as LightGBM's `LGBMClassifier` `class_weight` kwarg"""
from hyperparameter_hunter import Environment, CVExperiment, BayesianOptPro, Integer, Categorical
from hyperparameter_hunter.utils.learning_utils import get_iris_data
from sklearn.ensemble import RandomForestClassifier


def execute():
#################### Environment ####################
env = Environment(
train_dataset=get_iris_data(),
results_path="HyperparameterHunterAssets",
target_column="species",
metrics=["hamming_loss"],
cv_params=dict(n_splits=5, random_state=32),
)

#################### Experiment ####################
# Just a reference for normal `class_weight` usage outside of optimization
CVExperiment(RandomForestClassifier, dict(n_estimators=10, class_weight={0: 1, 1: 1, 2: 1}))

#################### Optimization ####################
opt = BayesianOptPro(iterations=10, random_state=32)
opt.forge_experiment(
model_initializer=RandomForestClassifier,
model_init_params=dict(
# Weight values for each class can be optimized with `Categorical`/`Integer`
class_weight={
0: Categorical([1, 3]),
1: Categorical([1, 4]),
2: Integer(1, 9), # You can also use `Integer` for low/high ranges
},
criterion=Categorical(["gini", "entropy"]),
n_estimators=Integer(5, 100),
),
)
opt.go()


if __name__ == "__main__":
execute()

0 comments on commit 0e24dae

Please sign in to comment.