I have used the following values:
learning rate = 0.0001
No. of iterations = 50 and 300 (no change in accuracy)
Accuracy is -50143577
HI please share your code as well
I have made some changes in the code, now the accuracy is -95%
import numpy as np import pandas as pd import matplotlib.pyplot as plt import openpyxl dfx = pd.read_csv(“Linear_X_Train.csv”) dfy = pd.read_csv(“Linear_Y_Train.csv”) df_test = pd.read_csv(“Linear_X_Test.csv”) workbook1 = openpyxl.load_workbook(‘Predicted_values.xlsx’) train = workbook1.worksheets[0] X = dfx.values Y = dfy.values xtest = df_test.values print(X.shape) print(Y.shape) # Visualize and pre-processing x = (X-X.mean())/X.std() y = Y “”" plt.scatter(X, Y, color=“black”, label=“raw data”) plt.scatter(x, y, color=“red”, label=“normalized data”) plt.xlabel(“X”) plt.ylabel(“Y”) plt.legend() plt.show() “”" # Gradient Descent def hypothesis(x, theta): return theta[0]+theta[1]x def error(x, y, theta): m = x.shape[0] err = 0 for i in range(m): hx = hypothesis(x[i], theta) err += (hx - y[i])/2 return err def gradient(x, y, theta): grad = np.zeros((2,)) m = x.shape[0] for i in range(m): hx = hypothesis(x[i], theta) grad[0] += (hx-y[i]) grad[1] += (hx - y[i])x[i] return grad # Algorithm def gradientDescent(x, y, learning_rate = 0.0001): error_list = [] theta = np.zeros((2,)) itr = 0 max_itr = 200 while itr < max_itr: grad = gradient(x, y, theta) e = error(x, y, theta) error_list.append(e) theta[0] = theta[0] - learning_rategrad[0] theta[1] = theta[1] - learning_rategrad[1] itr += 1 return theta, error_list final_theta, error_list = gradientDescent(x, y) plt.plot(error_list, label=“Error function”) plt.show() print(final_theta) print(“Min Value”, end=" “) print(x.min()) print(“Max Value”, end=” ") print(x.max()) plt.scatter(x, y, color=“red”, label=“normalized data”) plt.xlabel(“X”) plt.ylabel(“Y”) size = xtest.shape[0] ytest = list(hypothesis(xtest, final_theta)) for i in range(1, size+1): j = train.cell(row=i, column=1) j.value = y[i][0] workbook1.save(‘Predicted_values.xlsx’) plt.plot(xtest, ytest, color=“green”, label=“Predicted Values”) plt.legend() plt.show()
I found the error. Thanks for your time!
Okay no problem. Keep posting!