CaseWhenRuler
¶
Helper class to construct "case when"-style FunctionClassifiers.
This class allows you to write a system of rules using lambda functions. These functions cannot be pickled by scikit-learn however, so if you'd like to use this class in a GridSearch you will need to wrap it around a FunctionClassifier.
Parameters
Name | Type | Description | Default |
---|---|---|---|
default |
the default value to predict if no rules apply | None |
Usage:
from hulearn.datasets import load_titanic
from hulearn.experimental import CaseWhenRuler
from hulearn.classification import FunctionClassifier
def make_prediction(dataf, age=15):
ruler = CaseWhenRuler(default=0)
(ruler
.add_rule(lambda d: (d['pclass'] < 3.0) & (d['sex'] == "female"), 1, name="gender-rule")
.add_rule(lambda d: (d['pclass'] < 3.0) & (d['age'] <= age), 1, name="child-rule"))
return ruler.predict(dataf)
clf = FunctionClassifier(make_prediction)
add_rule(self, when, then, name=None)
¶
Show source code in experimental/ruler.py
41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
Adds a rule to the system.
Parameters
Name | Type | Description | Default |
---|---|---|---|
when |
a (lambda) function that tells us when the rule applies | required | |
then |
the value to output if the rule applies | required | |
name |
an optional name for the rule | None |
predict(self, X)
¶
Show source code in experimental/ruler.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
Makes a prediction based on the rules sofar.
Usage:
from hulearn.classification import FunctionClassifier
from hulearn.experimental import CaseWhenRuler
def make_prediction(dataf, gender_rule=True, child_rule=True, fare_rule=True):
ruler = CaseWhenRuler(default=0)
if gender_rule:
ruler.add_rule(when=lambda d: (d['pclass'] < 3.0) & (d['sex'] == "female"),
then=1,
name="gender-rule")
if child_rule:
ruler.add_rule(when=lambda d: (d['pclass'] < 3.0) & (d['age'] <= 15),
then=1,
name="child-rule")
if fare_rule:
ruler.add_rule(when=lambda d: (d['fare'] > 100),
then=1,
name="fare-rule")
return ruler.transform(dataf)
clf = FunctionClassifier(make_prediction)
transform(self, X)
¶
Show source code in experimental/ruler.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
Produces a dataframe that indicates the state of all rules.
Usage:
from hulearn.preprocessing import PipeTransformer
from hulearn.experimental import CaseWhenRuler
def make_prediction(dataf, gender_rule=True, child_rule=True, fare_rule=True):
ruler = CaseWhenRuler(default=0)
if gender_rule:
ruler.add_rule(when=lambda d: (d['pclass'] < 3.0) & (d['sex'] == "female"),
then=1,
name="gender-rule")
if child_rule:
ruler.add_rule(when=lambda d: (d['pclass'] < 3.0) & (d['age'] <= 15),
then=1,
name="child-rule")
if fare_rule:
ruler.add_rule(when=lambda d: (d['fare'] > 100),
then=1,
name="fare-rule")
return ruler.transform(dataf)
clf = PipeTransformer(make_prediction)