{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Watson Machine Learing Python Function登録サンプル" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1 Watson MLのWebサービス呼出し" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# wml認証情報 (Watson Machine Learningのcredntailsをコピペします)\n", "\n", "wml_credentials = {\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Machine LearningスコアリングURL (WebサービスエンドポイントのScoring End-pointをコピペします)\n", "scoring_url = ''" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Webサービス呼出しテスト用データ\n", "payload = {\"fields\": [\"CRIM\", \"ZN\", \"INDUS\", \"CHAS\", \"NOX\", \"RM\", \n", " \"AGE\", \"DIS\", \"RAD\", \"TAX\", \"PTRATIO\", \"B\", \"LSTAT\"], \n", "\"values\": \n", "[[0.00632,18.0,2.31,0.0,0.538,6.575,65.2,4.09,1.0,296.0,15.3,396.9,4.98],\n", "[0.02731,0.0,7.07,0.0,0.469,6.421,78.9,4.9671,2.0,242.0,17.8,396.9,9.14]]}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'fields': ['$E-PRICE'],\n", " 'values': [[30.003843377020388], [25.025562379060613]]}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Webサービス呼出しのテスト\n", "# 普通にWebサービス呼出しができることを確認します。\n", "\n", "from watson_machine_learning_client import WatsonMachineLearningAPIClient\n", "client = WatsonMachineLearningAPIClient(wml_credentials)\n", "client.deployments.score(scoring_url, payload)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2 Python Functionの定義" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Python関数呼び出し時に必要となる情報を ai_parmsという変数にまとめておきます\n", "ai_parms = { \"wml_credentials\" : wml_credentials,\n", " \"model_deployment_endpoint_url\" : scoring_url }" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Python Functionの定義\n", "# 上でテストしたWebサービス呼出しをPython Functionとして定義します。\n", "\n", "def spss_function(parms=ai_parms):\n", " from watson_machine_learning_client import WatsonMachineLearningAPIClient\n", " client = WatsonMachineLearningAPIClient( parms[\"wml_credentials\"] )\n", " def score(payload):\n", " return client.deployments.score(parms[\"model_deployment_endpoint_url\"], payload)\n", " return score" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'fields': ['$E-PRICE'], 'values': [[30.003843377020388], [25.025562379060613]]}\n" ] } ], "source": [ "# Python Functionのテスト\n", "# 上で定義したPython Functionをテストします。\n", "function_result = spss_function()(payload)\n", "print(function_result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3 Python FunctionをWatson MLに登録" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No matching default runtime found. Creating one...SUCCESS\n", "\n", "Successfully created runtime with uid: da7caab5-d7d3-4c6c-b601-05daa0044eec\n" ] } ], "source": [ "# PythonFunctionをstore_function関数で登録します\n", "\n", "meta_data = { client.repository.FunctionMetaNames.NAME : 'SPSS boston regression' }\n", "function_details = client.repository.store_function( meta_props=meta_data, function=spss_function )" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "92cb9365-08fb-4d9a-827c-536fe51ad4ea\n" ] } ], "source": [ "# Function IDを取得します\n", "\n", "function_id = function_details[\"metadata\"][\"guid\"]\n", "print(function_id)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "#######################################################################################\n", "\n", "Synchronous deployment creation for uid: '92cb9365-08fb-4d9a-827c-536fe51ad4ea' started\n", "\n", "#######################################################################################\n", "\n", "\n", "INITIALIZING\n", "DEPLOY_IN_PROGRESS.\n", "DEPLOY_SUCCESS\n", "\n", "\n", "------------------------------------------------------------------------------------------------\n", "Successfully finished deployment creation, deployment_uid='4ace00d9-f5af-40d1-a960-65a82ebc7ec0'\n", "------------------------------------------------------------------------------------------------\n", "\n", "\n" ] } ], "source": [ "# Functionをdeployします\n", "\n", "function_deployment_details = client.deployments.create( artifact_uid=function_id, name='SPSS boston deployment' )" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "https://us-south.ml.cloud.ibm.com/v3/wml_instances/fa53a255-4a53-4546-8208-9b92894ab3ba/deployments/4ace00d9-f5af-40d1-a960-65a82ebc7ec0/online\n" ] } ], "source": [ "# functionのdeployment URLを取得します\n", "\n", "function_deployment_endpoint_url = client.deployments.get_scoring_url( function_deployment_details )\n", "print(function_deployment_endpoint_url )" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'fields': ['$E-PRICE'],\n", " 'values': [[30.003843377020388], [25.025562379060613]]}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# deploy したPython Functionの呼出しテスト\n", "\n", "client.deployments.score( function_deployment_endpoint_url, payload)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.6", "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.6.8" } }, "nbformat": 4, "nbformat_minor": 1 }