<jupyter_start><jupyter_text>1. Regularized Linear Regression  1.1 visualizing the dataset<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.io import loadmat 
import scipy.optimize as opt
data = loadmat('ex5data1.mat')
data<jupyter_output><empty_output><jupyter_text>> 1. a training set: X, y    > 2. a cross validation set: Xval, yval    > 3. a test set: xtest, ytest<jupyter_code>def load_data(data):    
    X = data['X']
    Xtest = data['Xtest']
    Xval = data['Xval']
    y = data['y']
    ytest = data['ytest']
    yval = data['yval']
    return X, Xtest, Xval, y, ytest, yval
X, Xtest, Xval, y, ytest, yval = load_data(data)
# 增加常数项
X, Xval, Xtest = [np.insert(item, 0, np.ones(item.shape[0]), axis=1) for item in (X, Xval, Xtest)]
X.shape, y.shape
fig, ax = plt.subplots(figsize=(10, 8))
ax.scatter(X, y, c='red', marker='x')
plt.xlabel('Change in water level(x)')
plt.ylabel('Watering flowing out of the dam(y)')
plt.show()<jupyter_output><empty_output><jupyter_text>1.2 Regularized linear regression ost function regularized linear regression cost function:$J\left( \theta \right) =\dfrac {1}{2m}\sum ^{m}_{i=1}\left( h_{\theta }\left( x^\left({i}\right)\right) -y\left( i\right)\right)^{2}+\dfrac {\lambda}{2m}\left( \sum ^{n}_{i=1}\theta ^{2}_{j}\right) $<jupyter_code>def regularized_cost(X, y, theta, learning_rate=1):
    """
    X: (m, n)
    y: (m, 1)
    theta: (n, 1), linear regression parameters
    learning_rate: regularized parameter
    """
    m = X.shape[0]
    
    X = np.mat(X)
    y = np.mat(y)
    theta = np.mat(theta)
    
    inner = X * theta - y
    square_sum = inner.T * inner # (1, m) * (m, 1) -> (1, 1)
    cost = square_sum / (2 * m) + (learning_rate) / (2 * m) * (theta[1:].T * theta[1:])
    
    return np.ravel(cost)[0]
m, n = X.shape
theta = np.ones((n, 1))
regularized_cost(X, y, theta)<jupyter_output><empty_output><jupyter_text>1.3 regularized linear regression gradient j=0：$\dfrac {\partial J\left( \theta \right) }{\partial \theta _{0}}=\dfrac {1}{m}\sum ^{m}_{i=1}\left( h_{\theta }\left( x^\left({i}\right)\right) -y\left( i\right)\right)x_{j}\left( i\right)$j>=1：$\dfrac {\partial J\left( \theta \right) }{\partial \theta _{0}}=\dfrac {1}{m}\sum ^{m}_{i=1}\left( h_{\theta }\left( x^\left({i}\right)\right) -y\left( i\right)\right)x_{j}\left( i\right)+\dfrac {\lambda}{m}\theta _{j}$<jupyter_code>def regularized_gradient(X, y, theta, learning_rate=1):
    m = X.shape[0]
    
    X = np.mat(X)
    y = np.mat(y)
    theta_copy = theta.copy()
    theta_copy = np.mat(theta_copy)
    inner = X.T * (X * theta - y)
    
    theta_copy[0] = 0
    gradient = inner / m + (learning_rate) / m * theta_copy
    return gradient
regularized_gradient(X, y, theta)<jupyter_output><empty_output><jupyter_text>1.4 fitting linear regression<jupyter_code># set regularization parameters to 0
def linear_regression(X, y, theta, learning_rate=0, alpha=0.002, iter_nums=500):
    m = X.shape[0]
    
    for i in range(iter_nums):
        theta -= alpha * regularized_gradient(X, y, theta)
    return theta
theta = np.ones((n, 1))
final_theta = linear_regression(X, y, theta)
final_theta
fig, ax = plt.subplots(figsize=(10, 8))
ax.scatter(X[:, 1], y, c='r', marker='x', label="Training data")
plt.plot(X[:, 1],np.dot(X, final_theta), label="Prediction")
plt.xlabel('Change in water level(x)')
plt.ylabel('Watering flowing out of the dam(y)')
plt.legend(loc='best')
plt.show()<jupyter_output><empty_output><jupyter_text>2. Bias-variance > 机器学习中一个很重要的概念就是bias-variance，具有high bias的模型不够复杂，倾向于underfit，而high variance模型则通常会overfit  2.1 learing curves 绘制learning curves，我们需要training set和cross validation set $J_{train}\left( \theta \right) =\dfrac {1}{2m}\left[ \sum ^{m}\left(h_{\theta }\left( x^{i}\right) -y\left( i\right)\right) ^{2}\right]$<jupyter_code>def plot_learning_curves(X, y, Xval, yval, learning_rate=0):
    training_cost = []
    cv_cost = []
    m, n = X.shape
    for i in range(1, m + 1):
        theta = np.ones((n, 1))
        res = linear_regression(X[:i, :], y[:i], theta, learning_rate=learning_rate, alpha=0.001, iter_nums=500)

        tc = regularized_cost(X[:i, :], y[:i], res, learning_rate=learning_rate)
        cv = regularized_cost(Xval, yval, res, learning_rate=learning_rate)

        training_cost.append(tc)
        cv_cost.append(cv)
    
    # plot
    fig, ax = plt.subplots(figsize=(10, 8))
    ax.plot(np.arange(1, m+1), training_cost, label='training cost')
    ax.plot(np.arange(1, m+1), cv_cost, label='cv cost')
    plt.legend(loc='best')
    plt.show()
plot_learning_curves()<jupyter_output><empty_output><jupyter_text>看图像发现拟合模型具有**high bias**， 说明模型**underfit**  3. Polynomial regression 模型太简单，可以通过增加更多特征来弥补$h_{\theta }\left( x\right) =\theta _{0}+\theta _{1}x_{1}+\ldots +\theta _{p}x_{p}$<jupyter_code>def prepare_poly_data(X, power=8):
    for i in range(2, power+1):
        columns = np.power(X[:, 0], i)
        X = np.insert(X, i-1, values=columns, axis=1)
    return X
X, Xtest, Xval, y, ytest, yval = load_data(data)<jupyter_output><empty_output><jupyter_text>3.1 learning polynomial regression<jupyter_code># 数据的归一化
def normalize_feature(X):
    X =  pd.DataFrame(X)
    return ((X - X.mean()) / X.std()).as_matrix()
X, Xtest, Xval = map(normalize_feature, map(prepare_poly_data, [X, Xtest, Xval]))
# 增加常数项
X, Xval, Xtest = [np.insert(item, 0, np.ones(item.shape[0]), axis=1) for item in (X, Xval, Xtest)]
X<jupyter_output><empty_output><jupyter_text>$\lambda =0$<jupyter_code>plot_learning_curves(X, y, Xval, yval, learning_rate=0)<jupyter_output><empty_output><jupyter_text>$\lambda =1$<jupyter_code>plot_learning_curves(X, y, Xval, yval, learning_rate=1)<jupyter_output><empty_output><jupyter_text>$\lambda =100$<jupyter_code>plot_learning_curves(X, y, Xval, yval, learning_rate=100)<jupyter_output><empty_output><jupyter_text>3.2 selecting $\lambda$ using a cross validation set<jupyter_code>candidate = [0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10]
training_cost = []
cv_cost = []
for i in candidate:
    m, n = X.shape

    theta = np.ones((n, 1))
    res = linear_regression(X, y, theta, learning_rate=i, alpha=0.001, iter_nums=500)

    tc = regularized_cost(X, y, res, learning_rate=i)
    cv = regularized_cost(Xval, yval, res, learning_rate=i)

    training_cost.append(tc)
    cv_cost.append(cv)
fig, ax = plt.subplots(figsize=(10, 8))
ax.plot(candidate, training_cost, label='training')
ax.plot(candidate, cv_cost, label='cross validation')
plt.legend(loc='best')

plt.xlabel('$\lambda$')
plt.ylabel('cost')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17582916855974556, 'fraction_numerical': 0.023322732091473574, 'mean_word_length': 4.530988274706868, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '639171', 'license': 'MIT', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2655, 'n_words': 559, 'path': 'chapter5/Regularized Linear Regression and Bias v.s. Variance.ipynb', 'repo_name': 'plantree/Machine-Learning-Andrew-Ng', 'size': '6603'}