Browse and solve 1,200+ machine learning coding challenges on Deep-ML. Filter by difficulty (beginner, intermediate, advanced) and category (linear algebra, machine learning, deep learning, NLP, computer vision). Write Python solutions in the browser and get instant feedback against test cases.
Deep-ML | B-Spline Basis by the Cox-de Boor Recursion and Spline RegressionB-Spline Basis by the Cox-de Boor Recursion and Spline Regression
B-Spline Basis by the Cox-de Boor Recursion and Spline Regression
With one inner knot at 0.5 and degree 1 there are 1 + 1 + 1 = 3 hat functions peaking at 0, 0.5 and 1. At x = 0.25 the first two are each 0.5; at x = 1 (the right boundary) only the last one is active.
Build a B-spline design matrix from scratch and use it for regression.
bspline_basis(x, inner_knots, degree, lower, upper): use the clamped knot vector
t=(p+1ℓ,…,ℓ,κ1,…,κK,p+1u,…,u)
with p = degree, ℓ = lower, u = upper and inner knots κ1<⋯<κK. Return the N×(K+p+1) matrix of basis functions Bi,p(xn) from the Cox-de Boor recursion:
Bi,0(x)=1 if ti≤x<ti+1 and 0 otherwise, except that x=u belongs to the last non-empty interval;
Bi,p(x)=ti+p−tix−tiBi,p−1(x)+ti+p+1−ti+1ti+p+1−xBi+1,p−1(x), where a term with a zero denominator is 0.
Inputs satisfy ℓ≤x≤u.
fit_spline(x, y, n_knots, degree, lam=0.0): place n_knots inner knots at the quantiles np.quantile(x, q) for q = np.linspace(0, 1, n_knots + 2)[1:-1], use ℓ=minx and u=maxx, and solve (B⊤B+λI)c=B⊤y. Return (c, inner_knots, lower, upper).