202 lines
4.6 KiB
Plaintext
202 lines
4.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# WS 04 Vorlage - KNeighborsClassifier"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"* standardisieren Sie die Features von Trainings- und Testdaten mit Hilfe von sklearn.preprocessing.StandardScaler\n",
|
|
"* ermitteln Sie anschliessend die besten Parameterwerte für KNeighborsClassifier\n",
|
|
" * n_neighbors (1-10)\n",
|
|
" * p (z.B. 1, 2, 3)\n",
|
|
"* vergleichen Sie die Ergebnisse ohne und mit standardisieren"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## import libraries\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",
|
|
"## load data\n",
|
|
"datapath = '../3_data'\n",
|
|
"from os import chdir; chdir(datapath)\n",
|
|
"data = pd.read_csv('bank_data_prep.csv')\n",
|
|
"#data.shape ## check\n",
|
|
"\n",
|
|
"## features - target - split\n",
|
|
"X = data.drop('y', axis=1)\n",
|
|
"y = data['y']\n",
|
|
"\n",
|
|
"## test - train - split\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"X_train, X_test, y_train, y_test, = train_test_split(X,\n",
|
|
" y,\n",
|
|
" train_size=2 / 3,\n",
|
|
" random_state=1234)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"rem: für die obige Datenaufbereitung wird ab dem nächsten Workshop die Funktion `prep_data()` aus dem Modul `bfh_cas_pml` verwendet werden"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## standardiz features (lead: train data)\n",
|
|
"from sklearn.preprocessing import StandardScaler\n",
|
|
"scaler = StandardScaler().fit(X_train)\n",
|
|
"X_train_scaled = scaler.transform(X_train)\n",
|
|
"X_test_scaled = scaler.transform(X_test)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1\n",
|
|
"2\n",
|
|
"3\n",
|
|
"4\n",
|
|
"5\n",
|
|
"6\n",
|
|
"7\n",
|
|
"8\n",
|
|
"9\n",
|
|
"10\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"## Tune über n_neighbors\n",
|
|
"from sklearn.neighbors import KNeighborsClassifier\n",
|
|
"model = KNeighborsClassifier()\n",
|
|
"params = range(1, 11)\n",
|
|
"scores = [] ## scores ohne Standardisieren\n",
|
|
"scores_sc = [] ## scores mit Standardisieren\n",
|
|
"\n",
|
|
"for param in params:\n",
|
|
" print(param)\n",
|
|
" ## tbd\n",
|
|
"\n",
|
|
" \n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## Tune über p\n",
|
|
"params = range(1, 4) ## dasselbe wie [1, 2, 3]\n",
|
|
"## tbd\n",
|
|
"\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**Fazit**:\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 07 Klassifikation - KNeighborsClassifier",
|
|
"title_sidebar": "Contents",
|
|
"toc_cell": true,
|
|
"toc_position": {
|
|
"height": "calc(100% - 180px)",
|
|
"left": "10px",
|
|
"top": "150px",
|
|
"width": "205.2px"
|
|
},
|
|
"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()) "
|
|
}
|
|
},
|
|
"types_to_exclude": [
|
|
"module",
|
|
"function",
|
|
"builtin_function_or_method",
|
|
"instance",
|
|
"_Feature"
|
|
],
|
|
"window_display": false
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|