{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 👉 Use MLFlow API for Scoring"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dataset For Scoring"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" age | \n",
" sex | \n",
" bmi | \n",
" children | \n",
" smoker | \n",
" region | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 19 | \n",
" female | \n",
" 27.900 | \n",
" 0 | \n",
" yes | \n",
" southwest | \n",
"
\n",
" \n",
" 1 | \n",
" 18 | \n",
" male | \n",
" 33.770 | \n",
" 1 | \n",
" no | \n",
" southeast | \n",
"
\n",
" \n",
" 2 | \n",
" 28 | \n",
" male | \n",
" 33.000 | \n",
" 3 | \n",
" no | \n",
" southeast | \n",
"
\n",
" \n",
" 3 | \n",
" 33 | \n",
" male | \n",
" 22.705 | \n",
" 0 | \n",
" no | \n",
" northwest | \n",
"
\n",
" \n",
" 4 | \n",
" 32 | \n",
" male | \n",
" 28.880 | \n",
" 0 | \n",
" no | \n",
" northwest | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" age sex bmi children smoker region\n",
"0 19 female 27.900 0 yes southwest\n",
"1 18 male 33.770 1 no southeast\n",
"2 28 male 33.000 3 no southeast\n",
"3 33 male 22.705 0 no northwest\n",
"4 32 male 28.880 0 no northwest"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pycaret.datasets import get_data\n",
"data = get_data('insurance', verbose=False)\n",
"data.drop('charges', axis=1, inplace=True)\n",
"data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define MLFlow Scoring Function"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import mlflow\n",
"\n",
"def score_model(data, model_name, model_version):\n",
" \n",
" mlflow.set_tracking_uri(\"sqlite:///mlruns.db\")\n",
" model_uri = \"models:/{}/{}\".format(model_name, model_version)\n",
" model = mlflow.pyfunc.load_model(model_uri)\n",
" return model.predict(data)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([18894.26007319, 3698.2875344 , 6029.27157845, ...,\n",
" 2442.28835297, 2613.21866387, 28782.04091164])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"score_model(data, 'my_first_model', 1)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([16831.03017578, 2908.97255127, 4915.3686792 , ...,\n",
" 2035.52375305, 2061.27198486, 28688.36953125])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"score_model(data, 'my_first_model', 2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([25119.76880225, 4104.82323122, 6043.30438595, ...,\n",
" 4195.51717011, 1111.38184836, 36858.02738907])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"score_model(data, 'my_first_model', 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 👉 Alteratively ..."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Transformation Pipeline and Model Successfully Loaded\n"
]
}
],
"source": [
"from pycaret.regression import load_model, predict_model\n",
"l = load_model('mlruns/1/c391ad05516442eb85c5defd46c27931/artifacts/model/model')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pipeline(steps=[('dtypes',\n",
" DataTypes_Auto_infer(display_types=False,\n",
" ml_usecase='regression',\n",
" target='charges')),\n",
" ('imputer',\n",
" Simple_Imputer(categorical_strategy='not_available',\n",
" fill_value_categorical=None,\n",
" fill_value_numerical=None,\n",
" numeric_strategy='mean',\n",
" target_variable=None)),\n",
" ('new_levels1',\n",
" New_Catagorical_Levels_in_TestData(replacement_strategy='leas...\n",
" ('binn', 'passthrough'), ('rem_outliers', 'passthrough'),\n",
" ('cluster_all', 'passthrough'),\n",
" ('dummy', Dummify(target='charges')),\n",
" ('fix_perfect', Remove_100(target='charges')),\n",
" ('clean_names', Clean_Colum_Names()),\n",
" ('feature_select', 'passthrough'), ('fix_multi', 'passthrough'),\n",
" ('dfs', 'passthrough'), ('pca', 'passthrough'),\n",
" ['trained_model', GradientBoostingRegressor(random_state=123)]])\n"
]
}
],
"source": [
"print(l)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" age | \n",
" sex | \n",
" bmi | \n",
" children | \n",
" smoker | \n",
" region | \n",
" Label | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 19 | \n",
" female | \n",
" 27.900 | \n",
" 0 | \n",
" yes | \n",
" southwest | \n",
" 18894.260073 | \n",
"
\n",
" \n",
" 1 | \n",
" 18 | \n",
" male | \n",
" 33.770 | \n",
" 1 | \n",
" no | \n",
" southeast | \n",
" 3698.287534 | \n",
"
\n",
" \n",
" 2 | \n",
" 28 | \n",
" male | \n",
" 33.000 | \n",
" 3 | \n",
" no | \n",
" southeast | \n",
" 6029.271578 | \n",
"
\n",
" \n",
" 3 | \n",
" 33 | \n",
" male | \n",
" 22.705 | \n",
" 0 | \n",
" no | \n",
" northwest | \n",
" 8958.189116 | \n",
"
\n",
" \n",
" 4 | \n",
" 32 | \n",
" male | \n",
" 28.880 | \n",
" 0 | \n",
" no | \n",
" northwest | \n",
" 3900.039002 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" age sex bmi children smoker region Label\n",
"0 19 female 27.900 0 yes southwest 18894.260073\n",
"1 18 male 33.770 1 no southeast 3698.287534\n",
"2 28 male 33.000 3 no southeast 6029.271578\n",
"3 33 male 22.705 0 no northwest 8958.189116\n",
"4 32 male 28.880 0 no northwest 3900.039002"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predictions = predict_model(l, data=data)\n",
"predictions.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pycaret-dev",
"language": "python",
"name": "pycaret-dev"
},
"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.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}