Skip to content

Instantly share code, notes, and snippets.

@Momellouky
Last active September 15, 2024 10:24
Show Gist options
  • Save Momellouky/bc854a1f21a232a1fba175486ce4925e to your computer and use it in GitHub Desktop.
Save Momellouky/bc854a1f21a232a1fba175486ce4925e to your computer and use it in GitHub Desktop.
import numpy as np
from sklearn.preprocessing import add_dummy_feature
class LinearRegression:
def __init__(self):
self.X = None
self.y = None
self.theta = None
self.coaf_ = None
self.intercept_ = None
def predict(self, X):
'''
With the input X the functions uses the model to makes predictions
:param X: The independent variables.
:return: A numpy array containing the predicted values.
'''
predictions = np.array([])
for element in X:
predictions = np.append(predictions, element * self.coaf_ + self.intercept_)
return predictions
def fit(self, X, y, epoch=1000, learning_rate=0.1):
'''
train the linear regressor using the Batch Gradient Descent algorithm
:param X: The independent variables.
:param y: The dependent variables.
:return: the trained model.
'''
self.X = X
self.y = y
self.X = add_dummy_feature(self.X)
np.random.seed(42)
self.theta = np.random.randn(self.X.shape[1]) # Random initialisation
self.theta = self.theta.reshape(-1, 1)
m = self.X.shape[0] # The number of items
for i in range(epoch):
predictions = (self.X @ self.theta).reshape(-1, 1)
gradient = 2 / m * self.X.T @ (predictions - self.y)
self.theta = self.theta - learning_rate * gradient
self.coaf_ = self.theta[1:]
self.intercept_ = self.theta[0]
return self
def score(self, X, y):
'''
Evaluates the model on the test data using Mean Squared Error
:param X: independent variables.
:param y: dependent variables.
:return: Mean Squared Error value
'''
predictions = self.predict(X)
m = predictions.shape[0] # the size of the test data.
mae = 0
for i in range(m):
mae = mae + (y[i] - predictions[i]) ** 2
mae = mae / m
return mae
import linear_regression.linear_regression as LinearRegression
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(45)
number_of_instances = 100
X = np.random.randn(number_of_instances,1)
Y = 4 + 3 * X + np.random.randn(number_of_instances, 1)
plt.scatter(X, Y)
linear_regression = LinearRegression.LinearRegression()
regressor = linear_regression.fit(X, Y)
print(f"coaf: {linear_regression.coaf_}")
print(f"intercept: {linear_regression.intercept_}")
plt.plot(X, X @ regressor.coaf_ + regressor.intercept_, color='red')
print(f"predict: {linear_regression.predict([0])}")
print(f"score: {linear_regression.score([0], [4.07535703])}")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment