<jupyter_start><jupyter_text>Simple Linear RegressionThe goal of this project was to build a linear regression model from the ground up using numpy.<jupyter_code>%matplotlib inline

# upload the data file
from google.colab import files
uploaded = files.upload()

# imports
from numpy import *
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Import the dataHere, we're using a dataset with two columns containing the amount of hours studied and the test scores students achieved, respectively.<jupyter_code>points = genfromtxt('data.csv', delimiter=',')

#Extract columns
x = array(points[:,0])
y = array(points[:,1])

#Plot the dataset
plt.scatter(x,y)
plt.xlabel('Hours of study')
plt.ylabel('Test scores')
plt.title('Dataset')
plt.show()<jupyter_output><empty_output><jupyter_text>Defining the hyperparameters<jupyter_code>#hyperparameters
learning_rate = 0.0001
initial_b = 0
initial_m = 0
num_iterations = 10<jupyter_output><empty_output><jupyter_text>Define cost function<jupyter_code>def compute_cost(b, m, points):
    total_cost = 0
    N = float(len(points))
    
    #Compute sum of squared errors
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        total_cost += (y - (m * x + b)) ** 2
        
    #Return average of squared error
    return total_cost/N<jupyter_output><empty_output><jupyter_text>Define Gradient Descent functions<jupyter_code>def gradient_descent_runner(points, starting_b, starting_m, learning_rate, num_iterations):
    b = starting_b
    m = starting_m
    cost_graph = []

    #For every iteration, optimize b, m and compute its cost
    for i in range(num_iterations):
        cost_graph.append(compute_cost(b, m, points))
        b, m = step_gradient(b, m, array(points), learning_rate)

    return [b, m, cost_graph]

def step_gradient(b_current, m_current, points, learning_rate):
    m_gradient = 0
    b_gradient = 0
    N = float(len(points))

    #Calculate Gradient
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        m_gradient += - (2/N) * x * (y - (m_current * x + b_current))
        b_gradient += - (2/N) * (y - (m_current * x + b_current))
    
    #Update current m and b
    m_updated = m_current - learning_rate * m_gradient
    b_updated = b_current - learning_rate * b_gradient

    #Return updated parameters
    return b_updated, m_updated<jupyter_output><empty_output><jupyter_text>Run gradient_descent_runner() to get optimized parameters b and m<jupyter_code>b, m, cost_graph = gradient_descent_runner(points, initial_b, initial_m, learning_rate, num_iterations)

#Print optimized parameters
print ('Optimized b:', b)
print ('Optimized m:', m)

#Print error with optimized parameters
print ('Minimized cost:', compute_cost(b, m, points))<jupyter_output>Optimized b: 0.02963934787473239
Optimized m: 1.4774173755483797
Minimized cost: 112.65585181499746<jupyter_text>Plotting the cost per iterations<jupyter_code>plt.plot(cost_graph)
plt.xlabel('No. of iterations')
plt.ylabel('Cost')
plt.title('Cost per iteration')
plt.show()<jupyter_output><empty_output><jupyter_text>Gradient descent converges to local minimum after 5 iterations  Plot line of best fit<jupyter_code>#Plot dataset
plt.scatter(x, y)
#Predict y values
pred = m * x + b
#Plot predictions as line of best fit
plt.plot(x, pred, c='r')
plt.xlabel('Hours of study')
plt.ylabel('Test scores')
plt.title('Line of best fit')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10758226037195995, 'fraction_numerical': 0.021745350500715306, 'mean_word_length': 4.768976897689769, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '384034', 'license': 'MIT', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1181, 'n_words': 324, 'path': 'Linear_regression.ipynb', 'repo_name': 'pieter98/Algorithm-Implementations', 'size': '3495'}