@@ -0,0 +1,1836 @@
+#!/usr/bin/env python%0A%22%22%22%0A========================%0APolynomial interpolation%0A========================%0A%0AThis example demonstrates how to approximate a function with a polynomial of%0Adegree n_degree by using ridge regression. Concretely, from n_samples 1d%0Apoints, it suffices to build the Vandermonde matrix, which is n_samples x%0An_degree+1 and has the following form:%0A%0A%5B%5B1, x_1, x_1 ** 2, x_1 ** 3, ...%5D,%0A %5B1, x_2, x_2 ** 2, x_2 ** 3, ...%5D,%0A ...%5D%0A%0AIntuitively, this matrix can be interpreted as a matrix of pseudo features (the%0Apoints raised to some power). The matrix is akin to (but different from) the%0Amatrix induced by a polynomial kernel.%0A%0AThis example shows that you can do non-linear regression with a linear model,%0Aby manually adding non-linear features. Kernel methods extend this idea and can%0Ainduce very high (even infinite) dimensional feature spaces.%0A%22%22%22%0Aprint __doc__%0A%0A# Author: Mathieu Blondel%0A# License: BSD Style.%0A%0Aimport numpy as np%0Aimport pylab as pl%0A%0Afrom scikits.learn.linear_model import Ridge%0A%0Anp.random.seed(0)%0A%0A%0Adef f(x):%0A    %22%22%22 function to approximate by polynomial interpolation%22%22%22%0A    return x * np.sin(x)%0A%0A%0Adef vandermonde(x, degree=1):%0A    %22%22%22 build vandermonde matrix from 1d points%22%22%22%0A    X = %5Bnp.ones(len(x))%5D%0A    for n in range(degree):%0A        X.append(x ** (n + 1))%0A    return np.array(X).T%0A%0A# generate points used to plot%0Ax_plot = np.linspace(0, 10, 100)%0A%0A# generate points and keep a subset of them%0Ax = np.linspace(0, 10, 100)%0Anp.random.shuffle(x)%0Ax = np.sort(x%5B:20%5D)%0Ay = f(x)%0A%0Apl.plot(x_plot, f(x_plot), label=%22ground truth%22)%0Apl.scatter(x, y, label=%22training points%22)%0A%0Afor degree in %5B3, 4, 5%5D:%0A    ridge = Ridge()%0A    ridge.fit(vandermonde(x, degree), y)%0A    pl.plot(x_plot, ridge.predict(vandermonde(x_plot, degree)),%0A            label=%22degree %25d%22 %25 degree)%0A%0Apl.legend(loc='lower left')%0A%0Apl.show()%0A

Add polynomial interpolation example.