This repository has been archived on 2026-06-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
cas-pml/SL/aufgaben/template/4_WS/Loesungen/WS 08 Loesung.ipynb
T
2026-05-21 14:16:30 +02:00

5.5 KiB

WS 08 Regression mit Standardisieren und Logarithmieren

In [2]:
## prepare env, read and prepare data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()

#codepath = '../2_code' ## for import of user defined module
#datapath = '../3_data'
codepath = '.././2_code' ## for import of user defined module
datapath = '../../3_data'

from sys import path; path.insert(1, codepath)
from os import chdir; chdir(datapath)

from bfh_cas_pml import prep_data
X_train, X_test, y_train, y_test = prep_data('melb_data_prep.csv', target='Price', seed=1234)
In [3]:
## baseline
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

print(model.intercept_)
print(model.coef_[:6])
print(y_pred[:6])
print(r2_score(y_test, y_pred))
-105513873.23403685
[ 245383.60581414 -141356.39759052  -40383.66643969  161336.03949841
   40391.14829949   83303.27089591]
[1331246.16325189 2557493.2373921   871684.82823291 1495633.275723
 1549557.61151302  634348.67092323]
0.5601419746121152
In [4]:
## scaled features
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train_sc = scaler.transform(X_train)
X_test_sc = scaler.transform(X_test)

model = LinearRegression()
model.fit(X_train_sc, y_train)
y_pred = model.predict(X_test_sc)

print(model.intercept_)
print(model.coef_[:6])
print(y_pred[:6])
print(r2_score(y_test, y_pred))
1055902.69523731
[ 235020.76662584  -96493.73493151 -243470.62893089  106305.85273776
   35544.05464669   71047.51543032]
[1331246.16325187 2557493.23739203  871684.82823297 1495633.27572294
 1549557.611513    634348.67092323]
0.5601419746121148

Fazit

  • Auswirkung von Skalieren der Features
    • Koeffizienten und Intercept: Einfluss
    • Prediction: kein Einfluss
    • Score: natürlich auch kein Einfluss, wird ja aus Prediction berechnet
In [6]:
## log target
y_train_log = np.log10(y_train)
y_test_log = np.log10(y_test)

model = LinearRegression()
model.fit(X_train, y_train_log)
y_pred = model.predict(X_test)
print(r2_score(10**y_test_log, 10**y_pred))
0.5519266421486302

Fazit

  • wird sogar etwas schlechter
  • kombination mit skalierten Features erübrigt sich hier, da skalieren ja offenbar keinen Einfluss auf score hat