{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# SPSS Modelerで作った回帰モデルの説明性のテスト" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Successfully installed ibm-ai-openscale-2.1.11\r\n" ] } ], "source": [ "# 追加モジュールの導入\n", "!pip install --upgrade ibm-ai-openscale --no-cache | tail -n 1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# OpenScaleのcredential情報設定\n", "AIOS_CREDENTIALS = {\n", " \"instance_guid\": \"xxxx,\n", " \"apikey\": \"xxxx\", \n", " \"url\": \"https://api.aiopenscale.cloud.ibm.com\"\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.1.11'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# OpenScale APIの初期化\n", "from ibm_ai_openscale import APIClient\n", "from ibm_ai_openscale.engines import WatsonMachineLearningAsset\n", "aios_client = APIClient(AIOS_CREDENTIALS)\n", "aios_client.version" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", "

Subscriptions

\n", " \n", " \n", " \n", "
uidnametypebinding_uidcreated
0557e0cd-7664-401a-bdea-71715d58806aSPSS boston regressionfunctionfa53a255-4a53-4546-8208-9b92894ab3ba2019-07-13T08:56:00.951Z
241ff0f1-9465-45f0-8050-0c007807544dSPSS boston regression functionfunctionfa53a255-4a53-4546-8208-9b92894ab3ba2019-07-11T11:03:30.651Z
3ec6ef2e-9b77-457c-a886-9d8fa3d1c127scikit-learn lr modelmodelfa53a255-4a53-4546-8208-9b92894ab3ba2019-07-04T12:29:37.172Z
\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Subscriptionの一覧表示\n", "from ibm_ai_openscale.supporting_classes import *\n", "aios_client.data_mart.subscriptions.list()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "subscription_uid = 0557e0cd-7664-401a-bdea-71715d58806a\n", "transaction_id = c73b8c87df629dc722e382b90f02f4da-1\n" ] } ], "source": [ "# サブスクリプションIDの取得\n", "subscription_uid = aios_client.data_mart.subscriptions.get_uids()[0]\n", "print('subscription_uid = ', subscription_uid)\n", "\n", "# サブスクリプションオブジェクトの取得 (いろいろな操作が可能になる)\n", "subscription = aios_client.data_mart.subscriptions.get(subscription_uid)\n", "\n", "# 対象サブスクリプションの先頭トランザクションIDの取得\n", "transaction_id = subscription.payload_logging.get_table_content().scoring_id[0]\n", "print('transaction_id = ', transaction_id)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "================================================================\n", "\n", " Looking for explanation for c73b8c87df629dc722e382b90f02f4da-1 \n", "\n", "================================================================\n", "\n", "\n", "\n", "in_progress........\n", "finished\n", "\n", "---------------------------\n", " Successfully finished run \n", "---------------------------\n", "\n", "\n" ] } ], "source": [ "# 説明性の要求\n", "explanation = subscription.explainability.run(transaction_id, background_mode=False)" ] }, { "cell_type": "code", "execution_count": 11, "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", "
request_idtransaction_idasset_idasset_typedeployment_idsubscription_idservice_binding_idexplanationerrorstatuscreated_bycreated_at
003b038bf-7f91-4c2c-8406-595f287beb2dc73b8c87df629dc722e382b90f02f4da-18b4557af-16b2-4e5b-9257-4aeb6c8876a4numeric_categorical1ebb75e2-696a-40a7-b591-30f3ddd2b7700557e0cd-7664-401a-bdea-71715d58806afa53a255-4a53-4546-8208-9b92894ab3ba{'entity': {'status_lime': 'finished', 'predic...NonefinishedIBMid-270007H9EN2019-07-13T14:02:07.425730Z
\n", "
" ], "text/plain": [ " request_id transaction_id \\\n", "0 03b038bf-7f91-4c2c-8406-595f287beb2d c73b8c87df629dc722e382b90f02f4da-1 \n", "\n", " asset_id asset_type \\\n", "0 8b4557af-16b2-4e5b-9257-4aeb6c8876a4 numeric_categorical \n", "\n", " deployment_id subscription_id \\\n", "0 1ebb75e2-696a-40a7-b591-30f3ddd2b770 0557e0cd-7664-401a-bdea-71715d58806a \n", "\n", " service_binding_id \\\n", "0 fa53a255-4a53-4546-8208-9b92894ab3ba \n", "\n", " explanation error status \\\n", "0 {'entity': {'status_lime': 'finished', 'predic... None finished \n", "\n", " created_by created_at \n", "0 IBMid-270007H9EN 2019-07-13T14:02:07.425730Z " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 該当サブスクリプションに対して説明性テーブルの表示\n", "subscription.explainability.get_table_content(limit=5)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'entity': {'status_lime': 'finished',\n", " 'predictions': [{'value': '30.003843377020388',\n", " 'explanation': [{'weight': 0.3321053247301002,\n", " 'feature_name': 'LSTAT',\n", " 'feature_range': {'max': '6.95', 'max_inclusive': True}},\n", " {'weight': 0.2215896559235419,\n", " 'feature_name': 'PTRATIO',\n", " 'feature_range': {'max': '17.40', 'max_inclusive': True}},\n", " {'weight': -0.18185091967246045,\n", " 'feature_name': 'RAD',\n", " 'feature_range': {'max': '4.00', 'max_inclusive': True}},\n", " {'weight': 0.15330666524447153,\n", " 'feature_name': 'ZN',\n", " 'feature_range': {'min': '12.50', 'min_inclusive': False}},\n", " {'weight': 0.111147434429426,\n", " 'feature_name': 'TAX',\n", " 'feature_range': {'max': '330.00',\n", " 'min': '279.00',\n", " 'max_inclusive': True,\n", " 'min_inclusive': False}}]}],\n", " 'status_cem': 'not_supported',\n", " 'input_features': [{'name': 'CRIM',\n", " 'value': '0.00632',\n", " 'feature_type': 'numerical'},\n", " {'name': 'ZN', 'value': '18.0', 'feature_type': 'numerical'},\n", " {'name': 'INDUS', 'value': '2.31', 'feature_type': 'numerical'},\n", " {'name': 'CHAS', 'value': '0.0', 'feature_type': 'numerical'},\n", " {'name': 'NOX', 'value': '0.538', 'feature_type': 'numerical'},\n", " {'name': 'RM', 'value': '6.575', 'feature_type': 'numerical'},\n", " {'name': 'AGE', 'value': '65.2', 'feature_type': 'numerical'},\n", " {'name': 'DIS', 'value': '4.09', 'feature_type': 'numerical'},\n", " {'name': 'RAD', 'value': '1.0', 'feature_type': 'numerical'},\n", " {'name': 'TAX', 'value': '296.0', 'feature_type': 'numerical'},\n", " {'name': 'PTRATIO', 'value': '15.3', 'feature_type': 'numerical'},\n", " {'name': 'B', 'value': '396.9', 'feature_type': 'numerical'},\n", " {'name': 'LSTAT', 'value': '4.98', 'feature_type': 'numerical'}],\n", " 'status': 'finished',\n", " 'asset': {'id': '8b4557af-16b2-4e5b-9257-4aeb6c8876a4',\n", " 'name': 'SPSS boston regression',\n", " 'type': 'numeric_categorical',\n", " 'deployment': {'id': '1ebb75e2-696a-40a7-b591-30f3ddd2b770',\n", " 'name': 'SPSS boston deployment'}}}}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 説明性の最初の行の詳細表示\n", "subscription.explainability.get_table_content()['explanation'][0]" ] }, { "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 }