small updates

This commit is contained in:
Morten Hjorth-Jensen
2023-09-12 16:19:03 +02:00
parent e6636aafdb
commit 14144f2728
3 changed files with 220 additions and 127 deletions
+5 -3
View File
@@ -1,3 +1,5 @@
# tod make plot
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
@@ -35,13 +37,13 @@ def create_X(x, y, n ):
# Making meshgrid of datapoints and compute Franke's function
n = 5
N = 1000
n = 2
N = 2
x = np.sort(np.random.uniform(0, 1, N))
y = np.sort(np.random.uniform(0, 1, N))
z = FrankeFunction(x, y)
X = create_X(x, y, n=n)
print(X)
# We split the data in test and training data
X_train, X_test, y_train, y_test = train_test_split(X, z, test_size=0.2)
+64
View File
@@ -0,0 +1,64 @@
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import linear_model
def MSE(y_data,y_model):
n = np.size(y_model)
return np.sum((y_data-y_model)**2)/n
def FrankeFunction(x,y):
term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
return term1 + term2 + term3 + term4
def create_X(x, y, n ):
if len(x.shape) > 1:
x = np.ravel(x)
y = np.ravel(y)
N = len(x)
l = int((n+1)*(n+2)/2) # Number of elements in beta
X = np.ones((N,l))
for i in range(1,n+1):
q = int((i)*(i+1)/2)
for k in range(i+1):
X[:,q+k] = (x**(i-k))*(y**k)
return X
# Making meshgrid of datapoints and compute Franke's function
# fourth-order poly, intercept included above
n = 5
N = 1000
x = np.sort(np.random.uniform(0, 1, N))
y = np.sort(np.random.uniform(0, 1, N))
z = FrankeFunction(x, y)
X = create_X(x, y, n=n)
# We split the data in test and training data
X_train, X_test, y_train, y_test = train_test_split(X, z, test_size=0.2)
# matrix inversion to find beta, note no centering scaling and intercept column included
OLSbeta = np.linalg.pinv(X_train.T @ X_train) @ X_train.T @ y_train
print(OLSbeta)
# and then make the prediction
ytildeOLS = X_train @ OLSbeta
print("Training MSE for OLS")
print(MSE(y_train,ytildeOLS))
ypredictOLS = X_test @ OLSbeta
print("Test MSE OLS")
print(MSE(y_test,ypredictOLS))
File diff suppressed because one or more lines are too long