Source

ml-class / ex5.py

#!/usr/bin/env python

from scipy.io import loadmat
import matplotlib.pylab as plt
from sklearn import linear_model
import numpy as np

def error(clf, xs, ys):
    return np.power(clf.predict(xs) - ys, 2).sum() / (2 * xs.shape[0])

alphas = 


def go():
    data = loadmat('ex5/ex5data1.mat')
    xs = range(1, data['X'].shape[0]+1)
    tys = []
    vys = []
    for m in xs:
        clf = linear_model.RidgeRegression()
        clf.fit(data['X'][:m], data['y'][:m])
        tys.append(error(clf, data['Xtest'][:m], data['ytest'][:m]))
        vys.append(error(clf, data['Xval'][:m], data['yval'][:m]))


    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(xs, tys, label='Train')
    ax.plot(xs, vys, label='Cross Validation')
    plt.legend()
    fig.show()

    return fig

if __name__ == '__main__':
    fig = go()
    fig.ginput()