Files
wehub-resource-sync 593b94c120
pytest / Unit Tests (push) Has been cancelled
pytest / Integration (integration_tests_a) (push) Has been cancelled
pytest / Integration (integration_tests_b) (push) Has been cancelled
pytest / Integration (integration_tests_c) (push) Has been cancelled
pytest / Integration (integration_tests_d) (push) Has been cancelled
pytest / Integration (integration_tests_e) (push) Has been cancelled
pytest / Integration (integration_tests_f) (push) Has been cancelled
pytest / Integration (integration_tests_g) (push) Has been cancelled
pytest / Integration (integration_tests_h) (push) Has been cancelled
pytest / Integration (integration_tests_i) (push) Has been cancelled
pytest / Integration (integration_tests_j) (push) Has been cancelled
pytest / Distributed (distributed_a) (push) Has been cancelled
pytest / Distributed (distributed_b) (push) Has been cancelled
pytest / Distributed (distributed_c) (push) Has been cancelled
pytest / Distributed (distributed_d) (push) Has been cancelled
pytest / Distributed (distributed_e) (push) Has been cancelled
pytest / Distributed (distributed_f) (push) Has been cancelled
pytest / Minimal Install (push) Has been cancelled
pytest / Event File (push) Has been cancelled
pytest (slow) / py-slow (push) Has been cancelled
Publish JSON Schema / publish-schema (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:49:20 +08:00

273 lines
6.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"import os\n",
"import shutil\n",
"from pprint import pprint\n",
"\n",
"import pandas as pd\n",
"\n",
"from ludwig.api import LudwigModel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Receive data for training"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_df = pd.read_csv(\"./data/winequalityN.csv\")\n",
"train_df[\"quality\"] = train_df[\"quality\"].apply(str)\n",
"train_df.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Replace white space in column names with underscore\n",
"new_col = []\n",
"for i in range(len(train_df.columns)):\n",
" new_col.append(train_df.columns[i].replace(\" \", \"_\"))\n",
"\n",
"train_df.columns = new_col"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_df.describe().T"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_df.dtypes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_df[\"quality\"].value_counts().sort_index()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cols = list(set(train_df.columns) - set([\"quality\"]))\n",
"features = train_df[cols]\n",
"\n",
"# extract categorical features\n",
"categorical_features = []\n",
"for p in features:\n",
" if train_df[p].dtype == \"object\":\n",
" categorical_features.append(p)\n",
"\n",
"print(\"categorical features:\", categorical_features, \"\\n\")\n",
"\n",
"# get numerical features\n",
"numerical_features = list(set(features) - set(categorical_features))\n",
"\n",
"print(\"numerical features:\", numerical_features, \"\\n\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for feature in categorical_features:\n",
" print(f\"# of distinct values in categorical feature '{feature}' : {train_df[feature].nunique()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create Ludwig Config"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# template for config\n",
"config = {\"input_features\": [], \"output_features\": [], \"trainer\": {}}\n",
"\n",
"# setup input features for categorical features\n",
"for p in categorical_features:\n",
" a_feature = {\"name\": p.replace(\" \", \"_\"), \"type\": \"category\"}\n",
" config[\"input_features\"].append(a_feature)\n",
"\n",
"# setup input features for numerical features\n",
"for p in numerical_features:\n",
" a_feature = {\"name\": p.replace(\" \", \"_\"), \"type\": \"number\"}\n",
" config[\"input_features\"].append(a_feature)\n",
"\n",
"# set up output variable\n",
"config[\"output_features\"].append({\"name\": \"quality\", \"type\": \"category\"})\n",
"\n",
"# set default preprocessing and encoder for numerical features\n",
"config[\"defaults\"] = {\n",
" \"number\": {\n",
" \"preprocessing\": {\"missing_value_strategy\": \"fill_with_mean\", \"normalization\": \"zscore\"},\n",
" \"encoder\": {\"type\": \"dense\", \"num_layers\": 2},\n",
" },\n",
" \"category\": {\"encoder\": {\"type\": \"sparse\"}, \"decoder\": {\"top_k\": 2}, \"loss\": {\"confidence_penalty\": 0.1}},\n",
"}\n",
"\n",
"# set up trainer\n",
"config[\"trainer\"] = {\"epochs\": 5}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pprint(config, indent=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initialize and Train LudwigModel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = LudwigModel(config, backend=\"local\", logging_level=logging.INFO)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inspecting Config After Model Initialization"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pprint(model.config[\"input_features\"], indent=2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pprint(model.config[\"output_features\"], indent=2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eval_stats, train_stats, _, _ = model.experiment(dataset=train_df, experiment_name=\"wine_quality\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cleanup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" shutil.rmtree(\"./results\")\n",
" items = os.listdir(\"./\")\n",
" for item in items:\n",
" if item.endswith(\".hdf5\") or item.endswith(\".json\") or item == \".lock_preprocessing\":\n",
" os.remove(os.path.join(\"./\", item))\n",
"except Exception:\n",
" pass"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.13 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}