<jupyter_start><jupyter_text>Linear Regression from Scratch In this notebook, we implement linear regression and get to see it work on real data.  The file stockprices.csv contains the dataset we will use, it consists of stock prices of Amazon and Google.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D<jupyter_output><empty_output><jupyter_text>PredictionThe following is the predict method where the prediction is done using the dot product of theta and X. The prediction h_theta is given by :-First calculate the transpose of theta by theta.T, then calculate prediction by the dot product of X and theta transpose.This function takes as input a set of data point(s) X (X is a vector if there is more than one data point) and parameter vector θ.  It outputs the prediction(s) h_θ(x) for these input(s).<jupyter_code>def predict(X,theta):
  # takes m by n matrix X as input and returns an mx1 vector containing the predictions h_theta(x^i) for each row x^i, i=1,...,m in X
  ##### replace the next line with your code
 
  pred= X.dot(theta)
  return pred

def computeGradient(X,y,theta):
  # function calulate the gradient of J(theta) and returns its value
  ##### replace the next line with your code #####
  n=len(y)
  pred = X.dot(theta)
  val_1 = pred - y
  grad = X.T.dot(val_1)/n
  return grad
def gradientDescent(X, y, numparams):
  # iteratively update parameter vector theta
  # -- you should not modify this function
  
  # initialize variables for learning rate and iterations
  alpha = 0.02
  iters = 5000
  cost = np.zeros(iters)
  theta= np.zeros(numparams)

  for i in range(iters):
    theta = theta - alpha * computeGradient(X,y,theta)
    cost[i] = computeCost(X, y, theta)

  return theta, cost

def normaliseData(x):
  # rescale data to lie between 0 and 1
  scale = x.max(axis=0)
  return (x/scale, scale)<jupyter_output><empty_output><jupyter_text>Cost Function<jupyter_code>def computeCost(X, y, theta):
  # function calculates the cost J(theta) and return its value
  ##### replace the next line with your code #####
    cost = 0
    l = len(y);
    pred = X.dot(theta);
    val_1 = np.power((pred - y),2);
    cost = "{0:.3f}".format(sum(val_1)/(2*l));
    return cost

def splitData(X,y):
  # split data into training and test parts
  # ... for now, we use all of the data for training and testing
  Xtrain=X; ytrain=y; Xtest=X; ytest=y
  return (Xtrain,ytrain,Xtest,ytest)
  # load the data
  data=np.loadtxt('stockprices.csv',usecols=(1,2))
  X=data[:,0]
  y=data[:,1]
  
  # plot the data so we can see how it looks 
  # (output is in file graph.png)
  fig, ax = plt.subplots(figsize=(12, 8))
  ax.scatter(X, y, label='Data')
  ax.set_xlabel('Amazon')
  ax.set_ylabel('Google')
  ax.set_title('Google stock price vs Amazon')
  plt.show()


  # split the data into training and test parts
(Xtrain,ytrain,Xtest,ytest)=splitData(X,y)
  
  # add a column of ones to input data
m=len(y) # m is number of training data points
Xtrain = np.column_stack((np.ones((m, 1)), Xtrain))
(m,n)=Xtrain.shape # m is number of data points, n number of features

  

# rescale training data to lie between 0 and 1
(Xt,Xscale) = normaliseData(Xtrain)
(yt,yscale) = normaliseData(ytrain)

# calculate the prediction
print('testing the prediction function ...')
theta=(1,2)
print('when x=[1,1] and theta is [1,2]) cost = ',predict(np.ones(n),theta))
print('approx expected prediction is 3')
print('when x=[[1,1],[5,5]] and theta is [1,2]) cost = ',predict(np.array([[1,1],[5,5]]),theta))  
print('approx expected prediction is [3,15]')
  

print('testing the cost function ...')
theta=np.zeros(n)
print('when theta is zero cost = ',computeCost(Xt,yt,theta))
print('approx expected cost value is 0.318')
  

  # calculate the gradient when theta is zero
print('testing the gradient function ...')
print('when theta is zero gradient = ',computeGradient(Xt,yt,theta))
print('approx expected gradient value is [-0.79,-0.59]')
  
 
 # perform gradient descent to "fit" the model parameters
print('running gradient descent ...')
theta, cost = gradientDescent(Xt, yt, n)
print('after running gradientDescent() theta=',theta)
print('approx expected value is [0.34, 0.61]')

  
# plot some predictions
Xpred = np.linspace(X.min(), X.max(), 100)
Xpred = np.column_stack((np.ones((100, 1)), Xpred))
ypred = predict(Xpred/Xscale, theta)*yscale
fig, ax = plt.subplots(figsize=(12, 8))
ax.scatter(Xtest, ytest, color='b', label='Test Data')
ax.plot(Xpred[:,1], ypred, 'r', label='Prediction')
ax.set_xlabel('Amazon')
ax.set_ylabel('Google')
ax.legend(loc=2)
fig.savefig('pred.png')
plt.show()

# and plot how the cost varies as the gradient descent proceeds
fig2, ax2 = plt.subplots(figsize=(12, 8))
ax2.semilogy(cost,'r')
ax2.set_xlabel('iteration')
ax2.set_ylabel('cost')
#fig2.savefig('cost.png')
plt.show()  
 


 # plot the cost function
fig3 = plt.figure()
ax3 = fig3.add_subplot(1, 1, 1, projection='3d')
n=100
theta0, theta1 = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 2, n))  
cost = np.empty((n,n))
for i in range(n):
    for j in range(n):
      cost[i,j] = computeCost(Xt,yt,(theta0[i,j],theta1[i,j]))
ax3.plot_surface(theta0,theta1,cost)
ax3.set_xlabel('theta0')
ax3.set_ylabel('theta1')
ax3.set_zlabel('J(theta)')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1370377323071147, 'fraction_numerical': 0.02177585883236343, 'mean_word_length': 4.584905660377358, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '436610', 'license': 'MIT', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1848, 'n_words': 590, 'path': 'Linear Regression/linear_regression.ipynb', 'repo_name': 'someaditya/Machine-Learning-from-Scratch', 'size': '5327'}