Spaces:
Sleeping
Sleeping
| import numpy as np | |
| class LinearRegression: | |
| def __init__(self, learning_rate, num_iterations): | |
| self.learning_rate = learning_rate | |
| self.num_iterations = num_iterations | |
| self.weights = None | |
| self.bias = None | |
| def fit(self, X, y): | |
| num_samples, num_features = X.shape | |
| # Initialization | |
| self.weights = np.zeros(num_features) | |
| self.bias = 0 | |
| # Gradient Descent | |
| for _ in range(self.num_iterations): | |
| predictions = np.dot(X, self.weights) + self.bias | |
| dw = (1 / num_samples) * np.dot(X.T, (predictions - y)) | |
| db = (1 / num_samples) * np.sum(predictions - y) | |
| # Update | |
| self.weights = self.weights - (self.learning_rate * dw) | |
| self.bias = self.bias - (self.learning_rate * db) | |
| def predict(self, X): | |
| predictions = np.dot(X, self.weights) + self.bias | |
| return predictions | |