<jupyter_start><jupyter_text>Generic Gradient Descent Code Start<jupyter_code>#iterate through all the data points and find the slope 
#for generic gradient descent you have only one array of m and the last c have a coeff of 1
def step_grad(points,learning_rate,m):
    m_slope =np.zeros(len(points[0]))
    M = len(points)
    for i in range(M):
      #  x = points[i,0] this will not work we have to cal x_total (m1x1+m2x2+......)
        x_total =0
        for j in range((len(points[0])-1)):
            x_total += m[j]*points[i,j]

        y = points[i,(len(points[0])-1)]
        l=0
        for k in range((len(points[0])-1)):
            m_slope[l] += (-2/M)*(y-x_total)*points[i,k]
            l=l+1
        #c_slope += (-2/M)*(y-m*x-c)
    new_m=list([0 for j in range(len(points[0])-1)])
    a=0
    for i in range((len(points[0])-1)):
        new_m[i]=m[i]-learning_rate*m_slope[i]
    #new_m = m - learning_rate * m_slope
   # new_c = c - learning_rate * c_slope
    return new_m
#as said in gd defnition we have to start m&c with any random value
#substract the slope from the m or c till num_iter
def gd(points,learning_rate,num_iter):
    m=np.zeros(points.shape[1])
    #c=0
    for i in range(num_iter):
        m = step_grad(points,learning_rate,m)
        #use cost function not for calculating gd but for getting yourself idea of which way code is going!
        #print(i,"cost:")
        print(i,"cost:",cost(points,m))
    return m
#we have to load data and send it to gd function to figure out m & c
#gd requiires learning rate & number of iter
#generic data requires addition of 1 as the coeff of c to each row
def run():
    data = np.loadtxt("training_boston_x_y_train.csv",delimiter=',')
    M = len(data[0])
    x = data[:,0:(M-1)]
    y = data[:,M-1:]
   # x = preprocessing.scale(x_unscaled)
    z = np.ones((len(data),1))
    new_d = np.hstack((x,z))
    new_d = np.hstack((new_d,y))
    learning_rate = 0.1
    num_iter =100
    m = gd(new_d,learning_rate,num_iter)
    #print(m)
    return m
#no we need a predict fuction which pedicts the data by reading the values from test set
def predict(points,m):
    z = np.ones((len(points),1))
    new_d = np.hstack((points,z))
    y_pred = np.zeros(len(new_d))
    for i in range(len(new_d)):
        #y_pred[i]
        for j in range(len(new_d[0])):
            y_pred[i] += m[j]*new_d[i,j]
    return y_pred
m = run()
dataTest = np.loadtxt("test_boston_x_test.csv",delimiter=',')
y_pred = predict(dataTest,m)
np.savetxt("bostontry2.csv",y_pred,fmt="%.5f")<jupyter_output>0 cost: 374.240355979
1 cost: 245.379096561
2 cost: 165.106141922
3 cost: 114.495086212
4 cost: 82.4343266428
5 cost: 62.0417046883
6 cost: 49.015474666
7 cost: 40.6551932602
8 cost: 35.2597283507
9 cost: 31.7541214926
10 cost: 29.457161634
11 cost: 27.9360070221
12 cost: 26.9149224129
13 cost: 26.217802952
14 cost: 25.731872823
15 cost: 25.3846991177
16 cost: 25.1296035256
17 cost: 24.9363844105
18 cost: 24.785404792
19 cost: 24.6638182219
20 cost: 24.5631551436
21 cost: 24.4777765182
22 cost: 24.4038812007
23 cost: 24.3388674349
24 cost: 24.2809211483
25 cost: 24.2287497324
26 cost: 24.1814093006
27 cost: 24.1381921201
28 cost: 24.0985528595
29 cost: 24.0620599382
30 cost: 24.028363159
31 cost: 23.9971719425
32 cost: 23.9682404978
33 cost: 23.9413575577
34 cost: 23.9163391409
35 cost: 23.8930233395
36 cost: 23.8712664786
37 cost: 23.8509402168
38 cost: 23.8319293041
39 cost: 23.8141298077
40 cost: 23.7974476758
41 cost: 23.7817975557
42 cost: 23.7671018013
43 cost: 23.7532896315
44 c[...]
{'fraction_non_alphanumeric': 0.12851518560179978, 'fraction_numerical': 0.18082114735658042, 'mean_word_length': 3.4406991260923845, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '67231', 'license': 'MIT', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1427, 'n_words': 395, 'path': '07. MultivariableRegression/genericGradientDescentTry2.ipynb', 'repo_name': 'yaminikonka/ML-Notebook', 'size': '3556'}