225 lines
5.5 KiB
Plaintext
225 lines
5.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"toc": true
|
|
},
|
|
"source": [
|
|
"# WS 08 Regression mit Standardisieren und Logarithmieren"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"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",
|
|
"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('melb_data_prep.csv', target='Price', seed=1234)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"-105513873.23403685\n",
|
|
"[ 245383.60581414 -141356.39759052 -40383.66643969 161336.03949841\n",
|
|
" 40391.14829949 83303.27089591]\n",
|
|
"[1331246.16325189 2557493.2373921 871684.82823291 1495633.275723\n",
|
|
" 1549557.61151302 634348.67092323]\n",
|
|
"0.5601419746121152\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"## baseline\n",
|
|
"from sklearn.linear_model import LinearRegression\n",
|
|
"from sklearn.metrics import r2_score\n",
|
|
"model = LinearRegression()\n",
|
|
"model.fit(X_train, y_train)\n",
|
|
"y_pred = model.predict(X_test)\n",
|
|
"\n",
|
|
"print(model.intercept_)\n",
|
|
"print(model.coef_[:6])\n",
|
|
"print(y_pred[:6])\n",
|
|
"print(r2_score(y_test, y_pred))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1055902.69523731\n",
|
|
"[ 235020.76662584 -96493.73493151 -243470.62893089 106305.85273776\n",
|
|
" 35544.05464669 71047.51543032]\n",
|
|
"[1331246.16325187 2557493.23739203 871684.82823297 1495633.27572294\n",
|
|
" 1549557.611513 634348.67092323]\n",
|
|
"0.5601419746121148\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"## scaled features\n",
|
|
"from sklearn.preprocessing import StandardScaler\n",
|
|
"scaler = StandardScaler()\n",
|
|
"scaler.fit(X_train)\n",
|
|
"X_train_sc = scaler.transform(X_train)\n",
|
|
"X_test_sc = scaler.transform(X_test)\n",
|
|
"\n",
|
|
"model = LinearRegression()\n",
|
|
"model.fit(X_train_sc, y_train)\n",
|
|
"y_pred = model.predict(X_test_sc)\n",
|
|
"\n",
|
|
"print(model.intercept_)\n",
|
|
"print(model.coef_[:6])\n",
|
|
"print(y_pred[:6])\n",
|
|
"print(r2_score(y_test, y_pred))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**Fazit**\n",
|
|
"* Auswirkung von Skalieren der Features\n",
|
|
" * Koeffizienten und Intercept: Einfluss\n",
|
|
" * Prediction: kein Einfluss\n",
|
|
" * Score: natürlich auch kein Einfluss, wird ja aus Prediction berechnet"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0.5519266421486302\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"## log target\n",
|
|
"y_train_log = np.log10(y_train)\n",
|
|
"y_test_log = np.log10(y_test)\n",
|
|
"\n",
|
|
"model = LinearRegression()\n",
|
|
"model.fit(X_train, y_train_log)\n",
|
|
"y_pred = model.predict(X_test)\n",
|
|
"print(r2_score(10**y_test_log, 10**y_pred))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**Fazit**\n",
|
|
"* wird sogar etwas schlechter\n",
|
|
"* kombination mit skalierten Features erübrigt sich hier, da skalieren ja offenbar keinen Einfluss auf score hat"
|
|
]
|
|
}
|
|
],
|
|
"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": "1",
|
|
"nav_menu": {},
|
|
"number_sections": false,
|
|
"sideBar": true,
|
|
"skip_h1_title": true,
|
|
"title_cell": "WS 11 Regression - mit FE - solution",
|
|
"title_sidebar": "Contents",
|
|
"toc_cell": true,
|
|
"toc_position": {
|
|
"height": "calc(100% - 180px)",
|
|
"left": "10px",
|
|
"top": "150px",
|
|
"width": "195.933px"
|
|
},
|
|
"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": "321.85px",
|
|
"left": "785px",
|
|
"right": "20px",
|
|
"top": "118px",
|
|
"width": "350px"
|
|
},
|
|
"types_to_exclude": [
|
|
"module",
|
|
"function",
|
|
"builtin_function_or_method",
|
|
"instance",
|
|
"_Feature"
|
|
],
|
|
"window_display": false
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|