<jupyter_start><jupyter_text>Linear Regression from Scratch<jupyter_code># used for manipulating directory paths
import os

# Scientific and vector computation for python
import numpy as np

# Plotting library
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D  # needed to plot 3-D surfaces

# tells matplotlib to embed plots within the notebook
%matplotlib inline
# Read comma separated data
data = np.loadtxt(os.path.join('data.txt'), delimiter=',')
X, y = data[:, 0], data[:, 1]

m = y.size  # number of training examples
print(m)<jupyter_output>97<jupyter_text>Plotting the Data<jupyter_code>def plotData(x, y):
  
    fig = pyplot.figure()  # open a new figure

    pyplot.plot(x, y, 'ro', ms=10, mec='k')
    pyplot.ylabel('Profit in $10,000')
    pyplot.xlabel('Population of City in 10,000s')


plotData(X, y)<jupyter_output><empty_output><jupyter_text>Gradient Descent<jupyter_code># Add a column of ones to X. The numpy function stack joins arrays along a given axis. 
# The first axis (axis=0) refers to rows (training examples) 
# and second axis (axis=1) refers to columns (features).
X = np.stack([np.ones(m), X], axis=1)<jupyter_output><empty_output><jupyter_text>Computing the cost $J(\theta)$<jupyter_code>def computeCost(X, y, theta):

    
    m = y.size  # number of training examples
    
    # You need to return the following variables correctly
    J = 0
    
    y_predict = np.dot(X,theta)
    sqrd_error = (y_predict-y)**2
    cost=sum(sqrd_error)
    J=(1/(2*m)*cost)
    #J=(np.sum(np.square(np.dot(X,theta)-y)))/(2*m)
    
    return J
# YOU CAN CHANGE THE VALUE OF A,B FOR DIFFERENT INITIALIZATIONS OF THETA
A = 0.0
B = 0.0
J = computeCost(X, y, theta=np.array([A, B]))
print('With theta = [0, 0] \nCost computed = %.2f' % J)

# YOU CAN CHANGE THE VALUE OF A,B FOR DIFFERENT INITIALIZATIONS OF THETA
A = -1
B = 2
J = computeCost(X, y, theta=np.array([A, B]))
print('With theta = [-1, 2] \nCost computed = %.2f' % J)

def gradientDescent(X, y, theta, alpha, num_iters):
    
    #
    m = y.shape[0]  # number of training examples
    
    # make a copy of theta, to avoid changing the original array, since numpy arrays
    # are passed by reference to functions
    theta = theta.copy()
    
    J_history = [] # Use a python list to save cost in every iteration
    
    for i in range(num_iters):
        # ==================== YOUR CODE HERE =================================
        error = np.dot(X,theta)-y
        temp0 = theta[0]-((alpha/m) * np.sum(error))
        temp1 = theta[1]-((alpha/m) * np.sum(error*X[:,1]))
        theta = np.array([temp0,temp1])
        # =====================================================================
        
        # save the cost J in every iteration
        J_history.append(computeCost(X, y, theta))
    
    return theta, J_history
# initialize fitting parameters
# FEEL FREE TO CHANGE ANY PARAMETER
A = 0.0
B = 0.0
theta = np.array([A,B]) 

# some gradient descent settings
iterations = 1500
alpha = 0.01

theta, J_history = gradientDescent(X ,y, theta, alpha, iterations)
print('Theta found by gradient descent: {:.4f}, {:.4f}'.format(*theta))
theta<jupyter_output><empty_output><jupyter_text>We will use your final parameters to plot the linear fit. The results should look like the following figure.<jupyter_code># plot the linear fit
plotData(X[:, 1], y)
pyplot.plot(X[:, 1], np.dot(X, theta), '-')
pyplot.legend(['Training data', 'Linear regression']);
# Now you can predict values for various population sizes.
population_size = 35000
population_size = population_size/10000

predict1 = np.dot([1, population_size], theta)
print('For population = ' + str(population_size*10000))
print('we predict a profit of {:.2f}\n'.format(predict1*10000))<jupyter_output>For population = 35000.0
we predict a profit of 4519.77
{'fraction_non_alphanumeric': 0.1532426778242678, 'fraction_numerical': 0.025627615062761507, 'mean_word_length': 3.986962190352021, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '173912', 'license': 'MIT', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1274, 'n_words': 399, 'path': 'Linear Regression from Scratch.ipynb', 'repo_name': 'Ajaysinghkhalsa/Linear-Regression-from-Scratch', 'size': '3824'}