Page MenuHomec4science

plots.py
No OneTemporary

File Metadata

Created
Sun, Feb 23, 20:38

plots.py

# -*- coding: utf-8 -*-
"""a function of ploting figures."""
import numpy as np
from build_polynomial import *
import matplotlib.pyplot as plt
def plot_fitted_curve(y, x, weights, degree, ax):
"""plot the fitted curve."""
ax.scatter(x, y, color='b', s=12, facecolors='none', edgecolors='r')
xvals = np.arange(min(x) - 0.1, max(x) + 0.1, 0.1)
tx = build_poly(xvals, degree)
f = tx.dot(weights)
ax.plot(xvals, f)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Polynomial degree " + str(degree))
def plot_train_test(train_errors, test_errors, lambdas, degree):
"""
train_errors, test_errors and lambas should be list (of the same size) the respective train error and test error for a given lambda,
* lambda[0] = 1
* train_errors[0] = RMSE of a ridge regression on the train set
* test_errors[0] = RMSE of the parameter found by ridge regression applied on the test set
degree is just used for the title of the plot.
"""
plt.semilogx(lambdas, train_errors, color='b', marker='*', label="Train error")
plt.semilogx(lambdas, test_errors, color='r', marker='*', label="Test error")
plt.xlabel("lambda")
plt.ylabel("RMSE")
plt.title("Ridge regression for polynomial degree " + str(degree))
leg = plt.legend(loc=1, shadow=True)
leg.draw_frame(False)
plt.savefig("ridge_regression")

Event Timeline