good
This commit is contained in:
1 parent
5664329cae
commit
42a0e2bcfd
3 files changed
+12
-6
No files matched your search
@@ -31,7 +31,7 @@ X_train, X_test, y_train, y_test = train_test_split(
|
|||||||
date_train, date_test = train_test_split(date_index, test_size=0.02, shuffle=False)
|
date_train, date_test = train_test_split(date_index, test_size=0.02, shuffle=False)
|
||||||
|
|
||||||
# Step 4: Train model with gradient descent
|
# Step 4: Train model with gradient descent
|
||||||
theta, losses = train_linear_regression_gd(X_train, y_train, lr=0.005, n_epochs=300)
|
theta, losses = train_linear_regression_gd(X_train, y_train, lr=0.005, n_epochs=10000)
|
||||||
predictions = predict_linear_regression(X_test, theta)
|
predictions = predict_linear_regression(X_test, theta)
|
||||||
|
|
||||||
# Step 5: Evaluate
|
# Step 5: Evaluate
|
||||||
@@ -47,7 +47,12 @@ residuals = y_test - predictions
|
|||||||
errors = abs(residuals)
|
errors = abs(residuals)
|
||||||
|
|
||||||
plot_predictions(date_test, y_test_rescaled, predictions_rescaled)
|
plot_predictions(date_test, y_test_rescaled, predictions_rescaled)
|
||||||
plot_residuals(date_test, residuals)
|
print("date_test shape:", date_test.shape)
|
||||||
|
print("residuals shape:", residuals.shape)
|
||||||
|
print(date_test[:5])
|
||||||
|
print(date_test[-5:])
|
||||||
|
|
||||||
|
plot_residuals(date_test.to_numpy(), residuals)
|
||||||
plot_error_hist(errors)
|
plot_error_hist(errors)
|
||||||
plot_scatter(y_test.reshape(-1, 1), predictions.reshape(-1, 1))
|
plot_scatter(y_test.reshape(-1, 1), predictions.reshape(-1, 1))
|
||||||
plot_loss_curve(losses)
|
plot_loss_curve(losses)
|
||||||
+4
-4
@@ -1,7 +1,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
def train_linear_regression_gd(X_train, y_train, lr=0.005, n_epochs=300):
|
def train_linear_regression_gd(X_train, y_train, lr=0.01, n_epochs=1000):
|
||||||
"""
|
"""
|
||||||
Train linear regression model using batch gradient descent.
|
Train linear regression model using batch gradient descent.
|
||||||
|
|
||||||
@@ -18,15 +18,15 @@ def train_linear_regression_gd(X_train, y_train, lr=0.005, n_epochs=300):
|
|||||||
predictions = X_b @ theta
|
predictions = X_b @ theta
|
||||||
errors = predictions - y_train
|
errors = predictions - y_train
|
||||||
gradients = (2 / m) * X_b.T @ errors
|
gradients = (2 / m) * X_b.T @ errors
|
||||||
gradients = np.clip(gradients, -1, 1) # gradient clipping to prevent divergence
|
gradients = np.clip(gradients, -1, 1) # gradient clipping
|
||||||
theta -= lr * gradients
|
theta -= lr * gradients
|
||||||
loss = np.mean(errors ** 2)
|
loss = np.mean(errors ** 2)
|
||||||
losses.append(loss)
|
losses.append(loss)
|
||||||
|
|
||||||
# Debug print every 10 epochs
|
if epoch % 500 == 0:
|
||||||
if epoch % 10 == 0:
|
|
||||||
print(f"Epoch {epoch}: Loss = {loss:.6f}")
|
print(f"Epoch {epoch}: Loss = {loss:.6f}")
|
||||||
|
|
||||||
|
print(f"Final Loss: {losses[-1]:.6f}")
|
||||||
return theta, losses
|
return theta, losses
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ def plot_residuals(date_test, residuals):
|
|||||||
plt.figure(figsize=(10, 4))
|
plt.figure(figsize=(10, 4))
|
||||||
plt.plot(date_test, residuals, label="Residuals")
|
plt.plot(date_test, residuals, label="Residuals")
|
||||||
plt.hlines(0, 0, len(residuals), colors="red")
|
plt.hlines(0, 0, len(residuals), colors="red")
|
||||||
|
plt.xlim(date_test.min(), date_test.max())
|
||||||
plt.title("Residuals Over Time")
|
plt.title("Residuals Over Time")
|
||||||
plt.xlabel("Date")
|
plt.xlabel("Date")
|
||||||
plt.ylabel("Error")
|
plt.ylabel("Error")
|
||||||
|
|||||||
Reference in new issue
Block a user