{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
agesexbmichildrensmokerregion
019female27.9000yessouthwest
118male33.7701nosoutheast
228male33.0003nosoutheast
333male22.7050nonorthwest
432male28.8800nonorthwest
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
agesexbmichildrensmokerregionLabel
019female27.9000yessouthwest18894.260073
118male33.7701nosoutheast3698.287534
228male33.0003nosoutheast6029.271578
333male22.7050nonorthwest8958.189116
432male28.8800nonorthwest3900.039002
\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 }