<jupyter_start><jupyter_text>Import libraries:<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Load boston dataset from sklearn:<jupyter_code>from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Split data into 20% test and 80% train:<jupyter_code>X, y = load_boston(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train[0]<jupyter_output><empty_output><jupyter_text>Mean normalization:<jupyter_code>from utils import mean_normalize
X_train, X_test = mean_normalize(X_train, X_test)<jupyter_output><empty_output><jupyter_text>Add a column of ones left hand-side:<jupyter_code>X_train = np.c_[np.ones(X_train.shape[0]), X_train]
X_test = np.c_[np.ones(X_test.shape[0]), X_test]<jupyter_output><empty_output><jupyter_text>Compute cost:<jupyter_code>def compute_cost(X, Y, theta):
    landa = 0.1
    m = len(Y)
    J = (1/(2*m)) * (sum((np.matmul(X, theta) - Y)**2) + landa*sum(theta[1: ]**2))
    return J
compute_cost(X_train, y_train, np.zeros(X_train.shape[1]))
def gradientDescent(X, y, theta, alpha):
    j = []
    lamda = 0.001
    temp = [0 for _ in range(X.shape[1])]
    batchSize = 50
    batchCost = [0 for i in range(X.shape[1])]
    k = 0
    rounds = 0
    while True:
        rounds += 1
        
        if k % batchSize == 0:
            batchCost = [0 for i in range(X.shape[1])]
            temp[0] = theta[0] - alpha*(batchCost[0]+(sum(np.multiply((np.matmul(X, theta) - y),  X[:,0]))/X.shape[0]))
            for i in range(1, X.shape[1]):
                temp[i] = theta[i] - alpha*(batchCost[i]+(sum(np.multiply((np.matmul(X, theta) - y),  X[:,i])/X.shape[0] + lamda*theta[i]/X.shape[0])))
            theta = np.array(temp)
        else:
            batchCost[0] += sum(np.multiply((np.matmul(X, theta) - y),  X[:,0]))/X.shape[0]
            for i in range(1, X.shape[1]):
                batchCost[i] += sum(np.multiply((np.matmul(X, theta) - y),  X[:,i]))/X.shape[0] + lamda*theta[i]/X.shape[0]
        
        k += 1
        j.append(computeCost(X, y, theta))
        
        #if j.len() >= 2 and j[j.len()-1] - j[j.len()-2] < 0.000000001:
        if rounds > 1000:
            break
    return theta, j
def gradient_descent(X, Y, theta, alpha):
    landa = 0.1
    m = len(Y)
    J_history = []
    tmp = []
    batch_size = 50
    batch_cost = np.zeros(X.shape[1])
    rounds = 0

    while True:
        
        if rounds % batch_size == 0:
            
            
        tmp.append(theta[0] - alpha * (1/m) * sum(np.multiply(np.matmul(X, theta) - Y, X[:, 0])))
        for i in range(1, X.shape[1]):
            tmp.append(theta[i] - alpha * (1/m) * sum(np.multiply(np.matmul(X, theta) - Y, X[:, i])) + landa*theta[i] * 1/m)
        
        theta = np.array(tmp)
        tmp = []
        J_history.append(compute_cost(X, Y, theta))
        
        rounds += 1
        if J_history.__len__() > 2:
            if J_history[-1] - J_history[-2] < 0:
        # if k == 3000:
                break
        
    return J_history, theta
J_history, theta = gradient_descent(X_train, y_train, [0]*14, 0.01)
plt.plot(range(J_history.__len__()), J_history)
J_history[-1] - J_history[-2]
import math
math.isclose(J_history[-1], J_history[-2])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1636469900642899, 'fraction_numerical': 0.029807130333138514, 'mean_word_length': 2.85472972972973, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '638872', 'license': 'Apache-2.0', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1335, 'n_words': 258, 'path': 'HomeWorks/HW1/Untitled.ipynb', 'repo_name': 'TheMn/Machine-Learning-Algorithms-Spring2022', 'size': '3422'}