{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [], "toc": true }, "source": [ "# WS 15 Schwellenwert für Accuracy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `.predict_proba()` gibt bei allen Klassifikatoren die Wahrscheinlichkeit für die Zugehörigkeit zu den einlenen Klassen zurück, `predict()` dagegen die wahrscheinlichtste Klasse selber\n", "* für Zwei-Klassen Fragestellungen bedeutet dies, dass bei einer Wahrscheinlickkeit (proba) `> 0.5` für die erste Klasse diese zurückgegeben wird, andernfalls die zweite Klasse, `0.5` ist somit ein scheinbar willkürlicher Schwellenwert\n", "* untersuchen Sie die Auswirkung anderer Schwellenwerte auf die Accuracy mit `RandomForestClassifier` auf den aufbereiteten Bankkunden-Datan\n", "\n", "* vorgeschlagenes Vorgehen:\n", " * trainieren eines RandomForestClassifier mit den vorbereiteten Bankkundendaten (Trainingsdaten)\n", " * bestimmen der Wahrscheinlichkeit für jede Beobachtung der entsprechenden Testdaten zur Klasse 'no'\n", " * erstellen einens Range der zu untersuchenden Schwellenwerte, z.B. mit np.arange()\n", " * in einem Loop über alle Werte dieses Ranges\n", " * `y_pred` für den jeweiligen Schwellenwert berechnen (wiederum als `['no', 'yes']`)\n", " * `accuracy_score()` der jeweiligen Prediction (und sammeln in einer Liste)\n", " * ausgeben des besten Score-Wertes und des zugehörigen Schwellenwertes in der Konsole\n", " * visualisieren der Ergebnisse auch als Lineplot " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "## prepare env, read and prepare data\n", "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns; sns.set()\n", "\n", "codepath = '../2_code' ## for import of user defined module\n", "datapath = '../3_data'\n", "\n", "from sys import path; path.insert(1, codepath)\n", "from os import chdir; chdir(datapath)\n", "\n", "from bfh_cas_pml import prep_data\n", "X_train, X_test, y_train, y_test = prep_data('bank_data_prep.csv', target='y', seed=1234)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-04-15T21:03:01.918701Z", "start_time": "2020-04-15T21:03:01.844142Z" } }, "outputs": [], "source": [ "## train a model\n", "from sklearn.ensemble import RandomForestClassifier \n", "model = RandomForestClassifier(random_state=1234)\n", "model.fit(X_train, y_train) \n", "\n", "## prediction using .predict_proba()\n", "y_pred_p_no = model.predict_proba(X_test)[:, 0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0\n", "0.1\n", "0.2\n", "0.30000000000000004\n", "0.4\n", "0.5\n", "0.6000000000000001\n", "0.7000000000000001\n", "0.8\n", "0.9\n", "1.0\n" ] } ], "source": [ "## inspect different threshold values\n", "from sklearn.metrics import accuracy_score\n", "\n", "thresholds = np.arange(0, 1.01, 0.1) ## test over 10\n", "#thresholds = np.arange(0, 1.01, 0.01)\n", "\n", "scores = []\n", "\n", "for threshold in thresholds:\n", " ## tbd\n", " print(threshold)\n", "\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "## results\n", "## tbd\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [] }, "outputs": [], "source": [ "## viszalization\n", "## tbd\n", "\n", "\n" ] } ], "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.7" }, "toc": { "base_numbering": "", "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "WS 15 Validierung - Sampling", "title_sidebar": "Contents", "toc_cell": true, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "195.867px" }, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "position": { "height": "306.85px", "left": "862px", "right": "20px", "top": "137px", "width": "350px" }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }