Skip to content

Commit 939c824

Browse files
committed
Update relevant Core sample according to the new Rule API support
1 parent a6e6d71 commit 939c824

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

doc/samples/samples.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,17 +581,25 @@ Samples
581581
fold_index_variable.name = "FoldIndex"
582582
fold_index_variable.type = "Numerical"
583583
fold_index_variable.used = False
584-
fold_index_variable.rule = "Ceil(Product(" + str(fold_number) + ", Random()))"
585584
dictionary.add_variable(fold_index_variable)
586585
586+
# Create fold indexing rule and set it on `fold_index_variable`
587+
dictionary.set_variable_rule(
588+
fold_index_variable.name,
589+
kh.Rule("Ceil", kh.Rule("Product", fold_number, kh.Rule("Random()"))),
590+
)
591+
587592
# Add variables that indicate if the instance is in the train dataset:
588593
for fold_index in range(1, fold_number + 1):
589594
is_in_train_dataset_variable = kh.Variable()
590595
is_in_train_dataset_variable.name = "IsInTrainDataset" + str(fold_index)
591596
is_in_train_dataset_variable.type = "Numerical"
592597
is_in_train_dataset_variable.used = False
593-
is_in_train_dataset_variable.rule = "NEQ(FoldIndex, " + str(fold_index) + ")"
594598
dictionary.add_variable(is_in_train_dataset_variable)
599+
dictionary.set_variable_rule(
600+
is_in_train_dataset_variable.name,
601+
kh.Rule("NEQ", fold_index_variable, fold_index),
602+
)
595603
596604
# Print dictionary with fold variables
597605
print("Dictionary file with fold variables")

khiops/samples/samples.ipynb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,17 +788,25 @@
788788
"fold_index_variable.name = \"FoldIndex\"\n",
789789
"fold_index_variable.type = \"Numerical\"\n",
790790
"fold_index_variable.used = False\n",
791-
"fold_index_variable.rule = \"Ceil(Product(\" + str(fold_number) + \", Random()))\"\n",
792791
"dictionary.add_variable(fold_index_variable)\n",
793792
"\n",
793+
"# Create fold indexing rule and set it on `fold_index_variable`\n",
794+
"dictionary.set_variable_rule(\n",
795+
" fold_index_variable.name,\n",
796+
" kh.Rule(\"Ceil\", kh.Rule(\"Product\", fold_number, kh.Rule(\"Random()\"))),\n",
797+
")\n",
798+
"\n",
794799
"# Add variables that indicate if the instance is in the train dataset:\n",
795800
"for fold_index in range(1, fold_number + 1):\n",
796801
" is_in_train_dataset_variable = kh.Variable()\n",
797802
" is_in_train_dataset_variable.name = \"IsInTrainDataset\" + str(fold_index)\n",
798803
" is_in_train_dataset_variable.type = \"Numerical\"\n",
799804
" is_in_train_dataset_variable.used = False\n",
800-
" is_in_train_dataset_variable.rule = \"NEQ(FoldIndex, \" + str(fold_index) + \")\"\n",
801805
" dictionary.add_variable(is_in_train_dataset_variable)\n",
806+
" dictionary.set_variable_rule(\n",
807+
" is_in_train_dataset_variable.name,\n",
808+
" kh.Rule(\"NEQ\", fold_index_variable, fold_index),\n",
809+
" )\n",
802810
"\n",
803811
"# Print dictionary with fold variables\n",
804812
"print(\"Dictionary file with fold variables\")\n",

khiops/samples/samples.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,17 +649,25 @@ def train_predictor_with_cross_validation():
649649
fold_index_variable.name = "FoldIndex"
650650
fold_index_variable.type = "Numerical"
651651
fold_index_variable.used = False
652-
fold_index_variable.rule = "Ceil(Product(" + str(fold_number) + ", Random()))"
653652
dictionary.add_variable(fold_index_variable)
654653

654+
# Create fold indexing rule and set it on `fold_index_variable`
655+
dictionary.set_variable_rule(
656+
fold_index_variable.name,
657+
kh.Rule("Ceil", kh.Rule("Product", fold_number, kh.Rule("Random()"))),
658+
)
659+
655660
# Add variables that indicate if the instance is in the train dataset:
656661
for fold_index in range(1, fold_number + 1):
657662
is_in_train_dataset_variable = kh.Variable()
658663
is_in_train_dataset_variable.name = "IsInTrainDataset" + str(fold_index)
659664
is_in_train_dataset_variable.type = "Numerical"
660665
is_in_train_dataset_variable.used = False
661-
is_in_train_dataset_variable.rule = "NEQ(FoldIndex, " + str(fold_index) + ")"
662666
dictionary.add_variable(is_in_train_dataset_variable)
667+
dictionary.set_variable_rule(
668+
is_in_train_dataset_variable.name,
669+
kh.Rule("NEQ", fold_index_variable, fold_index),
670+
)
663671

664672
# Print dictionary with fold variables
665673
print("Dictionary file with fold variables")

0 commit comments

Comments
 (0)