{ "cells": [ { "cell_type": "code", "execution_count": 18, "id": "e9ca0612-adb5-4d11-89a1-b224de3bd3b5", "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append('../') ##accrocchio\n", "from src.utils import retrive_data,split\n", "import pickle" ] }, { "cell_type": "code", "execution_count": null, "id": "dcb4d7b5-fe20-43f1-b988-51c9d595e06d", "metadata": {}, "outputs": [], "source": [ "labeled,labeled_small = retrive_data(False,threshold_under_represented=0.5,path='/home/agobbi/Projects/PID/datanalytics/PID/src')" ] }, { "cell_type": "code", "execution_count": 16, "id": "569dff52-2a78-43f4-ae40-07842d3669e3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " class p\n", "0 0 0.203424\n", "1 1 0.013596\n", "2 2 0.056109\n", "3 3 1.130661\n", "4 4 16.206137\n" ] } ], "source": [ "dataset,dataset_test = split(labeled_small,\n", " SKI_AREA_TEST= 'Klausberg',\n", " SEASON_TEST_SKIAREA = 'Kronplatz',\n", " SEASON_TEST_YEAR= 2023,\n", " use_smote = False,\n", " weight_type = 'sqrt' )" ] }, { "cell_type": "code", "execution_count": 19, "id": "65405081-748d-43ef-aed6-2a58e38673b4", "metadata": {}, "outputs": [], "source": [ "with open('../src/best_params_and_final_model.pkl','rb') as f:\n", " tot,bst_FS,FS = pickle.load(f) " ] }, { "cell_type": "code", "execution_count": 49, "id": "53434031-1a11-49f7-9481-de77f250d35b", "metadata": {}, "outputs": [], "source": [ "import shap\n", "import xgboost as xgb\n", "import numpy as np\n", "dtest_FS = xgb.DMatrix(dataset_test.X_test_area[bst_FS.feature_names],dataset_test.y_test_area,enable_categorical=True,)\n", "\n", "explainer = shap.TreeExplainer(bst_FS,feature_names=bst_FS.feature_names)\n", "shap_values = explainer.shap_values(dtest_FS)\n" ] }, { "cell_type": "code", "execution_count": 33, "id": "f3b72453-27be-4db5-b40d-883454c294f9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "shap.initjs()" ] }, { "cell_type": "code", "execution_count": 73, "id": "129dd5fb-367e-493e-937c-12ae0727cf85", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.58451770e-03, 2.63414029e-02, 8.63697469e-01, 1.07182816e-01,\n", " 1.93854154e-04], dtype=float32)" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pred = bst_FS.predict(dtest_FS)\n", "pred[2]" ] }, { "cell_type": "code", "execution_count": 53, "id": "46bf2240-cfff-4c1b-a5db-87290bf566be", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "age 53.0\n", "diagnosis dislocation\n", "injury_general_location upper_limbs\n", "country Germania\n", "difficulty easy\n", "cause fall_alone\n", "destination hospital_emergency_room\n", "injury_side R\n", "equipment ski\n", "gender F\n", "snowmobile True\n", "helicopter True\n", "akja False\n", "helmet True\n", "ambulance False\n", "snowmobile_sled False\n", "ski_lift False\n", "indipendently False\n", "skiarea_ambulance False\n", "other False\n", "Name: 724, dtype: object" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset_test.X_test_area[bst_FS.feature_names].iloc[2]" ] }, { "cell_type": "code", "execution_count": 51, "id": "27f3b14c-4917-45ff-93ee-234dbc392b20", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset_test.y_test_area.iloc[2]" ] }, { "cell_type": "code", "execution_count": 64, "id": "9799b767-15f2-403b-8535-eb471b862ab3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-3.5973988, -1.704391, 1.8931085, -0.9031036, -7.5351233]" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[shap_values[i][2].sum() for i in range(5)]" ] }, { "cell_type": "code", "execution_count": 71, "id": "f4cac6a8-1ecc-435b-a7c0-5dff7df561f3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", " Visualization omitted, Javascript library not loaded!
\n", " Have you run `initjs()` in this notebook? If this notebook was from another\n", " user you must also trust this notebook (File -> Trust notebook). If you are viewing\n", " this notebook on github the Javascript has been stripped for security. If you are using\n", " JupyterLab this error is because a JupyterLab extension has not yet been written.\n", "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i=1\n", "shap.plots.force(explainer.expected_value[1], shap_values[i][2],feature_names=bst_FS.feature_names)\n" ] }, { "cell_type": "code", "execution_count": 70, "id": "3d56cbf0-ff20-4d3a-94b7-a0e07c5cc019", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", " Visualization omitted, Javascript library not loaded!
\n", " Have you run `initjs()` in this notebook? If this notebook was from another\n", " user you must also trust this notebook (File -> Trust notebook). If you are viewing\n", " this notebook on github the Javascript has been stripped for security. If you are using\n", " JupyterLab this error is because a JupyterLab extension has not yet been written.\n", "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i=2\n", "shap.plots.force(explainer.expected_value[1], shap_values[i][2],feature_names=bst_FS.feature_names)" ] }, { "cell_type": "code", "execution_count": null, "id": "6bbece5c-75f4-4ba0-a44e-677d9819b17a", "metadata": {}, "outputs": [], "source": [] } ], "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.11.0" } }, "nbformat": 4, "nbformat_minor": 5 }