# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.9.6 64-bit
#     language: python
#     name: python3
# ---

# ## Approach-1: Gradient is ZERO (0)

# #### Our goal
# - We want the line that “best fits” this data set as measured by residual sum of squares -- the simple linear regression cost. We have a closed form solution that involves the following terms:
#
# - The number of data points (N)
# - The sum (or mean) of the Ys
# - The sum (or mean) of the Xs
# - The sum (or mean) of the product of the Xs and the Ys
# - The sum (or mean) of the Xs squared
#
# Then once we have calculated all of these terms, we can use the formulas to compute the slope and intercept. Recall that we first solve for the slope and then we use the value of the slope to solve for the intercept.  The formula for the slope is a fraction with:
#
# numerator = (sum of X*Y) - (1/N)*((sum of X) * (sum of Y))
# denominator = (sum of X^2) - (1/N)*((sum of X) * (sum of X))

# ### Calculating Gradient Descent

# +
import numpy as np
import matplotlib.pyplot as plt

X = np.array([0, 1, 2, 3, 4])
y = np.array([1, 3, 7, 13, 21])
# -

plt.scatter(X, y)

#Define the relevant variables
initial_intercept = 0
initial_slope = 0
step_size = 0.05
tolerance = 0.01

Xsum = np.sum(X)
Ysum = np.sum(y)
N = X.size
print("Xsum : " + str(Xsum))
print("Ysum : " + str(Ysum))
print("N : " + str(N))

#Slope calculation
numerator = (np.sum(X * y)) - ((1 / N) * Xsum * Ysum)
denom = (np.sum(X**2)) - ((1 / N) * Xsum * Xsum)
slope = numerator / denom
print("Num : " + str(numerator))
print("Denom : " + str(denom))
print("Result (SLOPE) : " + str(slope))


#intercept = (mean of Y) - slope * (mean of X)
intercept = np.mean(y) - slope * (np.mean(X))
print("Intercept : " + str(intercept))


# +
def abline(slope, intercept):
    y_vals = intercept + slope * X
    plt.plot(X, y_vals, '--')

abline(slope, intercept)
plt.scatter(X, y)
# -



StarcoderdataJupyterScriptsDedupFiltered