Commit b7986c47 authored by Carsten Eie Frigaard's avatar Carsten Eie Frigaard
Browse files

update

parent 5e6dc526
#!/opt/anaconda3/bin/python3
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def LoadDataFromL01():
import pickle
filename = "../../L04/Data/itmal_l01_data.pkl"
with open(f"{filename}", "rb") as f:
(X, y) = pickle.load(f)
return X, y
X, y = LoadDataFromL01()
print(f"X.shape={X.shape}, y.shape={y.shape}")
assert X.shape[0] == y.shape[0]
assert X.ndim == 2
assert y.ndim == 1 # did a y.ravel() before saving to picke file
assert X.shape[0] == 29
# re-create plot data (not stored in the Pickel file)
m = np.linspace(0, 60000, 1000)
M = np.empty([m.shape[0], 1])
M[:, 0] = m
print("OK")
# Setup the MLP and lin. regression again..
from sklearn.neural_network import MLPRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import r2_score
def PlotModels(model1, model2, X, y, name_model1="lin.reg", name_model2="MLP"):
# NOTE: local function is such a nifty feature of Python!
def CalcPredAndScore(model1, model2, X, y):
y_pred_model1 = model1.predict(X)
y_pred_model2 = model2.predict(X)
# call r2
score_model1 = r2_score(y, y_pred_model1)
score_model2 = r2_score(y, y_pred_model2)
return y_pred_model1, y_pred_model2, score_model1, score_model2
def Fill(s, n):
while(len(s)<n):
s += " "
return s
y_pred_model1, y_pred_model2, score_model1, score_model2 = CalcPredAndScore(
model1, model2, X, y)
plt.plot(X, y_pred_model1, "r.-")
plt.plot(X, y_pred_model2, "kx-")
plt.scatter(X, y)
plt.xlabel("GDP per capita")
plt.ylabel("Life satisfaction")
plt.legend([name_model1, name_model2, "X OECD data"])
l = max(len(name_model1), len(name_model2))
print(f"{Fill(name_model1,l)}.score(X, y)={score_model1:0.2f}")
print(f"{Fill(name_model2,l)}.score(X, y)={score_model2:0.2f}")
# lets make a linear and MLP regressor and redo the plots
mlp = MLPRegressor(hidden_layer_sizes=(10, ),
solver='lbfgs',
activation='relu',
tol=1E-5,
max_iter=100000,
verbose=False)
linreg = LinearRegression()
mlp.fit(X, y)
linreg.fit(X, y)
print("The MLP may mis-fit the data, seen in the, sometimes, bad R^2 score..\n")
PlotModels(linreg, mlp, X, y)
print("\nOK")
from sklearn.neural_network import MLPRegressor
solvers = {"lbfgs", "sgd", "adam"}
mlps = {}
y_preds = {}
for i in solvers:
mlp = MLPRegressor(solver=i, hidden_layer_sizes=(1,))
mlp.fit(X, y.ravel())
score = mlp.score(X, y)
y_preds[i] = mlp.predict(M)
mlps[i] = mlp
print(f"mlp(solver={i:5},..) => score={score:.1f}")
print()
PlotModels(linreg, mlps["lbfgs"], X, y, "lin.reg", "MPL[lbfgs]")
print("\nOK")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment