|
908 | 908 | "print(f\"The interpretation model is '{interpretor_file_path}'\")" |
909 | 909 | ] |
910 | 910 | }, |
| 911 | + { |
| 912 | + "cell_type": "markdown", |
| 913 | + "metadata": {}, |
| 914 | + "source": [ |
| 915 | + "### `reinforce_predictor()`\n\n", |
| 916 | + "Builds reinforced predictor for existing predictor\n\n The reinforced predictor produces the following reinforcement variables for the\n specified target value to reinforce (i.e. whose probability of occurrence is\n tentatively increased):\n\n - initial score, containing the conditional probability of the target value before\n reinforcement\n - four variables are output in decreasing reinforcement value: name of the lever\n variable, reinforcement part, final score after reinforcement, and class change\n tag.\n\n It calls `~.api.train_predictor` and `~.api.reinforce_predictor` only with\n their mandatory parameters.\n \n" |
| 917 | + ] |
| 918 | + }, |
| 919 | + { |
| 920 | + "cell_type": "code", |
| 921 | + "execution_count": null, |
| 922 | + "metadata": {}, |
| 923 | + "outputs": [], |
| 924 | + "source": [ |
| 925 | + "# Imports\n", |
| 926 | + "import os\n", |
| 927 | + "from khiops import core as kh\n", |
| 928 | + "\n", |
| 929 | + "dictionary_file_path = os.path.join(kh.get_samples_dir(), \"Adult\", \"Adult.kdic\")\n", |
| 930 | + "data_table_path = os.path.join(kh.get_samples_dir(), \"Adult\", \"Adult.txt\")\n", |
| 931 | + "output_dir = os.path.join(\"kh_samples\", \"reinforce_predictor\")\n", |
| 932 | + "analysis_report_file_path = os.path.join(output_dir, \"AnalysisResults.khj\")\n", |
| 933 | + "reinforced_predictor_file_path = os.path.join(output_dir, \"ReinforcedAdultModel.kdic\")\n", |
| 934 | + "\n", |
| 935 | + "# Build prediction model\n", |
| 936 | + "_, predictor_file_path = kh.train_predictor(\n", |
| 937 | + " dictionary_file_path,\n", |
| 938 | + " \"Adult\",\n", |
| 939 | + " data_table_path,\n", |
| 940 | + " \"class\",\n", |
| 941 | + " analysis_report_file_path,\n", |
| 942 | + ")\n", |
| 943 | + "\n", |
| 944 | + "# Build reinforced predictor\n", |
| 945 | + "kh.reinforce_predictor(\n", |
| 946 | + " predictor_file_path,\n", |
| 947 | + " \"SNB_Adult\",\n", |
| 948 | + " reinforced_predictor_file_path,\n", |
| 949 | + " reinforcement_lever_variables=[\"occupation\"],\n", |
| 950 | + ")\n", |
| 951 | + "\n", |
| 952 | + "print(f\"The reinforced predictor is '{reinforced_predictor_file_path}'\")" |
| 953 | + ] |
| 954 | + }, |
911 | 955 | { |
912 | 956 | "cell_type": "markdown", |
913 | 957 | "metadata": {}, |
|
1454 | 1498 | ")" |
1455 | 1499 | ] |
1456 | 1500 | }, |
| 1501 | + { |
| 1502 | + "cell_type": "markdown", |
| 1503 | + "metadata": {}, |
| 1504 | + "source": [ |
| 1505 | + "### `deploy_reinforced_model_mt()`\n\n", |
| 1506 | + "Deploys a multi-table reinforced model in the simplest way possible\n\n It is a call to `~.api.deploy_model` with additional parameters related to\n the lever variables.\n\n In this example, a reinforced Selective Naive Bayes (SNB) model is\n deployed by applying its associated dictionary to the input database.\n The reinforced model predictions are written to the output data table.\n \n" |
| 1507 | + ] |
| 1508 | + }, |
| 1509 | + { |
| 1510 | + "cell_type": "code", |
| 1511 | + "execution_count": null, |
| 1512 | + "metadata": {}, |
| 1513 | + "outputs": [], |
| 1514 | + "source": [ |
| 1515 | + "# Imports\n", |
| 1516 | + "import os\n", |
| 1517 | + "from khiops import core as kh\n", |
| 1518 | + "\n", |
| 1519 | + "# Set the file paths\n", |
| 1520 | + "accidents_dir = os.path.join(kh.get_samples_dir(), \"AccidentsSummary\")\n", |
| 1521 | + "dictionary_file_path = os.path.join(accidents_dir, \"Accidents.kdic\")\n", |
| 1522 | + "accidents_table_path = os.path.join(accidents_dir, \"Accidents.txt\")\n", |
| 1523 | + "vehicles_table_path = os.path.join(accidents_dir, \"Vehicles.txt\")\n", |
| 1524 | + "output_dir = os.path.join(\"kh_samples\", \"deploy_reinforced_model_mt\")\n", |
| 1525 | + "report_file_path = os.path.join(output_dir, \"AnalysisResults.khj\")\n", |
| 1526 | + "reinforced_predictor_file_path = os.path.join(output_dir, \"ReinforcedModel.kdic\")\n", |
| 1527 | + "output_data_table_path = os.path.join(output_dir, \"ReinforcedAccidents.txt\")\n", |
| 1528 | + "\n", |
| 1529 | + "# Train the predictor (see train_predictor_mt for details)\n", |
| 1530 | + "_, model_dictionary_file_path = kh.train_predictor(\n", |
| 1531 | + " dictionary_file_path,\n", |
| 1532 | + " \"Accident\",\n", |
| 1533 | + " accidents_table_path,\n", |
| 1534 | + " \"Gravity\",\n", |
| 1535 | + " report_file_path,\n", |
| 1536 | + " additional_data_tables={\"Vehicles\": vehicles_table_path},\n", |
| 1537 | + " max_trees=0,\n", |
| 1538 | + ")\n", |
| 1539 | + "\n", |
| 1540 | + "# Reinforce the predictor\n", |
| 1541 | + "kh.reinforce_predictor(\n", |
| 1542 | + " model_dictionary_file_path,\n", |
| 1543 | + " \"SNB_Accident\",\n", |
| 1544 | + " reinforced_predictor_file_path,\n", |
| 1545 | + " reinforcement_target_value=\"NonLethal\",\n", |
| 1546 | + " reinforcement_lever_variables=[\"InAgglomeration\", \"CollisionType\"],\n", |
| 1547 | + ")\n", |
| 1548 | + "\n", |
| 1549 | + "# Deploy the reinforced model on the database\n", |
| 1550 | + "# Besides the mandatory parameters, it is specified:\n", |
| 1551 | + "# - A python dictionary linking data paths to file paths for non-root tables\n", |
| 1552 | + "kh.deploy_model(\n", |
| 1553 | + " reinforced_predictor_file_path,\n", |
| 1554 | + " \"Reinforcement_SNB_Accident\",\n", |
| 1555 | + " accidents_table_path,\n", |
| 1556 | + " output_data_table_path,\n", |
| 1557 | + " additional_data_tables={\"Vehicles\": vehicles_table_path},\n", |
| 1558 | + ")" |
| 1559 | + ] |
| 1560 | + }, |
1457 | 1561 | { |
1458 | 1562 | "cell_type": "markdown", |
1459 | 1563 | "metadata": {}, |
|
0 commit comments