Skip to content

Commit 084b4c0

Browse files
committed
Add basic text features sample
1 parent 5263a0b commit 084b4c0

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

doc/samples/samples.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,44 @@ Samples
948948
kh.deploy_model(
949949
model_dictionary_file_path, "SNB_Adult", data_table_path, output_data_table_path
950950
)
951+
.. autofunction:: deploy_model_text
952+
.. code-block:: python
953+
954+
# Imports
955+
import os
956+
from khiops import core as kh
957+
958+
# Set the file paths
959+
dictionary_file_path = os.path.join(
960+
kh.get_samples_dir(), "NegativeAirlineTweets", "NegativeAirlineTweets.kdic"
961+
)
962+
data_table_path = os.path.join(
963+
kh.get_samples_dir(), "NegativeAirlineTweets", "NegativeAirlineTweets.txt"
964+
)
965+
output_dir = os.path.join("kh_samples", "deploy_model")
966+
report_file_path = os.path.join(output_dir, "AnalysisResults.khj")
967+
output_data_table_path = os.path.join(output_dir, "ScoresNegativeAirlineTweets.txt")
968+
969+
# Train the predictor
970+
_, model_dictionary_file_path = kh.train_predictor(
971+
dictionary_file_path,
972+
"FlightNegativeTweets",
973+
data_table_path,
974+
"negativereason",
975+
report_file_path,
976+
max_trees=5,
977+
max_text_features=1000,
978+
text_features="words",
979+
)
980+
981+
# Deploy the model on the database
982+
# It will score it according to the trained predictor
983+
kh.deploy_model(
984+
model_dictionary_file_path,
985+
"SNB_FlightNegativeTweets",
986+
data_table_path,
987+
output_data_table_path,
988+
)
951989
.. autofunction:: deploy_model_mt
952990
.. code-block:: python
953991

khiops/samples/samples.ipynb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,57 @@
12481248
")"
12491249
]
12501250
},
1251+
{
1252+
"cell_type": "markdown",
1253+
"metadata": {},
1254+
"source": [
1255+
"### `deploy_model_text()`\n\n",
1256+
"Deploys a model learned on textual data\n It is a call to `~.api.deploy_model` with its mandatory parameters, plus\n text-specific parameters.\n\n In this example, a Selective Naive Bayes (SNB) model is deployed by applying its\n associated dictionary to the input database. The model predictions are written to\n the output database.\n \n"
1257+
]
1258+
},
1259+
{
1260+
"cell_type": "code",
1261+
"execution_count": null,
1262+
"metadata": {},
1263+
"outputs": [],
1264+
"source": [
1265+
"# Imports\n",
1266+
"import os\n",
1267+
"from khiops import core as kh\n",
1268+
"\n",
1269+
"# Set the file paths\n",
1270+
"dictionary_file_path = os.path.join(\n",
1271+
" kh.get_samples_dir(), \"NegativeAirlineTweets\", \"NegativeAirlineTweets.kdic\"\n",
1272+
")\n",
1273+
"data_table_path = os.path.join(\n",
1274+
" kh.get_samples_dir(), \"NegativeAirlineTweets\", \"NegativeAirlineTweets.txt\"\n",
1275+
")\n",
1276+
"output_dir = os.path.join(\"kh_samples\", \"deploy_model\")\n",
1277+
"report_file_path = os.path.join(output_dir, \"AnalysisResults.khj\")\n",
1278+
"output_data_table_path = os.path.join(output_dir, \"ScoresNegativeAirlineTweets.txt\")\n",
1279+
"\n",
1280+
"# Train the predictor\n",
1281+
"_, model_dictionary_file_path = kh.train_predictor(\n",
1282+
" dictionary_file_path,\n",
1283+
" \"FlightNegativeTweets\",\n",
1284+
" data_table_path,\n",
1285+
" \"negativereason\",\n",
1286+
" report_file_path,\n",
1287+
" max_trees=5,\n",
1288+
" max_text_features=1000,\n",
1289+
" text_features=\"words\",\n",
1290+
")\n",
1291+
"\n",
1292+
"# Deploy the model on the database\n",
1293+
"# It will score it according to the trained predictor\n",
1294+
"kh.deploy_model(\n",
1295+
" model_dictionary_file_path,\n",
1296+
" \"SNB_FlightNegativeTweets\",\n",
1297+
" data_table_path,\n",
1298+
" output_data_table_path,\n",
1299+
")"
1300+
]
1301+
},
12511302
{
12521303
"cell_type": "markdown",
12531304
"metadata": {},

khiops/samples/samples.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,52 @@ def train_recoder_mt_flatten():
10221022
)
10231023

10241024

1025+
def deploy_model_text():
1026+
"""Deploys a model learned on textual data
1027+
It is a call to `~.api.deploy_model` with its mandatory parameters, plus
1028+
text-specific parameters.
1029+
1030+
In this example, a Selective Naive Bayes (SNB) model is deployed by applying its
1031+
associated dictionary to the input database. The model predictions are written to
1032+
the output database.
1033+
"""
1034+
# Imports
1035+
import os
1036+
from khiops import core as kh
1037+
1038+
# Set the file paths
1039+
dictionary_file_path = os.path.join(
1040+
kh.get_samples_dir(), "NegativeAirlineTweets", "NegativeAirlineTweets.kdic"
1041+
)
1042+
data_table_path = os.path.join(
1043+
kh.get_samples_dir(), "NegativeAirlineTweets", "NegativeAirlineTweets.txt"
1044+
)
1045+
output_dir = os.path.join("kh_samples", "deploy_model")
1046+
report_file_path = os.path.join(output_dir, "AnalysisResults.khj")
1047+
output_data_table_path = os.path.join(output_dir, "ScoresNegativeAirlineTweets.txt")
1048+
1049+
# Train the predictor
1050+
_, model_dictionary_file_path = kh.train_predictor(
1051+
dictionary_file_path,
1052+
"FlightNegativeTweets",
1053+
data_table_path,
1054+
"negativereason",
1055+
report_file_path,
1056+
max_trees=5,
1057+
max_text_features=1000,
1058+
text_features="words",
1059+
)
1060+
1061+
# Deploy the model on the database
1062+
# It will score it according to the trained predictor
1063+
kh.deploy_model(
1064+
model_dictionary_file_path,
1065+
"SNB_FlightNegativeTweets",
1066+
data_table_path,
1067+
output_data_table_path,
1068+
)
1069+
1070+
10251071
def deploy_model():
10261072
"""Deploys a model in the simplest way possible
10271073
@@ -1829,6 +1875,7 @@ def build_deployed_dictionary():
18291875
train_recoder_with_multiple_parameters,
18301876
train_recoder_mt_flatten,
18311877
deploy_model,
1878+
deploy_model_text,
18321879
deploy_model_mt,
18331880
deploy_model_mt_with_interpretation,
18341881
deploy_model_mt_snowflake,

0 commit comments

Comments
 (0)