Rule Generator (Optimisation algorithm) Example

This notebook contains an example of how the Rule Generator (Optimisation algorithm) can be used to create rules based on a labelled dataset. This algorithm generate rules by optimising the thresholds of single features and combining these one condition rules with AND conditions to create more complex rules.

Requirements

To run, you’ll need the following:

  • A labelled, processed dataset (nulls imputed, categorical features encoded).


Import packages

[1]:
from iguanas.rule_generation import RuleGeneratorOpt
from iguanas.metrics.classification import FScore

import random
import pandas as pd
import numpy as np

Read in data

Let’s read in some labelled, processed dummy data.

[2]:
X_train = pd.read_csv(
    'dummy_data/X_train.csv',
    index_col='eid'
)
y_train = pd.read_csv(
    'dummy_data/y_train.csv',
    index_col='eid'
).squeeze()
X_test = pd.read_csv(
    'dummy_data/X_test.csv',
    index_col='eid'
)
y_test = pd.read_csv(
    'dummy_data/y_test.csv',
    index_col='eid'
).squeeze()

Generate rules

Set up class parameters

Now we can set our class parameters for the Rule Generator. Here we’re using the F1 score as the optimisation function (you can choose a different function from the metrics.classification module or create your own).

Note that if you’re using the FScore, Precision or Recall score as the optimisation function, use the FScore, Precision or Recall classes in the metrics.classification module rather than the same functions from Sklearn’s metrics module, as the former are ~100 times faster on larger datasets.

Please see the class docstring for more information on each parameter.

[3]:
fs = FScore(beta=1)
[4]:
params = {
    'opt_func': fs.fit,
    'n_total_conditions': 4,
    'num_rules_keep': 50,
    'n_points': 10,
    'ratio_window': 2,
    'remove_corr_rules': False,
    'target_feat_corr_types': 'Infer',
    'verbose': 1
}

Instantiate class and run fit method

Once the parameters have been set, we can run the .fit() method to generate rules.

[5]:
rg = RuleGeneratorOpt(**params)
[6]:
X_rules = rg.fit(
    X=X_train,
    y=y_train,
    sample_weight=None
)
--- Generating one condition rules for numeric features ---
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 24/24 [00:00<00:00, 275.12it/s]
--- Generating one condition rules for OHE categorical features ---
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:00<00:00, 114520.25it/s]
--- Generating pairwise rules ---
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:01<00:00,  1.82it/s]

Outputs

The .fit() method returns a dataframe giving the binary columns of the generated rules as applied to the training dataset.

Useful attributes created by running the .fit() method are:

  • rule_strings: The generated rules, defined using the standard Iguanas string format (values) and their names (keys).

  • rule_descriptions: A dataframe showing the logic of the generated rules and their performance metrics as applied to the training dataset.

[7]:
X_rules.head()
[7]:
RGO_Rule_20211123_1 RGO_Rule_20211123_0 RGO_Rule_20211123_2 RGO_Rule_20211123_5 RGO_Rule_20211123_3 RGO_Rule_20211123_95 RGO_Rule_20211123_37 RGO_Rule_20211123_124 RGO_Rule_20211123_123 RGO_Rule_20211123_36 ... RGO_Rule_20211123_303 RGO_Rule_20211123_302 RGO_Rule_20211123_224 RGO_Rule_20211123_353 RGO_Rule_20211123_221 RGO_Rule_20211123_220 RGO_Rule_20211123_219 RGO_Rule_20211123_199 RGO_Rule_20211123_196 RGO_Rule_20211123_195
eid
867-8837095-9305559 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
974-5306287-3527394 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
584-0112844-9158928 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
956-4190732-7014837 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
349-7005645-8862067 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0

5 rows × 340 columns

[8]:
rg.rule_descriptions.head()
[8]:
Precision Recall PercDataFlagged OptMetric Logic nConditions
Rule
RGO_Rule_20211123_1 0.991837 1.0 0.027547 0.995902 (X['account_number_num_fraud_transactions_per_... 1
RGO_Rule_20211123_0 0.991837 1.0 0.027547 0.995902 (X['account_number_num_fraud_transactions_per_... 1
RGO_Rule_20211123_2 0.972000 1.0 0.028109 0.985801 (X['account_number_num_fraud_transactions_per_... 1
RGO_Rule_20211123_5 0.960474 1.0 0.028446 0.979839 (X['account_number_num_fraud_transactions_per_... 1
RGO_Rule_20211123_3 0.960474 1.0 0.028446 0.979839 (X['account_number_num_fraud_transactions_per_... 1

Apply rules to a separate dataset

Use the .transform() method to apply the generated rules to a separate dataset.

[9]:
X_rules_test = rg.transform(
    X=X_test,
    y=y_test,
    sample_weight=None
)

Outputs

The .transform() method returns a dataframe giving the binary columns of the rules as applied to the given dataset.

A useful attribute created by running the .transform() method is:

  • rule_descriptions: A dataframe showing the logic of the generated rules and their performance metrics as applied to the given dataset.

[10]:
rg.rule_descriptions.head()
[10]:
Precision Recall PercDataFlagged OptMetric Logic nConditions
Rule
RGO_Rule_20211123_1 0.991453 1.0 0.026700 0.995708 (X['account_number_num_fraud_transactions_per_... 1
RGO_Rule_20211123_0 0.991453 1.0 0.026700 0.995708 (X['account_number_num_fraud_transactions_per_... 1
RGO_Rule_20211123_2 0.958678 1.0 0.027613 0.978903 (X['account_number_num_fraud_transactions_per_... 1
RGO_Rule_20211123_5 0.943089 1.0 0.028069 0.970711 (X['account_number_num_fraud_transactions_per_... 1
RGO_Rule_20211123_3 0.943089 1.0 0.028069 0.970711 (X['account_number_num_fraud_transactions_per_... 1
[11]:
X_rules_test.head()
[11]:
Rule RGO_Rule_20211123_1 RGO_Rule_20211123_0 RGO_Rule_20211123_2 RGO_Rule_20211123_5 RGO_Rule_20211123_3 RGO_Rule_20211123_150 RGO_Rule_20211123_96 RGO_Rule_20211123_66 RGO_Rule_20211123_67 RGO_Rule_20211123_151 ... RGO_Rule_20211123_314 RGO_Rule_20211123_290 RGO_Rule_20211123_230 RGO_Rule_20211123_268 RGO_Rule_20211123_267 RGO_Rule_20211123_266 RGO_Rule_20211123_311 RGO_Rule_20211123_310 RGO_Rule_20211123_293 RGO_Rule_20211123_195
eid
975-8351797-7122581 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
785-6259585-7858053 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
057-4039373-1790681 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
095-5263240-3834186 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
980-3802574-0009480 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0

5 rows × 340 columns