Finish project. Hopefully

This commit is contained in:
2025-09-24 17:51:09 +02:00
parent e20075fc4e
commit d93ac3436d
27 changed files with 738 additions and 55 deletions
+29 -14
View File
@@ -50,7 +50,9 @@
"source": [
"x = np.linspace(-1, 1, 100_000)\n",
"y = datamanip.noise_data(datamanip.runge_function(x), 1.0)\n",
"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)\n",
"x_train, x_test, y_train, y_test = train_test_split(\n",
" x, y, test_size=0.2, random_state=datamanip.get_RNG().integers(0, 1e6)\n",
")\n",
"\n",
"fig, ax = plotting.scatter_dataset(x_train, x_test, y_train, y_test)\n",
"fig.set_layout_engine(\"compressed\")\n",
@@ -255,6 +257,7 @@
" [f\"${format_number(tick)}$\" for tick in lambda_values[::3]], rotation=45\n",
")\n",
"\n",
"fig.tight_layout()\n",
"fig.savefig(os.path.join(FIG_DIR, \"ridge_parameter_plot.pdf\"))"
]
},
@@ -477,7 +480,9 @@
"\n",
"x = np.linspace(-1, 1, X_size)\n",
"y = datamanip.noise_data(datamanip.runge_function(x), 0.1) # LOWER NOISE FOR SGD\n",
"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)\n",
"x_train, x_test, y_train, y_test = train_test_split(\n",
" x, y, test_size=0.2, random_state=datamanip.get_RNG().integers(0, 1e6)\n",
")\n",
"\n",
"X_train = datamanip.polynomial_features(x_train, 10, False)\n",
"X_test = datamanip.polynomial_features(x_test, 10, False)\n",
@@ -518,7 +523,9 @@
"\n",
"x = np.linspace(-1, 1, X_size)\n",
"y = datamanip.noise_data(datamanip.runge_function(x), 0.1) # LOWER NOISE FOR SGD\n",
"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)\n",
"x_train, x_test, y_train, y_test = train_test_split(\n",
" x, y, test_size=0.2, random_state=datamanip.get_RNG().integers(0, 1e6)\n",
")\n",
"\n",
"X_train = datamanip.polynomial_features(x_train, 10, False)\n",
"X_test = datamanip.polynomial_features(x_test, 10, False)\n",
@@ -603,14 +610,16 @@
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace(-1, 1, 300)\n",
"x = np.linspace(-1, 1, 100)\n",
"y = datamanip.runge_function(x)\n",
"x_train, x_test, y_train, y_test_noise_free = train_test_split(x, y, test_size=0.2)\n",
"x_train, x_test, y_train, y_test_noise_free = train_test_split(\n",
" x, y, test_size=0.2, random_state=datamanip.get_RNG().integers(0, 1e6)\n",
")\n",
"y_train = datamanip.noise_data(y_train, 1.0)\n",
"y_test = datamanip.noise_data(y_test_noise_free, 1.0)\n",
"\n",
"polynomial_degrees = np.arange(1, 50)\n",
"n_bootstraps = len(x_train)\n",
"polynomial_degrees = np.arange(1, 15)\n",
"n_bootstraps = 5000\n",
"mses = np.zeros((len(polynomial_degrees), n_bootstraps))\n",
"biases = np.zeros((len(polynomial_degrees), n_bootstraps))\n",
"variances = np.zeros((len(polynomial_degrees), n_bootstraps))\n",
@@ -719,9 +728,14 @@
"metadata": {},
"outputs": [],
"source": [
"fig, (ax, ax2) = plt.subplots(\n",
" 1, 2, figsize=plotting.get_figsize(0.35, True), sharey=True\n",
")\n",
"SINGULAR_PLOT = True\n",
"if not SINGULAR_PLOT:\n",
" fig, (ax, ax2) = plt.subplots(\n",
" 1, 2, figsize=plotting.get_figsize(0.35, True), sharey=True\n",
" )\n",
"else:\n",
" fig, ax = plt.subplots(1, 1, figsize=plotting.get_figsize(0.5, False))\n",
" ax2 = ax\n",
"mse_mean = np.mean(mses, axis=1)\n",
"bias_mean = np.mean(biases, axis=1)\n",
"var_mean = np.mean(variances, axis=1)\n",
@@ -767,13 +781,14 @@
"\n",
"fig.tight_layout()\n",
"\n",
"\n",
"ax2.plot(polynomial_degrees, mse_mean, label=\"Bootstrapping MSE\", color=\"C0\")\n",
"if not SINGULAR_PLOT:\n",
" ax2.plot(polynomial_degrees, mse_mean, label=\"Bootstrapping MSE\", color=\"C1\")\n",
"ax2.plot(\n",
" polynomial_degrees,\n",
" np.mean(k_fold_mses, axis=1),\n",
" label=f\"{k_folds}-Fold Crossvalidation MSE\",\n",
" color=\"C1\",\n",
" color=\"black\",\n",
" ls=\"--\",\n",
")\n",
"\n",
"\n",
@@ -791,7 +806,7 @@
"metadata": {},
"outputs": [],
"source": [
"polynomial_degrees = np.arange(1, 30)\n",
"polynomial_degrees = np.arange(1, 25)\n",
"k_folds = 5\n",
"\n",
"k_fold_mses_ols = np.zeros((len(polynomial_degrees), k_folds))\n",
+9 -3
View File
@@ -5,6 +5,10 @@ from sklearn.utils import resample # type: ignore
from sklearn.model_selection import KFold # type: ignore
def get_RNG() -> np.random.Generator:
return np.random.default_rng(314)
def polynomial_features(x: np.ndarray, p: int, intercept: bool = True) -> np.ndarray:
"""Generates a design matrix with polynomial features up to degree p.
Args:
@@ -69,7 +73,7 @@ def noise_data(y: np.ndarray, noise_level: float = 1.0) -> np.ndarray:
Returns:
The noisy target vector of shape (n_samples,).
"""
noise = np.random.normal(0, noise_level, size=y.shape)
noise = get_RNG().normal(0, noise_level, size=y.shape)
return y + noise
@@ -97,7 +101,9 @@ def bootstrap_resample(
"""
resamples = []
for _ in range(n_resamples):
X_resample, y_resample = resample(X, y)
X_resample, y_resample = resample(
X, y, random_state=get_RNG().integers(0, int(1e6))
)
resamples.append((X_resample, y_resample))
return resamples
@@ -113,7 +119,7 @@ def k_fold_split(
Returns:
A list of tuples, each containing (X_train, y_train, X_val, y_val) for each fold.
"""
kf = KFold(n_splits=k, shuffle=True)
kf = KFold(n_splits=k, shuffle=True, random_state=get_RNG().integers(0, int(1e6)))
folds = []
for train_index, val_index in kf.split(X):
X_train, X_val = X[train_index], X[val_index]
+4 -2
View File
@@ -1,4 +1,5 @@
import numpy as np
from pyoptim.datamanip import get_RNG
def Ridge_parameters(X: np.ndarray, y: np.ndarray, lam: float) -> np.ndarray:
@@ -274,17 +275,18 @@ class OLSStochasticGradientDescent(OLSGradientDescent):
super().__init__(*args, **kwargs)
self.batch_size = batch_size
self.batches_per_epoch = batches_per_epoch
self.RNG = get_RNG()
def _precomp(self):
self.N = len(self.y)
self.indices = np.arange(self.N)
self.n = self.batch_size
np.random.shuffle(self.indices)
self.RNG.shuffle(self.indices)
self.X = self.X[self.indices]
self.y = self.y[self.indices]
def _comp_step(self):
index = np.random.randint(0, self.N)
index = self.RNG.integers(0, self.N)
batch_indices = slice(index, index + self.batch_size)
if index + self.batch_size > self.N:
batch_indices = slice(index, self.N)