Modules
Top-level package for Action Rules.
ActionRules
¶
Generate action rules from tabular data using one-hot encoding and bitset support counting.
Attributes:
| Name | Type | Description |
|---|---|---|
min_stable_attributes |
int
|
The minimum number of stable attributes required. |
min_flexible_attributes |
int
|
The minimum number of flexible attributes required. |
min_undesired_support |
int
|
The minimum support for the undesired state. |
min_undesired_confidence |
float
|
The minimum confidence for the undesired state. |
min_desired_support |
int
|
The minimum support for the desired state. |
min_desired_confidence |
float
|
The minimum confidence for the desired state. |
verbose |
(bool, optional)
|
If True, enables verbose output. |
rules |
(Optional[Rules], optional)
|
Stores the generated rules. |
output |
(Optional[Output], optional)
|
Stores the generated action rules. |
np |
(Optional[ModuleType], optional)
|
The numpy or cupy module used for array operations. |
pd |
(Optional[ModuleType], optional)
|
The pandas or cudf module used for DataFrame operations. |
is_gpu_np |
bool
|
Indicates whether GPU-accelerated numpy (cupy) is used. |
is_gpu_pd |
bool
|
Indicates whether GPU-accelerated pandas (cudf) is used. |
intrinsic_utility_table |
(dict, optional)
|
(attribute, value) -> float A lookup table for the intrinsic utility of each attribute-value pair. If None, no intrinsic utility is considered. |
transition_utility_table |
(dict, optional)
|
(attribute, from_value, to_value) -> float A lookup table for cost/gain of transitions between values. If None, no transition utility is considered. |
Methods:
| Name | Description |
|---|---|
fit |
Generates action rules based on the provided dataset and parameters. |
get_bindings |
Binds attributes to corresponding columns in the dataset. |
get_stop_list |
Generates a stop list to prevent certain combinations of attributes. |
get_rules |
Returns the generated action rules if available. |
predict |
Predicts recommended actions based on the provided row of data. |
Source code in src/action_rules/action_rules.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 95 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 | |
__init__(min_stable_attributes, min_flexible_attributes, min_undesired_support, min_undesired_confidence, min_desired_support, min_desired_confidence, verbose=False, intrinsic_utility_table=None, transition_utility_table=None)
¶
Initialize the ActionRules class with the specified parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_stable_attributes |
int
|
The minimum number of stable attributes required. |
required |
min_flexible_attributes |
int
|
The minimum number of flexible attributes required. |
required |
min_undesired_support |
int
|
The minimum support for the undesired state. |
required |
min_undesired_confidence |
float
|
The minimum confidence for the undesired state. |
required |
min_desired_support |
int
|
The minimum support for the desired state. |
required |
min_desired_confidence |
float
|
The minimum confidence for the desired state. |
required |
verbose |
bool
|
If True, enables verbose output. Default is False. |
False
|
intrinsic_utility_table |
dict
|
(attribute, value) -> float A lookup table for the intrinsic utility of each attribute-value pair. If None, no intrinsic utility is considered. |
None
|
transition_utility_table |
dict
|
(attribute, from_value, to_value) -> float A lookup table for cost/gain of transitions between values. If None, no transition utility is considered. |
None
|
Notes
The verbose parameter can be used to enable detailed output during the rule generation process.
Source code in src/action_rules/action_rules.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 134 135 136 137 138 139 140 141 | |
build_bit_masks(data)
¶
Pack a binary feature matrix into 64-bit masks for fast intersection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Union[ndarray, ndarray]
|
Dense matrix produced by |
required |
Returns:
| Type | Description |
|---|---|
Union[ndarray, ndarray]
|
bit_masks is a uint64 array with shape (num_attributes, num_words) holding 64 bits/transactions for each item. |
Notes
- The packing uses 64-bit little-endian words (bit 0 corresponds to the first transaction in each chunk).
- Sparse inputs are not supported; callers should densify before packing.
Source code in src/action_rules/action_rules.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
confidence_intervals(data, method='bootstrap', confidence_level=0.95, threshold=None, metric='uplift', n_bootstrap=1000, n_mc=10000, random_state=None, analytic_type='wald', bootstrap_type='percentile')
¶
Compute confidence intervals for all fitted action rules.
Applies a statistical inference engine to the provided dataset and attaches confidence interval results to the output object. Results are also returned directly for immediate inspection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Union[DataFrame, DataFrame]
|
The original (pre-encoding) dataset used for inference. Columns
must match the attribute names supplied during |
required |
method |
str
|
CI method to use. One of:
|
'bootstrap'
|
confidence_level |
float
|
Nominal coverage probability, e.g. |
0.95
|
threshold |
float
|
Decision boundary used to categorise rules after computing
intervals. When |
None
|
metric |
str
|
Metric to use for categorisation when threshold is provided.
One of |
'uplift'
|
n_bootstrap |
int
|
Number of bootstrap resamples. Only used when
|
1000
|
n_mc |
int
|
Number of Monte Carlo samples. Only used when
|
10000
|
random_state |
int
|
Seed for reproducibility. Passed to the engine when applicable.
|
None
|
analytic_type |
str
|
Sub-type of the analytic method. Only used when
|
'wald'
|
bootstrap_type |
str
|
Sub-type of the bootstrap method. Only used when
|
'percentile'
|
Returns:
| Type | Description |
|---|---|
list
|
List of :class: |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model has not been fitted yet ( |
ValueError
|
If method is not one of the supported values. |
Notes
Results are also stored on the output object via
self.output.set_confidence_intervals(results) so that subsequent
calls to get_ar_notation(), get_pretty_ar_notation(), and
get_export_notation() include the CI information.
Source code in src/action_rules/action_rules.py
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 | |
count_max_nodes(stable_items_binding, flexible_items_binding)
¶
Calculate the maximum number of nodes based on the given item bindings.
This function takes two dictionaries, stable_items_binding and flexible_items_binding,
which map attributes to lists of items. It calculates the total number of nodes by considering
all possible combinations of the lengths of these item lists and summing the product of each combination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stable_items_binding |
dict
|
A dictionary where keys are attributes and values are lists of stable items. |
required |
flexible_items_binding |
dict
|
A dictionary where keys are attributes and values are lists of flexible items. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The total number of nodes calculated by summing the product of lengths of all combinations of item lists. |
Notes
- The function first combines the lengths of item lists from both dictionaries.
- It then calculates the sum of the products of all possible combinations of these lengths.
Source code in src/action_rules/action_rules.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
cross_validate(data, stable_attributes, flexible_attributes, target, target_undesired_state, target_desired_state, *, n_splits=5, stratify=True, strategies=None, metrics=None, k_fraction=0.2, ci_method='bootstrap', n_bootstrap=500, risk_lambda=1.96, confidence_level=0.95, random_state=None, n_bootstrap_oof=1000, bootstrap_design='cluster_fold', track_stability=True, use_sparse_matrix=False, scale_support_thresholds=True, compute_insample_baseline=False)
¶
Run stratified K-fold cross-validation on the action-rule pipeline.
Each fold receives a fresh :class:ActionRules instance configured
with the same hyperparameters and utility tables as self. Per
fold, rules are mined on the train split, confidence intervals are
computed on the train split, and every discovered rule is re-scored
on the held-out test split (test_uplift, test_realistic_gain).
Targeting metrics (uplift@k, Qini, AUUC, profit@k) are
evaluated under several targeting strategies on the test split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
DataFrame
|
The full dataset, pre-encoding. |
required |
stable_attributes |
list of str
|
|
required |
flexible_attributes |
list of str
|
|
required |
target |
str
|
|
required |
target_undesired_state |
str
|
|
required |
target_desired_state |
str
|
|
required |
n_splits |
int
|
Number of folds (default |
5
|
stratify |
bool
|
Whether to stratify folds by |
True
|
strategies |
sequence of str
|
Subset of |
None
|
metrics |
sequence of str
|
Subset of |
None
|
k_fraction |
float
|
Top-k cutoff used by the |
0.2
|
ci_method |
forwarded to
|
:class: |
'bootstrap'
|
n_bootstrap |
forwarded to
|
:class: |
'bootstrap'
|
confidence_level |
forwarded to
|
:class: |
'bootstrap'
|
risk_lambda |
forwarded to
|
:class: |
'bootstrap'
|
random_state |
int
|
Seed for fold splitting and bootstrap CIs. |
None
|
n_bootstrap_oof |
int
|
Bootstrap replicates for the across-fold rule-resampling CI.
Set to |
1000
|
bootstrap_design |
str
|
|
'cluster_fold'
|
track_stability |
bool
|
Compute pairwise Jaccard overlap of discovered rule sets across
folds (default |
True
|
compute_insample_baseline |
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
CrossValidationResult
|
|
Notes
- Naive K-fold CIs based on
mean ± 1.96·std/√Kover folds have below-nominal coverage (Bates, Hastie & Tibshirani, 2021, arXiv:2104.00673). This method therefore reports fold spread (std) as a stability indicator and a stratified bootstrap CI over OOF rule records as the inferential interval. - Calling :meth:
cross_validatedoes not require the model to be fitted on the full data first. It does not mutateself; each fold operates on a fresh internal instance.
Source code in src/action_rules/action_rules.py
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 | |
df_to_array(df)
¶
Convert a one-hot DataFrame to a binary array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
Union[DataFrame, DataFrame]
|
The DataFrame to convert. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing the transposed array and the DataFrame columns. |
Notes
The data is converted to an unsigned 8-bit array (np.uint8), backed by
NumPy or CuPy depending on the selected cpu/gpu backend.
Source code in src/action_rules/action_rules.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
fit(data, stable_attributes, flexible_attributes, target, target_undesired_state, target_desired_state, use_sparse_matrix=False, use_gpu=False, **kwargs)
¶
Generate action rules for the provided dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Union[DataFrame, DataFrame]
|
The dataset to generate action rules from. |
required |
stable_attributes |
list
|
List of stable attributes. |
required |
flexible_attributes |
list
|
List of flexible attributes. |
required |
target |
str
|
The target attribute. |
required |
target_undesired_state |
str
|
The undesired state of the target attribute. |
required |
target_desired_state |
str
|
The desired state of the target attribute. |
required |
use_sparse_matrix |
bool
|
Kept for backward compatibility with action-rules <= 1.0.11. The bitset
backend supersedes sparse matrices, so this flag is accepted and ignored.
Other unrecognized keyword arguments ( |
False
|
use_gpu |
bool
|
Use GPU (cuDF) for data processing if available. Default is False. |
False
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model has already been fitted. |
Notes
The method runs one-hot encoding (when needed), packs bit masks, explores candidate branches, prunes classification rules by depth, and finally materializes action rules.
Source code in src/action_rules/action_rules.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 | |
fit_onehot(data, stable_attributes, flexible_attributes, target, target_undesired_state, target_desired_state, use_sparse_matrix=False, use_gpu=False, **kwargs)
¶
Fit the model when input data is already one-hot encoded.
The method remaps one-hot columns to the internal naming convention
(_<item_stable>_, _<item_flexible>_, _<item_target>_), drops
unrelated columns, and forwards execution to fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Union[DataFrame, DataFrame]
|
The dataset to be processed and used for fitting the model. |
required |
stable_attributes |
dict
|
A dictionary mapping stable attribute names to lists of column names corresponding to those attributes. |
required |
flexible_attributes |
dict
|
A dictionary mapping flexible attribute names to lists of column names corresponding to those attributes. |
required |
target |
dict
|
A dictionary mapping the target attribute name to a list of column names corresponding to that attribute. |
required |
target_undesired_state |
str
|
The undesired state of the target attribute, used in action rule generation. |
required |
target_desired_state |
str
|
The desired state of the target attribute, used in action rule generation. |
required |
use_sparse_matrix |
bool
|
Kept for backward compatibility with action-rules <= 1.0.11. The bitset
backend supersedes sparse matrices, so this flag is accepted and ignored.
Other unrecognized keyword arguments ( |
False
|
use_gpu |
bool
|
If True, the GPU (cuDF) is used for data processing if available. Default is False. |
False
|
Notes
This method expects boolean/binary one-hot columns.
Source code in src/action_rules/action_rules.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
get_bindings(columns, stable_attributes, flexible_attributes, target)
¶
Bind stable/flexible/target attribute to corresponding column in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns |
list
|
List of column names in the dataset. |
required |
stable_attributes |
list
|
List of stable attributes. |
required |
flexible_attributes |
list
|
List of flexible attributes. |
required |
target |
str
|
The target attribute. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing the bindings for stable attributes, flexible attributes, and target items. |
Notes
The method generates mappings from column indices to attribute values for stable, flexible, and target attributes.
Source code in src/action_rules/action_rules.py
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 | |
get_rules()
¶
Return the generated action rules if available.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model has not been fitted. |
Returns:
| Type | Description |
|---|---|
Output
|
The generated action rules. |
Notes
This method returns the Output object containing the generated action rules.
Source code in src/action_rules/action_rules.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 | |
get_split_bit_masks(target_items_binding, target)
¶
Return packed bit-mask rows for each target state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_items_binding |
dict
|
Indexes of target attributes columns in one-hot table. |
required |
target |
str
|
Name of the target attribute. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary mapping target attributes to the corresponding packed mask rows. |
Notes
Requires that build_bit_masks has been executed beforehand.
Source code in src/action_rules/action_rules.py
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | |
get_stop_list(stable_items_binding, flexible_items_binding)
¶
Generate a stop list to prevent certain combinations of attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stable_items_binding |
dict
|
Dictionary containing bindings for stable items. |
required |
flexible_items_binding |
dict
|
Dictionary containing bindings for flexible items. |
required |
Returns:
| Type | Description |
|---|---|
list
|
A list of stop combinations. |
Notes
The stop list is generated by creating pairs of stable item indices and ensuring flexible items do not repeat.
Source code in src/action_rules/action_rules.py
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
one_hot_encode(data, stable_attributes, flexible_attributes, target)
¶
Perform one-hot encoding on the attributes of the DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Union[DataFrame, DataFrame]
|
The input DataFrame containing the data to be encoded. |
required |
stable_attributes |
list
|
List of stable attributes to be one-hot encoded. |
required |
flexible_attributes |
list
|
List of flexible attributes to be one-hot encoded. |
required |
target |
str
|
The target attribute to be one-hot encoded. |
required |
Returns:
| Type | Description |
|---|---|
Union[DataFrame, DataFrame]
|
A DataFrame with the specified attributes one-hot encoded. |
Notes
Stable and flexible (antecedent) columns are cast to strings only for non-missing values; NaN is
preserved so that pd.get_dummies skips it instead of creating a phantom <attr>_<item_*>_nan
category. This implements the pessimistic interpretation of null values in incomplete information
systems --- a missing antecedent does not match any value-specific itemset and therefore cannot appear
in a discovered rule --- as defined for action-rule mining by Dardzinska, Action Rules Mining
(Springer 2013, Section 2.3.2). The target column is cast to strings in full so that any NaN
target value becomes its own explicit category (downstream get_split_bit_masks will exclude it
from both the undesired and desired splits, which is the intended behaviour when callers want to
ignore unlabelled rows).
Source code in src/action_rules/action_rules.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
predict(frame_row)
¶
Predict recommended actions based on the provided row of data.
This method applies the fitted action rules to the given row of data and generates a DataFrame with recommended actions if any of the action rules are triggered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_row |
Union[Series, Series]
|
A row of data in the form of a cuDF or pandas Series. The Series should contain the features required by the action rules. |
required |
Returns:
| Type | Description |
|---|---|
Union[DataFrame, DataFrame]
|
A DataFrame with the recommended actions. The DataFrame includes the following columns: - The original attributes with recommended changes. - 'ActionRules_RuleIndex': Index of the action rule applied. - 'ActionRules_UndesiredSupport': Support of the undesired part of the rule. - 'ActionRules_DesiredSupport': Support of the desired part of the rule. - 'ActionRules_UndesiredConfidence': Confidence of the undesired part of the rule. - 'ActionRules_DesiredConfidence': Confidence of the desired part of the rule. - 'ActionRules_Uplift': Uplift value of the rule. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model has not been fitted. |
Notes
The method compares the given row of data against the undesired itemsets of the action rules. If a match is found, it applies the desired itemset changes and records the action rule's metadata. The result is a DataFrame with one or more rows representing the recommended actions for the given data.
Source code in src/action_rules/action_rules.py
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 | |
remap_utility_tables(column_values)
¶
Remap the keys of intrinsic and transition utility tables using the provided column mapping.
The function uses column_values, a dictionary mapping internal column indices to
(attribute, value) tuples, to invert the mapping so that utility table keys are replaced
with the corresponding integer index (for intrinsic utilities) or a tuple of integer indices
(for transition utilities).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column_values |
dict
|
Dictionary mapping integer column indices to (attribute, value) pairs. Example: {0: ('Age', 'O'), 1: ('Age', 'Y'), 2: ('Sex', 'F'), ...} |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple (remapped_intrinsic, remapped_transition) where: - remapped_intrinsic is a dict mapping integer column index to utility value. - remapped_transition is a dict mapping (from_index, to_index) to utility value. |
Notes
- The method performs case-insensitive matching by converting attribute names and values to lowercase.
- If a key in a utility table does not have a corresponding entry in column_values, it is skipped.
Source code in src/action_rules/action_rules.py
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 | |
set_array_library(use_gpu, df)
¶
Set the appropriate array and DataFrame libraries (cuDF or pandas) based on the user's preference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_gpu |
bool
|
Indicates whether to use GPU (cuDF) for data processing if available. |
required |
df |
Union[DataFrame, DataFrame]
|
The DataFrame to convert. |
required |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Warnings
UserWarning
If use_gpu is True but cuDF is not available, a warning is issued indicating fallback to pandas.
Notes
This method determines whether to use GPU-accelerated libraries for processing data, falling back to CPU-based libraries if necessary.
Source code in src/action_rules/action_rules.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
ConfidenceIntervalResult
dataclass
¶
Confidence interval result for a single action rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule_index |
int
|
Zero-based index of the action rule in the source |
required |
method |
str
|
Name of the CI method used: |
required |
confidence_level |
float
|
Nominal coverage probability, e.g. |
required |
uplift_point |
float
|
Point estimate of the uplift measure. |
required |
uplift_ci_lower |
float
|
Lower bound of the uplift confidence interval. |
required |
uplift_ci_upper |
float
|
Upper bound of the uplift confidence interval. |
required |
uplift_se |
float
|
Standard error of the uplift estimate. |
required |
realistic_rule_gain_point |
float
|
Point estimate of the realistic rule gain (only when utility tables are given). |
None
|
realistic_rule_gain_ci_lower |
float
|
Lower bound of the realistic rule gain CI. |
None
|
realistic_rule_gain_ci_upper |
float
|
Upper bound of the realistic rule gain CI. |
None
|
realistic_rule_gain_se |
float
|
Standard error of the realistic rule gain. |
None
|
support |
int
|
Transaction support of the action rule. |
0
|
confidence |
float
|
Confidence of the action rule. |
0.0
|
category |
RuleCategory
|
Qualitative verdict derived from the uplift CI vs. a threshold. |
None
|
undefined_bootstrap_fraction |
float
|
For the bootstrap engine: fraction of resamples that hit a degenerate
configuration ( |
None
|
samples_uplift |
ndarray
|
Raw bootstrap/posterior samples for uplift (excluded from repr to keep it brief). |
None
|
samples_gain |
ndarray
|
Raw bootstrap/posterior samples for gain (excluded from repr to keep it brief). |
None
|
Source code in src/action_rules/inference/base.py
87 88 89 90 91 92 93 94 95 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
to_dict(include_samples=False)
¶
Return a plain dict representation of this result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_samples |
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Keys mirror the dataclass fields. |
Source code in src/action_rules/inference/base.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
Input
¶
A class used to import action rules.
Methods:
| Name | Description |
|---|---|
import_action_rules |
Import action rules from a JSON string and set the action_rules attribute. |
Source code in src/action_rules/input/input.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 95 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
__init__()
¶
Initialize the Input class.
Notes
This class is used to import action rules from a JSON string and convert them into an Output object.
Source code in src/action_rules/input/input.py
18 19 20 21 22 23 24 25 | |
import_action_rules(json_data)
¶
Import action rules from a JSON string and set the action_rules attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_data |
str
|
JSON string representing the action rules. |
required |
Returns:
| Type | Description |
|---|---|
Output
|
Output object representing the action rules. |
Notes
This method parses a JSON string containing action rules, extracts relevant information, and constructs an Output object. The method initializes the target attribute, stable items, flexible items, and column values. It processes both stable and flexible items for each rule and updates the corresponding dictionaries.
The JSON structure is expected to have the following format: [ { "target": { "attribute": "target_attribute", "undesired": "undesired_value", "desired": "desired_value" }, "support of undesired part": int, "confidence of undesired part": float, "support of desired part": int, "confidence of desired part": float, "uplift": float, "max_rule_gain": float, (optional) "realistic_rule_gain": float, (optional) "realistic_dataset_gain": float, (optional) "stable": [ {"attribute": "attribute_name", "value": "attribute_value"}, ... ], "flexible": [ {"attribute": "attribute_name", "undesired": "undesired_value", "desired": "desired_value"}, ... ] }, ... ]
The method ensures that each attribute-value pair is assigned a unique index and maintains the mappings in the column_values dictionary. The stable_items_binding and flexible_items_binding dictionaries are updated accordingly.
Example
json_data = ''' [ { "target": { "attribute": "target", "undesired": "no", "desired": "yes" }, "support of undesired part": 10, "confidence of undesired part": 0.5, "support of desired part": 20, "confidence of desired part": 0.8, "uplift": 0.3, "stable": [ {"attribute": "age", "value": "young"}, {"attribute": "income", "value": "high"} ], "flexible": [ {"attribute": "education", "undesired": "low", "desired": "high"} ] } ] ''' input_obj = Input() output = input_obj.import_action_rules(json_data)
Source code in src/action_rules/input/input.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 95 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
Output
¶
A class used to format and export action rules.
Attributes:
| Name | Type | Description |
|---|---|---|
action_rules |
list
|
List containing the action rules. |
target |
str
|
The target attribute for the action rules. |
stable_cols |
list
|
List of indices for stable columns. |
flexible_cols |
list
|
List of indices for flexible columns. |
column_values |
dict
|
Dictionary containing the values of the columns. |
Methods:
| Name | Description |
|---|---|
get_ar_notation |
Generate a string representation of the action rules in a human-readable format. |
get_export_notation |
Generate a JSON string of dictionaries representing the action rules for export. |
get_pretty_ar_notation |
Generate a list of text strings representing the action rules. |
Source code in src/action_rules/output/output.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 95 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
__init__(action_rules, target, stable_items_binding, flexible_items_binding, column_values)
¶
Initialize the Output class with the specified action rules and target attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action_rules |
list
|
List containing the action rules. |
required |
target |
str
|
The target attribute for the action rules. |
required |
stable_items_binding |
dict
|
Dictionary containing bindings for stable items. |
required |
flexible_items_binding |
dict
|
Dictionary containing bindings for flexible items. |
required |
column_values |
dict
|
Dictionary containing the values of the columns. |
required |
Notes
The constructor initializes the Output object by setting the provided action rules, target attribute, stable items, flexible items, and column values. It flattens the stable and flexible items bindings to create lists of indices for stable and flexible columns.
Source code in src/action_rules/output/output.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 | |
get_ar_notation()
¶
Generate a string representation of the action rules in a human-readable format.
Returns:
| Type | Description |
|---|---|
str
|
String representation of the action rules. |
Notes
This method constructs a human-readable string representation of the action rules. Each rule is formatted to show the attribute-value conditions and transitions. The representation includes the support and confidence values for both the undesired and desired parts, as well as the uplift.
Source code in src/action_rules/output/output.py
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
get_dominant_rules()
¶
Identify and select the dominant (Pareto-optimal) action rules.
This method compares action rules based on the union of their 'undesired' and 'desired' itemsets, as well as their 'uplift' values. It applies a Pareto-dominance approach:
- If the new candidate rule is a superset of a current dominant rule with smaller or equal uplift, the candidate is dominated and not added.
- If the new candidate rule is a subset of a current dominant rule with larger or equal uplift, the current dominant rule is dominated and removed.
- Otherwise, the new candidate is added to the set of dominant rules.
After processing all rules, the remaining dominant rules are sorted by 'uplift' in descending order, and the method returns their indices.
Returns:
| Type | Description |
|---|---|
list
|
A list of indices representing the dominant (Pareto-optimal) action rules, sorted by uplift in descending order. |
Source code in src/action_rules/output/output.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
get_export_notation()
¶
Generate a JSON string of dictionaries representing the action rules for export.
Returns:
| Type | Description |
|---|---|
str
|
JSON string of dictionaries representing the action rules. |
Notes
This method constructs a list of dictionaries where each dictionary represents an action rule. The dictionaries include attributes for stable and flexible items, as well as the target attribute, support, confidence, and uplift values. The list is then converted to a JSON string for export.
Source code in src/action_rules/output/output.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
get_pretty_ar_notation()
¶
Generate a list of text strings representing the action rules.
Returns:
| Type | Description |
|---|---|
list
|
List of text strings representing the action rules. |
Notes
This method constructs a list of text strings where each string represents an action rule in a readable format. The format includes conditions and transitions for each attribute, along with the target attribute change, support, confidence, and uplift values.
Source code in src/action_rules/output/output.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
set_confidence_intervals(results)
¶
Store confidence interval results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results |
list
|
List of ConfidenceIntervalResult objects. |
required |
Source code in src/action_rules/output/output.py
92 93 94 95 96 97 98 99 100 | |
RuleCategory
¶
Bases: Enum
Classification of an action rule based on its confidence interval.
Attributes:
| Name | Type | Description |
|---|---|---|
ACCEPT |
str
|
The entire CI is at or above the threshold — rule is reliable. |
REJECT |
str
|
The entire CI is below the threshold — rule should be discarded. |
UNCERTAIN |
str
|
The CI straddles the threshold — insufficient evidence to decide. |
Source code in src/action_rules/inference/base.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
RuleMasks
dataclass
¶
Structured representation of a single action rule for data-driven inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_undesired |
dict
|
Mapping of {attribute: value} for conditions in the undesired classification rule (stable attributes at their fixed value plus flexible attributes at their undesired value). |
required |
mask_desired |
dict
|
Mapping of {attribute: value} for conditions in the desired classification rule (stable attributes at their fixed value plus flexible attributes at their desired value). |
required |
target_attribute |
str
|
Name of the target column. |
required |
target_undesired |
str
|
Target value representing the undesired outcome. |
required |
target_desired |
str
|
Target value representing the desired outcome. |
required |
rule_index |
int
|
Zero-based position of this rule in the original |
required |
undesired_itemset |
tuple
|
Original integer column indices that form the undesired itemset. Preserved so that utility tables (keyed by integer index) can be looked up without re-encoding. |
required |
desired_itemset |
tuple
|
Original integer column indices that form the desired itemset. |
required |
Source code in src/action_rules/inference/base.py
47 48 49 50 51 52 53 54 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 | |
Rules
¶
A class used to manage and generate classification and action rules.
Attributes:
| Name | Type | Description |
|---|---|---|
classification_rules |
defaultdict
|
Default dictionary to store classification rules for undesired and desired states. |
undesired_state |
str
|
The undesired state of the target attribute. |
desired_state |
str
|
The desired state of the target attribute. |
columns |
list
|
List of columns in the dataset. |
action_rules |
list
|
List to store generated action rules. |
undesired_prefixes_without_conf |
set
|
Set to store prefixes of undesired states without conflicts. |
desired_prefixes_without_conf |
set
|
Set to store prefixes of desired states without conflicts. |
count_transactions |
int
|
The number of transactions in the data. |
intrinsic_utility_table |
(dict, optional)
|
(attribute, value) -> float A lookup table for the intrinsic utility of each attribute-value pair. If None, no intrinsic utility is considered. |
transition_utility_table |
(dict, optional)
|
(attribute, from_value, to_value) -> float A lookup table for cost/gain of transitions between values. If None, no transition utility is considered. |
Methods:
| Name | Description |
|---|---|
add_prefix_without_conf |
Add a prefix to the set of prefixes without conflicts. |
add_classification_rules |
Add classification rules for undesired and desired states. |
generate_action_rules |
Generate action rules from classification rules. |
prune_classification_rules |
Prune classification rules based on their length and update the stop list. |
calculate_confidence |
Calculate the confidence of a rule. |
calculate_uplift |
Calculate the uplift of an action rule. |
Source code in src/action_rules/rules/rules.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 95 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
__init__(undesired_state, desired_state, columns, count_transactions, intrinsic_utility_table=None, transition_utility_table=None)
¶
Initialize the Rules class with the specified undesired and desired states, columns, and transaction count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
undesired_state |
str
|
The undesired state of the target attribute. |
required |
desired_state |
str
|
The desired state of the target attribute. |
required |
columns |
list
|
List of columns in the dataset. |
required |
count_transactions |
int
|
The number of transactions in the data. |
required |
intrinsic_utility_table |
dict
|
(attribute, value) -> float A lookup table for the intrinsic utility of each attribute-value pair. If None, no intrinsic utility is considered. |
None
|
transition_utility_table |
dict
|
(attribute, from_value, to_value) -> float A lookup table for cost/gain of transitions between values. If None, no transition utility is considered. |
None
|
Notes
The classification_rules attribute is initialized as a defaultdict with a lambda function that creates dictionaries for 'desired' and 'undesired' states.
Source code in src/action_rules/rules/rules.py
54 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 95 96 97 98 99 100 | |
add_classification_rules(new_ar_prefix, itemset_prefix, undesired_states, desired_states)
¶
Add classification rules for undesired and desired states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_ar_prefix |
tuple
|
Prefix of the action rule. |
required |
itemset_prefix |
tuple
|
Prefix of the itemset. |
required |
undesired_states |
list
|
List of dictionaries containing undesired state information. |
required |
desired_states |
list
|
List of dictionaries containing desired state information. |
required |
Notes
This method updates the classification_rules attribute with new rules based on the provided undesired and desired states. Each state is represented as a dictionary containing item, support, confidence, and target information.
Source code in src/action_rules/rules/rules.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
add_prefix_without_conf(prefix, is_desired)
¶
Add a prefix to the set of prefixes without conflicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix |
tuple
|
The prefix to be added. |
required |
is_desired |
bool
|
If True, add the prefix to the desired prefixes set; otherwise, add it to the undesired prefixes set. |
required |
Notes
This method is useful for keeping track of prefixes that have no conflicting rules and can be used directly in rule generation.
Source code in src/action_rules/rules/rules.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
calculate_confidence(support, opposite_support)
¶
Calculate the confidence of a rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
support |
int
|
The support value for the desired or undesired state. |
required |
opposite_support |
int
|
The support value for the opposite state. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The confidence value calculated as support / (support + opposite_support). Returns 0 if the sum of support and opposite_support is 0. |
Notes
Confidence is a measure of the reliability of a rule. A higher confidence indicates a stronger association between the conditions of the rule and the target state.
Source code in src/action_rules/rules/rules.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
calculate_uplift(undesired_support, undesired_confidence, desired_confidence)
¶
Calculate the uplift of an action rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
undesired_support |
int
|
The support value for the undesired state. |
required |
undesired_confidence |
float
|
The confidence value for the undesired state. |
required |
desired_confidence |
float
|
The confidence value for the desired state. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The uplift value calculated as: ((desired_confidence - (1 - undesired_confidence)) * (undesired_support / undesired_confidence)) / self.count_transactions. |
Notes
Uplift measures the increase in the probability of achieving the desired state when applying the action rule compared to not applying it. It is used to assess the effectiveness of the rule.
Source code in src/action_rules/rules/rules.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
compute_action_rule_measures(support_undesired, confidence_undesired, support_desired, confidence_desired)
¶
Compute the support and confidence for an action rule formed from an undesired rule and a desired rule.
The action rule is derived by pairing a classification rule that leads to an undesired outcome with a classification rule that leads to a desired outcome. In this formulation, the support of the action rule is defined as the minimum of the supports of the two component rules, and the confidence of the action rule is defined as the product of their confidences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
support_undesired |
float
|
The support of the undesired rule (e.g., count or relative frequency). |
required |
confidence_undesired |
float
|
The confidence of the undesired rule (a value between 0 and 1). |
required |
support_desired |
float
|
The support of the desired rule. |
required |
confidence_desired |
float
|
The confidence of the desired rule (a value between 0 and 1). |
required |
Returns:
| Type | Description |
|---|---|
tuple of (float, float)
|
A tuple containing: - action_support : float The support of the action rule, computed as min(support_undesired, support_desired). - action_confidence : float The confidence of the action rule, computed as confidence_undesired * confidence_desired. |
Source code in src/action_rules/rules/rules.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
compute_rule_utility(undesired_rule, desired_rule)
¶
Compute various utility gains for a rule transition from undesired to desired.
The function computes intrinsic utilities for items in both the undesired and desired rule itemsets, calculates a transition gain for changes in flexible attributes, and adjusts these gains using target state utilities and rule confidences to derive realistic gain metrics at both the rule and dataset levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
undesired_rule |
dict
|
Dictionary representing the undesired rule. Expected keys: - 'itemset': list of item indices in the undesired rule. - 'confidence': (optional) confidence level of the undesired rule. - 'support': (optional) support count of the undesired rule. |
required |
desired_rule |
dict
|
Dictionary representing the desired rule. Expected keys: - 'itemset': list of item indices in the desired rule. - 'confidence': (optional) confidence level of the desired rule. |
required |
Returns:
| Type | Description |
|---|---|
tuple of (float, float, float)
|
|
Source code in src/action_rules/rules/rules.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | |
generate_action_rules()
¶
Generate action rules from classification rules.
Notes
This method creates action rules by combining classification rules for undesired and desired states.
The uplift for each action rule is calculated using the calculate_uplift method and the result is
stored in the action_rules attribute.
Source code in src/action_rules/rules/rules.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
prune_classification_rules(k, stop_list)
¶
Prune classification rules based on their length and update the stop list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
int
|
Length of the attribute prefix. |
required |
stop_list |
list
|
List of prefixes to stop generating rules for. |
required |
Notes
This method removes classification rules whose prefix length equals k and either desired or undesired states are empty. The corresponding prefixes are also added to the stop_list to avoid further rule generation.
Source code in src/action_rules/rules/rules.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |