Hill climb on linear regression

Viewed 21

HI I am required to fill the linear regression line on to below graph the required thing is we need to find the optimal a. Can anybody help how this can be done

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

X=np.linspace(0,3,30)
Y=2.5*np.linspace(0,3,30)+5*np.random.rand(30)
fig,ax=plt.subplots()
ax.set_title('Regressionlinetofit')
ax.set_ylabel('Y')
ax.set_xlabel('X')
ax.plot(X,Y,label='linetofit')
ax.legend()
plt.show()

enter image description here

You will have a visualization as in Figure 3: Figure 3: Regression line to fit You will need to fit a linear regression to this line plot and the regression equation is like yf it = a ∗ X To fit the line, you will need to find the optimal a here, and you could follow hill climb as below steps: • defining the total rounds of run n, randomly giving value to a in first round, • calculating the error (yfit − Y ), • adjusting the a for little as aadjust, and get new yfit = aadjust ∗ X, • calculating the errornew (yfit − Y ), • if the errornew < error, then a = aadjust and error = errornew. • Finishing all n rounds

0 Answers
Related