def validate(M, lam):
    X_train, Y_train = regressTrainData()
    X_validate, Y_validate = regressValidateData()

    phi_train = phi(X_train, M)
    w = w_ml(phi_train, Y_train, lam, M)

    phi_validate = phi(X_validate, M)
    Y_approx = np.dot(phi_validate, w)
    sse = np.linalg.norm(Y_validate - Y_approx)

    return sse
    M = np.arange(1, 10)
    results = {}
    for m in M:
        lam, w, ss = find_lam(X_train, Y_train, m, 0.5, 0.01, 0.1)
        print m, lam, ss
        results[m] = {'w': w,
                      'lam': lam,
                      'sse': ss}
    for key, value in results.iteritems():
        print "{}: {}".format(key, value)
    """
    M = 2
    w = [1.12338195, 0.83457492]

    X_train, Y_train = regressTrainData()
    X_test, Y_test = regressTestData()
    X_val, Y_val = regressValidateData()

    pts = [p for p in np.linspace(min(X_test), max(X_test), 100)]
    plt.plot(X_test.T.tolist()[0], Y_test.T.tolist()[0], 'gs')

    y_test_approx = []
    for pt in pts:
        s = 0
        for k in range(M):
            s += w[k] * (pt ** k)
        y_test_approx.append(s)

    plt.plot(pts, y_test_approx)
    plt.title('Test dataset')