import numpy as np


def ridge_fit(Phi, t, lam):
    """Minimizes sum of squared errors + lam * (sum of squared weights).
    Column 0 of Phi is the bias and is not penalized."""
    if lam == 0:
        w, *_ = np.linalg.lstsq(Phi, t, rcond=None)
        return w
    P = np.eye(Phi.shape[1])
    P[0, 0] = 0
    return np.linalg.solve(Phi.T @ Phi + lam * P, Phi.T @ t)


def kfold_indices(n, k, seed):
    """Random split of range(n) into k folds, same seed gives the same folds."""
    idx = np.random.RandomState(seed).permutation(n)
    return np.array_split(idx, k)
