24
loading...
This website collects cookies to deliver better user experience
x = [1,2,3]
and y = [1,2,2]
and we are going to fit the best possible line on this dataset.y = wx + b
where w is the slope and b is the intercept. In machine learning lingo we call w as weight and b as bias. For above line it came out to be w = 0.5 and *b = 0.667 * (Don't worry! we'll see how).The inner product of two vectors a and b can be found by calculating aTb
NormalLinearRegression
to handle all the computations for us and return the optimal values of weights.Note: All the code files can be found on Github through this link.
And it's highly recommended to follow the notebook along with this section for better understanding.
class NormalLinearRegression:
def __init__(self) -> None:
self.X = None
self.Y = None
self.theta = None
def fit(self,x,y):
"""
Returns the optimal weights.
parameters:
x : input/feature matrix
y : target matrix
Returns:
theta: Array of the optimal value of weights.
"""
self.X = x
if self.X.ndim == 1: # adding extra dimension, if X is a 1-D array
self.X = self.X.reshape(-1,1)
# adding extra column of 1s for the bias term
self.X = np.concatenate([np.ones((self.X.shape[0], 1)), self.X], axis=1)
self.Y = y
self.theta = np.zeros((self.X.shape[1],1))
self.theta = self.calculate_theta()
self.theta = self.theta.reshape(-1,1)
return self.theta
def predict(self, x):
"""
Returns the predicted target.
parameters:
x : test input/feature matrix
Returns:
y: predicted target value.
"""
x = np.array(x) # converting list to numpy array
if x.ndim == 1:
x = x.reshape(1,-1) # adding extra dimension in front
x = np.concatenate([np.ones((x.shape[0],1)), x], axis=1)
return np.dot(x,self.theta)
def calculate_theta(self):
"""
Calculate the optimal weights.
parameters: None
Returns:
theta_temp: Array containing the calculated value of weights
"""
y_projection = np.dot(self.X.T, self.Y)
cov = np.dot(self.X.T, self.X)
cov_inv = np.linalg.pinv(cov)
theta_temp = np.dot(cov_inv, y_projection)
return theta_temp
theta
matrix had both weight and bias terms so we just added an extra column of 1s so that matrix multiplication handles the addition of bias term.NormalLinearRegression
class to find the best hypothesis to fit on our data.params
. Class NormalLinearRegression
had a method predict
that we can use to get the predictions and after that, we can use them to draw the hypothesis as shown below.If you're wondering how I plot them, just visit the repo for this algorithm through this link and you'll find the notebook where all the implementations are already done for you.
predict
method of our class.np.linalg.pinv()
function to calculate the inverse and it uses Singular Value Decomposition to return the pseudo-inverse of the matrix if it's non-invertible.calculate_theta
method of our class which is reponsible for the calculation of (XTX)-1XTY .calculate_theta
method should look something like this:def calculate_theta(self, lambda_):
"""
Calculate the optimal weights.
parameters: None
Returns:
theta_temp: Array containing the calculated value of weights
"""
y_projection = np.dot(self.X.T, self.Y)
# Creating matrix M (identity matrix with fist element 0)
M = np.identity(self.X.shape[1])
M[0,0] = 0
cov = np.dot(self.X.T, self.X) + lambda_*M # adding lambda_ times M to X.T@X
cov_inv = np.linalg.pinv(cov)
theta_temp = np.dot(cov_inv, y_projection)
return theta_temp
errors
let's print it out and see which λ got the least error.