<jupyter_start><jupyter_text>This is an implementation of gradient descent to solve a hypothetical linear regression problem.<jupyter_code>import numpy as np  # Importing numpy
train_data = np.genfromtxt('train_data.csv', delimiter=',')   # Extracting data from csv file
train_data = np.delete(train_data, (0), axis=0)   # Deleting nan values 
theta = np.array([0,0,0,0,0])   # Initialising parameters
y =train_data[:,-1]             # Storing y values
X = train_data[:, 0:4]          # Creating feature/design matrix
m= len(train_data)              # Number of training examples
X= np.insert(X,0,np.ones(m),axis = 1)   # Setting x0 = 1, for all rows
alpha = 0.03              # Initialising learning rate
# Given below is a function used to calculate the cost/loss 

def calc_cost(hypothesis,y):
        diff = hypothesis - y
        s = np.sum(diff**2)    # Sum of square error
        c = s/(2*m)
        return c
hypothesis = np.dot(X,theta.transpose())   #Initial hypothesis
oldcost = calc_cost(hypothesis,y)        #Initial cost value
diff = hypothesis - y                   #Initial difference between hypothesis and target
#Given below is the iteration step that implements gradient descent. The derivative has been manually calculated.
ct = 1                        
while True:
        theta = theta - (alpha/m)*(np.dot(diff.transpose(),X))  #Theta simulataneous update.
        hypothesis = np.dot(X,theta.transpose())                #Hypothesis update
        diff = hypothesis - y
        newcost = calc_cost(hypothesis,y)                     #Cost value updated
        print "Iteration:-",ct," Cost = ",newcost
        ct = ct + 1
        if abs(newcost-oldcost) <= 0.000000001:                   #Checking for convergence 
                break
        else:
                oldcost = newcost
print "Weights arrived at:- ",theta                            #Display of parameters obtained 
predicted = np.dot(X,theta.transpose())                        #Values predicted by obtained theta
print predicted
print "The errors for each prediction are given in the following vector:-"
print predicted - y
test = np.genfromtxt('test_input.csv',delimiter = ',')
test= np.delete(test,(0),axis=0)
test= np.insert(test,0,np.ones(len(test)),axis = 1)
print np.dot(test,theta.transpose())        
np.savetxt("testop.csv", np.dot(test,theta.transpose()), delimiter=",")    #Saving output to  csv file<jupyter_output>[ 3.31604937  5.87503751  5.02232781  6.29173699  6.16101991  4.57736313
  6.24786945  4.26318882  2.99589932  3.95124102  4.7258695   5.2317555
  4.74352003  5.60291474  3.87386464  5.69359471  3.24434798  6.12528599
  6.26174438  3.33973578  4.53498646  4.38999898  5.31388612  5.20367742
  3.00707972  4.89261701  5.26880253  5.02521849  2.64763101  5.12793402
  5.27604995  6.11359695  4.7230054   6.02868251  5.99623008  4.60786157
  5.14516843  3.55570757  3.55850323  5.74555819  5.93309882  3.40577205
  4.90447584  6.05539417  4.09427658  6.090593    3.49089045  4.59480303
  3.70253152  5.2335223   5.62862115  5.67563615  4.80075231  4.70854673
  5.30874556  5.02084177  5.6692162   4.94511198  4.0704836   5.54820602]
{'fraction_non_alphanumeric': 0.10168414362885288, 'fraction_numerical': 0.1801715919923737, 'mean_word_length': 2.656213704994193, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '104031', 'license': 'MIT', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1077, 'n_words': 277, 'path': 'ML-Python/Week2/submissions/A3/Assignment-3_Mahir.ipynb', 'repo_name': 'darshanv6/SummerProjects17', 'size': '3147'}