From 60a217d8018e39a1583fdad82784998874c3b88f Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 6 May 2022 12:42:19 -0400 Subject: [PATCH] Steam-DAI-MLOps tutorial --- Steam-DAI-MLOPS.ipynb | 1091 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1091 insertions(+) create mode 100644 Steam-DAI-MLOPS.ipynb diff --git a/Steam-DAI-MLOPS.ipynb b/Steam-DAI-MLOPS.ipynb new file mode 100644 index 0000000..0994bec --- /dev/null +++ b/Steam-DAI-MLOPS.ipynb @@ -0,0 +1,1091 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "aa504b61", + "metadata": {}, + "source": [ + "## Prerequisites\n", + "This tutorial relies on the latest Steam SDK (1.8.11) which can be installed into a python environment by:\n", + "\n", + "1. Click on My AI Engines from the H2O AI Cloud and then `Python client` to download the wheel file\n", + "2. Navigate to the location where the python client was downloaded and install the client using `pip install h2osteam-1.8.11-py2.py3-none-any.whl`\n", + "\n", + "We require the `h2o_authn` library for securely connecting to the H2O AI Cloud platform: `pip install h2o_authn`.\n", + "\n", + "We also set the following variables to connect to a specific H2O AI Cloud environment. They can be found by logging into the platform, clicking on your name, and choosing the `CLI & API Access` page. Then, copy values from the `Accessing H2O AI Cloud APIs` section." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "9f7ccd7b", + "metadata": {}, + "outputs": [], + "source": [ + "CLIENT_ID = \"q8s-internal-platform\"\n", + "TOKEN_ENDPOINT = \"https://auth.demo.h2o.ai/auth/realms/q8s-internal/protocol/openid-connect/token\"\n", + "REFRESH_TOKEN = \"https://cloud-internal.h2o.ai/auth/get-platform-token\"\n", + "\n", + "H2O_STEAM_URL = \"https://steam.cloud-internal.h2o.ai/\"\n", + "\n", + "H2O_MLOPS_GATEWAY = \"https://mlops-api.cloud-internal.h2o.ai\"" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6809e237", + "metadata": {}, + "outputs": [], + "source": [ + "from getpass import getpass\n", + "\n", + "import h2o_authn\n", + "import h2osteam\n", + "from h2osteam.clients import DriverlessClient\n", + "\n", + "import requests\n", + "import json\n", + "import h2o_mlops_client\n", + "\n", + "import pandas as pd\n", + "import numpy as np\n" + ] + }, + { + "cell_type": "markdown", + "id": "d2fcf993", + "metadata": {}, + "source": [ + "## Securely connect to the platform\n", + "We first connect to the H2O AI Cloud using our personal access token to create a token provider object. We can then use this object to log into Steam and other APIs." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b8cfce11", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Visit https://cloud-internal.h2o.ai/auth/get-platform-token to get your personal access token\n", + "Enter your access token: ········\n" + ] + } + ], + "source": [ + "print(f\"Visit {REFRESH_TOKEN} to get your personal access token\")\n", + "tp = h2o_authn.TokenProvider(\n", + " refresh_token=getpass(\"Enter your access token: \"),\n", + " client_id=CLIENT_ID,\n", + " token_endpoint_url=TOKEN_ENDPOINT\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "a76b5099", + "metadata": {}, + "source": [ + "Next, we will connect to our AI Engine manager to view all instances of Driverless AI that we have access to. If you don't have an instance of Driverless AI, or you need to learn how to start your instance, please view the Enterprise Steam tutorial. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "cc42d071", + "metadata": {}, + "outputs": [], + "source": [ + "steam = h2osteam.login(\n", + " url=H2O_STEAM_URL,\n", + " access_token=tp()\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "e22f8863", + "metadata": {}, + "source": [ + "## Connect to Driverless AI\n", + "We'll create a connection object called dai that we will use to interact with the platform. Throughout the client you can use the .gui() function to get a link which will take you to the user interface of that specific page." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "7045c589", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Driverless AI instance is submitted, please wait...\n", + "Driverless AI instance is running\n" + ] + } + ], + "source": [ + "instance = DriverlessClient().launch_instance(name=\"test-instance\",\n", + " version=\"1.10.2\",\n", + " profile_name=\"default-driverless-kubernetes\")\n", + "client = instance.connect()" + ] + }, + { + "cell_type": "markdown", + "id": "efc2bd3b", + "metadata": {}, + "source": [ + "### Upload and Download Data\n", + "You can upload data using any method that is enabled on your system. Here we will show:\n", + "* Add data from a public s3 link\n", + "* Download a dataset\n", + "* Upload data from your local machine\n", + "* Rename a dataset\n", + "* Upload using the JDBC connector" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "404486a7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Complete 100.00% - [4/4] Computed stats for column Churn?\n" + ] + } + ], + "source": [ + "telco_churn = client.datasets.create(data=\"https://h2o-internal-release.s3-us-west-2.amazonaws.com/data/Splunk/churn.csv\", \n", + " data_source=\"s3\", \n", + " name=\"Telco_Churn\",\n", + " force=True\n", + " )" + ] + }, + { + "cell_type": "markdown", + "id": "972f5c65", + "metadata": {}, + "source": [ + "### Split a Dataset\n", + "The split function returns a dictionary of two datasets so you can easily pass them to the experiments" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "7645bcf9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Complete\n" + ] + } + ], + "source": [ + "telco_churn_split = telco_churn.split_to_train_test(\n", + " train_size=0.8, \n", + " train_name='telco_churn_train', \n", + " test_name='telco_churn_test', \n", + " target_column= \"Churn?\", # Beta users with client from before March 15th use target_col\n", + " seed=42\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "6c56c38e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'train_dataset': 97bf065c-cd58-11ec-83d6-7205daa658dd telco_churn_train,\n", + " 'test_dataset': 97bf2812-cd58-11ec-83d6-7205daa658dd telco_churn_test}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "telco_churn_split" + ] + }, + { + "cell_type": "markdown", + "id": "01175384", + "metadata": {}, + "source": [ + "## Modeling\n", + "**Notes:** Dictionaries allow you to easily use common settings in your experiments
\n", + "**Notes:** Experiments will be `sync` by default meaning they will lock the notebook until they are complete. You can also use `async` versions of the fucntions. With the `async` functions you can use included code below to monthior and experiment as it runs, see logs in real time, and stop it when it is \"good enough\"." + ] + }, + { + "cell_type": "markdown", + "id": "d075cc7c", + "metadata": {}, + "source": [ + "### Dictionary for a Use Case\n", + "We might want to run several experiments with different dial and expert settings. All of these will likely have some things in common, namely details about this specific dataset. We will create a dictionary to use in many experiments." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6c04e900", + "metadata": {}, + "outputs": [], + "source": [ + "telco_settings = {\n", + " **telco_churn_split,\n", + " 'task': 'classification',\n", + " 'target_column': \"Churn?\", # Beta users with client from before March 15th use target_col\n", + " 'scorer': 'F1'\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "95897cad", + "metadata": {}, + "source": [ + "### Launch an Experiment\n", + "We will start by running an async experiment which will immeadiatly free our notebook to run additional commands" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "a46140cb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Experiment launched at: https://steam.cloud-internal.h2o.ai:443/proxy/driverless/535/#/experiment?key=c3900880-cd58-11ec-83d6-7205daa658dd\n" + ] + } + ], + "source": [ + "default_baseline = client.experiments.create_async(\n", + " **telco_settings, \n", + " name='Default Baseline', accuracy=7, time=2, interpretability=8,\n", + " force=True,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "f0e60e46", + "metadata": {}, + "source": [ + "### View the Experiment Summary" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "7896b466", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Status: Complete\n", + "Experiment: Default Baseline (c3900880-cd58-11ec-83d6-7205daa658dd)\n", + " Version: 1.10.2, 2022-05-06 16:30\n", + " Settings: 7/2/8, seed=793681543, GPUs disabled\n", + " Train data: telco_churn_train (2666, 21)\n", + " Validation data: N/A\n", + " Test data: [Test] (667, 20)\n", + " Target column: Churn? (binary, 14.479% target class)\n", + "System specs: Docker/Linux, 28 GB, 32 CPU cores, 0/0 GPU\n", + " Max memory usage: 1.03 GB, 0 GB GPU\n", + "Recipe: AutoDL (19 iterations, 8 individuals)\n", + " Validation scheme: stratified, 6 internal holdouts (3-fold CV)\n", + " Feature engineering: 94 features scored (18 selected)\n", + "Timing: MOJO latency 0.0920 millis (1.8MB), Python latency 82.2066 millis (1.3MB)\n", + " Data preparation: 7.96 secs\n", + " Shift/Leakage detection: 3.70 secs\n", + " Model and feature tuning: 111.75 secs (67 of 72 models trained)\n", + " Feature evolution: 236.41 secs (198 of 288 models trained)\n", + " Final pipeline training: 29.70 secs (6 models trained)\n", + " Python / MOJO scorer building: 36.26 secs / 22.83 secs\n", + "Validation score: F1 = 0.2529489 (constant preds of -1.776)\n", + "Validation score: F1 = 0.7169587 +/- 0.02627256 (baseline)\n", + "Validation score: F1 = 0.7791869 +/- 0.02702316 (final pipeline)\n", + "Test score: F1 = 0.7292818 +/- 0.03269455 (final pipeline)\n" + ] + } + ], + "source": [ + "default_baseline.summary()" + ] + }, + { + "cell_type": "markdown", + "id": "bc89edde", + "metadata": {}, + "source": [ + "## Projects" + ] + }, + { + "cell_type": "markdown", + "id": "fa5b9055", + "metadata": {}, + "source": [ + "### Create a project" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "32a54c31", + "metadata": {}, + "outputs": [], + "source": [ + "project = client.projects.create(\n", + " name=\"Example_Project\",\n", + " description=\"Steam-DAI-MLOps Tutorial\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "a53740d2", + "metadata": {}, + "source": [ + "### Link experiment to project" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "dcb500ac", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "experiment = client.experiments.list()[0]\n", + "project.link_experiment(experiment=experiment)" + ] + }, + { + "cell_type": "markdown", + "id": "24868b39", + "metadata": {}, + "source": [ + "## Connect to MLOps" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "4fb5d547", + "metadata": {}, + "outputs": [], + "source": [ + "mlops = h2o_mlops_client.Client(\n", + " gateway_url=H2O_MLOPS_GATEWAY,\n", + " token_provider=tp,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "21c6c830", + "metadata": {}, + "source": [ + "### List all projects you have access to" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "d8f9c49e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96798617-1ab5-49f7-8a60-c9fd2438cd27 Example_Project\n" + ] + } + ], + "source": [ + "my_projects = mlops.storage.project.list_projects(body={\n", + " 'filter': None, \n", + " 'paging': None, \n", + " 'sorting': None\n", + "}).project\n", + "\n", + "for p in my_projects:\n", + " print(p.id, p.display_name)" + ] + }, + { + "cell_type": "markdown", + "id": "5df670e6", + "metadata": {}, + "source": [ + "### Select a specific project to work with\n", + "We have previously created this project using Driverless AI and added models to it." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "3b4b18cb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'project': {'created_time': datetime.datetime(2022, 5, 6, 16, 31, 53, 841712, tzinfo=tzutc()),\n", + " 'description': 'Steam-DAI-MLOps Tutorial',\n", + " 'display_name': 'Example_Project',\n", + " 'id': '96798617-1ab5-49f7-8a60-c9fd2438cd27',\n", + " 'last_modified_time': datetime.datetime(2022, 5, 6, 16, 31, 53, 841712, tzinfo=tzutc()),\n", + " 'owner_id': '0d118392-fb76-4c79-b5bd-f188ba6e22f2'}}" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "USE_CASE_PROJECT = mlops.storage.project.get_project(body={\n", + " 'project_id': '96798617-1ab5-49f7-8a60-c9fd2438cd27'\n", + "})\n", + "USE_CASE_PROJECT" + ] + }, + { + "cell_type": "markdown", + "id": "b0bd6a79", + "metadata": {}, + "source": [ + "## Experiments" + ] + }, + { + "cell_type": "markdown", + "id": "3c1ee189", + "metadata": {}, + "source": [ + "### List experiments\n", + "Get a list of all experiments that are in our project" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "6a45a3e3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c3900880-cd58-11ec-83d6-7205daa658dd Default Baseline\n" + ] + } + ], + "source": [ + "my_project_experiments = mlops.storage.experiment.list_experiments({\n", + " 'filter': None,\n", + " 'paging': None,\n", + " 'project_id': USE_CASE_PROJECT.project.id,\n", + " 'response_metadata': None,\n", + " 'sorting': None\n", + "}).experiment\n", + "\n", + "for e in my_project_experiments:\n", + " print(e.id, e.display_name)" + ] + }, + { + "cell_type": "markdown", + "id": "d4fb4911", + "metadata": {}, + "source": [ + "### Select a specific project to work with\n", + "We have previously created this project using Driverless AI and added models to it." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "3cb3b885", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'experiment': {'created_time': datetime.datetime(2022, 5, 6, 16, 31, 56, 649868, tzinfo=tzutc()),\n", + " 'display_name': 'Default Baseline',\n", + " 'id': 'c3900880-cd58-11ec-83d6-7205daa658dd',\n", + " 'last_modified_time': datetime.datetime(2022, 5, 6, 16, 31, 56, 649868, tzinfo=tzutc()),\n", + " 'metadata': None,\n", + " 'owner_id': '0d118392-fb76-4c79-b5bd-f188ba6e22f2',\n", + " 'parameters': {'fold_column': '',\n", + " 'target_column': 'Churn?',\n", + " 'test_dataset_id': '',\n", + " 'training_dataset_id': '',\n", + " 'validation_dataset_id': '',\n", + " 'weight_column': ''},\n", + " 'statistics': {'training_duration': '452s'},\n", + " 'status': 'EXPERIMENT_STATUS_UNSPECIFIED',\n", + " 'tag': []}}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "USE_CASE_EXPERIMENT = mlops.storage.experiment.get_experiment({\n", + " 'id': 'c3900880-cd58-11ec-83d6-7205daa658dd', \n", + " 'response_metadata': None\n", + "})\n", + "USE_CASE_EXPERIMENT" + ] + }, + { + "cell_type": "markdown", + "id": "09214d8e", + "metadata": {}, + "source": [ + "## Deployments" + ] + }, + { + "cell_type": "markdown", + "id": "29b9eea8", + "metadata": {}, + "source": [ + "### Deployment environment \n", + "`Dev` and `Prod` are deployment environment tags that you can use for your model deployements. " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "4ee2f801", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "82d04998-4a0e-418c-8289-5fe75a4ac133 DEV\n", + "fc5088e9-323f-4040-a097-6dc396614a91 PROD\n" + ] + } + ], + "source": [ + "my_project_deployment_environments = mlops.storage.deployment_environment.list_deployment_environments(body={\n", + " 'filter': None, \n", + " 'paging': None, \n", + " 'project_id': USE_CASE_PROJECT.project.id, \n", + " 'sorting': None\n", + "}).deployment_environment\n", + "\n", + "for de in my_project_deployment_environments:\n", + " print(de.id, de.display_name)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "009272f8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'deployment_environment': {'created_time': datetime.datetime(2022, 5, 6, 16, 31, 53, 850811, tzinfo=tzutc()),\n", + " 'deployment_target_name': 'kubernetes',\n", + " 'display_name': 'DEV',\n", + " 'id': '82d04998-4a0e-418c-8289-5fe75a4ac133',\n", + " 'last_modified_time': datetime.datetime(2022, 5, 6, 16, 31, 53, 850811, tzinfo=tzutc()),\n", + " 'project_id': '96798617-1ab5-49f7-8a60-c9fd2438cd27'}}" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "USE_CASE_DEPLOYMENT_EVN = mlops.storage.deployment_environment.get_deployment_environment({\n", + " 'deployment_environment_id': '82d04998-4a0e-418c-8289-5fe75a4ac133'\n", + "})\n", + "USE_CASE_DEPLOYMENT_EVN" + ] + }, + { + "cell_type": "markdown", + "id": "25a1a620", + "metadata": {}, + "source": [ + "### Deployment Types\n", + "Deployment types help MLOps understand how traffic should be routed when new data is sent for predictions." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "2eb41daa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['DEPLOYMENT_TYPE_UNSPECIFIED',\n", + " 'SINGLE_MODEL',\n", + " 'SHADOW_TRAFFIC',\n", + " 'SPLIT_TRAFFIC']" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h2o_mlops_client.StorageDeploymentType().allowable_values" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "f66ddb14", + "metadata": {}, + "outputs": [], + "source": [ + "USE_CASE_DEPLOYMENT = mlops.storage.deployment_environment.deploy({\n", + " 'deployment_environment_id': USE_CASE_DEPLOYMENT_EVN.deployment_environment.id,\n", + " 'experiment_id': USE_CASE_EXPERIMENT.experiment.id,\n", + " 'metadata': None,\n", + " 'response_metadata': None,\n", + " 'secondary_scorer': None,\n", + " 'type': h2o_mlops_client.StorageDeploymentType().SINGLE_MODEL\n", + "})\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "50408297", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'deployment': {'created_time': datetime.datetime(2022, 5, 6, 16, 34, 48, 90732, tzinfo=tzutc()),\n", + " 'deployer_data': '',\n", + " 'deployer_data_version': '',\n", + " 'deployment_environment_id': '82d04998-4a0e-418c-8289-5fe75a4ac133',\n", + " 'experiment_id': 'c3900880-cd58-11ec-83d6-7205daa658dd',\n", + " 'id': '0c36ead1-ae68-492b-9493-c5fcceba5e3d',\n", + " 'last_modified_time': datetime.datetime(2022, 5, 6, 16, 34, 48, 90732, tzinfo=tzutc()),\n", + " 'metadata': None,\n", + " 'project_id': '96798617-1ab5-49f7-8a60-c9fd2438cd27',\n", + " 'secondary_scorer': [],\n", + " 'type': 'SINGLE_MODEL'}}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "USE_CASE_DEPLOYMENT" + ] + }, + { + "cell_type": "markdown", + "id": "3b58ad04", + "metadata": {}, + "source": [ + "### Deployment health\n", + "Wait until our deployment has gone from launching to healthy" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "38f89a97", + "metadata": {}, + "outputs": [], + "source": [ + "while mlops.deployer.deployment_status.get_deployment_status({\n", + " 'deployment_id': USE_CASE_DEPLOYMENT.deployment.id\n", + "}).deployment_status == \"LAUNCHING\":\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "7ba6d03f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HEALTHY\n", + "https://model.cloud-internal.h2o.ai/0c36ead1-ae68-492b-9493-c5fcceba5e3d/model/sample_request\n", + "https://model.cloud-internal.h2o.ai/0c36ead1-ae68-492b-9493-c5fcceba5e3d/model/score\n" + ] + } + ], + "source": [ + "DEPLOYMENT_STATUS = mlops.deployer.deployment_status.get_deployment_status({\n", + " 'deployment_id': USE_CASE_DEPLOYMENT.deployment.id\n", + "}).deployment_status\n", + "\n", + "print(DEPLOYMENT_STATUS.state)\n", + "print(DEPLOYMENT_STATUS.scorer.sample_request.url)\n", + "print(DEPLOYMENT_STATUS.scorer.score.url)" + ] + }, + { + "cell_type": "markdown", + "id": "59c9a5ef", + "metadata": {}, + "source": [ + "## Make predictions\n", + "Using the `https` library, we make predictions on new data" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "fad16f5a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'fields': ['Account Length',\n", + " 'Area Code',\n", + " \"Int'l Plan\",\n", + " 'VMail Plan',\n", + " 'VMail Message',\n", + " 'Day Mins',\n", + " 'Day Calls',\n", + " 'Day Charge',\n", + " 'Eve Mins',\n", + " 'Eve Calls',\n", + " 'Eve Charge',\n", + " 'Night Mins',\n", + " 'Night Calls',\n", + " 'Night Charge',\n", + " 'Intl Mins',\n", + " 'Intl Calls',\n", + " 'Intl Charge',\n", + " 'CustServ Calls'],\n", + " 'rows': [['0',\n", + " '0',\n", + " 'text',\n", + " 'text',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0']]}" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sample_request_as_text = requests.get(DEPLOYMENT_STATUS.scorer.sample_request.url).text\n", + "sample_request = json.loads(sample_request_as_text)\n", + "sample_request" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "54ca9732", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Account Length',\n", + " 'Area Code',\n", + " \"Int'l Plan\",\n", + " 'VMail Plan',\n", + " 'VMail Message',\n", + " 'Day Mins',\n", + " 'Day Calls',\n", + " 'Day Charge',\n", + " 'Eve Mins',\n", + " 'Eve Calls',\n", + " 'Eve Charge',\n", + " 'Night Mins',\n", + " 'Night Calls',\n", + " 'Night Charge',\n", + " 'Intl Mins',\n", + " 'Intl Calls',\n", + " 'Intl Charge',\n", + " 'CustServ Calls']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fields = sample_request[\"fields\"]\n", + "fields" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "a80a0856", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['0',\n", + " '0',\n", + " 'text',\n", + " 'text',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0',\n", + " '0']" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sample_row = sample_request[\"rows\"][0]\n", + "sample_row" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "4564f3ea", + "metadata": {}, + "outputs": [], + "source": [ + "new_row = ['100', '100', 'text', 'text', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100']" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "cf6a5d66", + "metadata": {}, + "outputs": [], + "source": [ + "new_predictions = requests.post(\n", + " url=DEPLOYMENT_STATUS.scorer.score.url,\n", + " json={\n", + " 'fields': fields,\n", + " 'rows': [sample_row, new_row]\n", + " }\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "339cd299", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_predictions" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "04008a7c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'fields': ['Churn?.False.', 'Churn?.True.'],\n", + " 'id': 'c3900880-cd58-11ec-83d6-7205daa658dd',\n", + " 'score': [['0.9789733205468227', '0.021026679453177242'],\n", + " ['0.15210581391684352', '0.8478941860831565']]}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "predictions_dict = json.loads(new_predictions.text)\n", + "predictions_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "772146e8", + "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", + "
Churn?.False.Churn?.True.
00.97897332054682270.021026679453177242
10.152105813916843520.8478941860831565
\n", + "
" + ], + "text/plain": [ + " Churn?.False. Churn?.True.\n", + "0 0.9789733205468227 0.021026679453177242\n", + "1 0.15210581391684352 0.8478941860831565" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.DataFrame(predictions_dict[\"score\"], columns=predictions_dict[\"fields\"])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}