Linear Regression via Gradient Descent
Linear regression is a foundational supervised learning algorithm. Given a dataset of input features and corresponding target values , the goal is to find a linear relationship that best fits the data. The "best fit" is typically defined by minimizing the Mean Squared Error (MSE) loss function.
Your task is to implement linear regression using batch gradient descent to find the optimal weights and bias .
The MSE loss function for samples is:
You need to derive the gradients of with respect to and :
And then update and iteratively:
where is the learning rate.
Implementation Details:
Implement a Python class MyLinearRegression with the following methods:
* __init__(self, learning_rate=0.01, n_iterations=1000): Initializes learning rate and number of iterations.
* fit(self, X, y): Trains the model using gradient descent.
* Initialize and randomly or to zeros.
* Perform n_iterations of gradient descent. In each iteration:
* Calculate predictions .
* Calculate the loss.
* Calculate gradients and .
* Update and .
* Store the history of loss values.
* predict(self, X): Makes predictions on new data X.
Verification:
1. Generate a synthetic dataset: X = 2 * np.random.rand(100, 1), y = 4 + 3 * X + np.random.randn(100, 1).
2. Train your MyLinearRegression model on this dataset.
3. Plot the loss history to ensure it converges.
4. Compare your learned and values with the true values (3 and 4 in the example) and with the results from sklearn.linear_model.LinearRegression. The parameters should be very close.
5. Plot the regression line learned by your model against the data points.