this
This commit is contained in:
1 parent
2317d9a622
commit
9764ce28db
3 files changed
+37
-18
No files matched your search
@@ -1,4 +1,5 @@
|
||||
# import pandas as pd
|
||||
import os
|
||||
from matplotlib import pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split # type: ignore[import]
|
||||
@@ -35,8 +36,11 @@ from visualize import (
|
||||
)
|
||||
|
||||
plt.ioff()
|
||||
data_file = "hanoi_weather_data.csv"
|
||||
model_file = "theta.npy"
|
||||
|
||||
# Step 1: Load data
|
||||
data = load_weather_data("hanoi_weather_data.csv")
|
||||
data = load_weather_data(data_file)
|
||||
|
||||
plot_value_distribution(data)
|
||||
plot_time_series(data, feature_names=["QV2M"], title="QV2M Time Series")
|
||||
@@ -69,35 +73,48 @@ date_train, date_test = train_test_split(date_index, test_size=test_size, shuffl
|
||||
# linear_lr = lambda epoch: linear_decay_schedule(
|
||||
# epoch, initial_lr=0.01, min_lr=0.001, total_epochs=1000
|
||||
# )
|
||||
# cycle_lr = lambda epoch: cyclical_lr_schedule(
|
||||
# epoch, base_lr=0.001, max_lr=0.01, step_size=200
|
||||
# )
|
||||
cycle_lr = lambda epoch: cyclical_lr_schedule(
|
||||
epoch, base_lr=0.001, max_lr=0.01, step_size=200
|
||||
)
|
||||
# poly_lr = lambda epoch: polynomial_decay_schedule(
|
||||
# epoch, initial_lr=0.01, min_lr=0.001, total_epochs=1000, power=2
|
||||
# )
|
||||
# warmup_lr = lambda epoch: warmup_schedule(
|
||||
# epoch, warmup_epochs=10, initial_lr=0.001, max_lr=0.01
|
||||
# )
|
||||
trained = None
|
||||
if os.path.isfile(model_file):
|
||||
if input("found model, train anyway? ").strip().lower() in "y":
|
||||
print("found model but training")
|
||||
trained = False
|
||||
else:
|
||||
print("found model")
|
||||
trained = True
|
||||
else:
|
||||
print("model not found, training")
|
||||
|
||||
# Step 4: Train model with gradient descent
|
||||
theta, losses = train_linear_regression_gd(
|
||||
if not trained:
|
||||
theta, losses = train_linear_regression_gd(
|
||||
X_train,
|
||||
y_train,
|
||||
lr_schedule=lambda epoch: cyclical_lr_schedule(
|
||||
epoch, base_lr=0.0005, max_lr=0.01, step_size=150
|
||||
),
|
||||
lr_schedule=cycle_lr,
|
||||
n_epochs=1000,
|
||||
batch_size=32,
|
||||
patience=80,
|
||||
l2_reg=0.001, # Enable L2 regularization
|
||||
l1_reg=0.0, # Disable L1 regularization
|
||||
)
|
||||
patience=50,
|
||||
)
|
||||
|
||||
# Save model
|
||||
np.save(model_file, theta)
|
||||
else:
|
||||
# Load model
|
||||
theta = np.load(model_file)
|
||||
|
||||
predictions = predict_linear_regression(X_test, theta)
|
||||
|
||||
# Step 5: Evaluate
|
||||
mse, r2 = evaluate_model(y_test, predictions)
|
||||
print(f"MSE: {mse:.4f}, R2: {r2:.4f}")
|
||||
mse, r2, mae = evaluate_model(y_test, predictions)
|
||||
print(f"MSE: {mse:.4f}, R2: {r2:.4f}, MAE: {mae:.4f}")
|
||||
|
||||
# Step 6: Rescale predictions
|
||||
y_test_rescaled = inverse_scale(y_test, scaler, data.shape[1])
|
||||
@@ -115,7 +132,8 @@ plot_error_hist(errors)
|
||||
plot_scatter(y_test.reshape(-1, 1), predictions.reshape(-1, 1))
|
||||
plot_residual_scatter(y_test, predictions)
|
||||
# == inspect_residuals(predictions, residuals)
|
||||
plot_loss_curve_with_annotations(losses)
|
||||
if losses:
|
||||
plot_loss_curve_with_annotations(losses)
|
||||
plot_feature_importance(theta, feature_names=[f"QV2M_t-{i}" for i in range(seq_length)])
|
||||
|
||||
|
||||
|
||||
+4
-3
@@ -3,7 +3,7 @@ import time
|
||||
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
from sklearn.metrics import (mean_squared_error, # type: ignore[import]
|
||||
from sklearn.metrics import (mean_absolute_error, mean_squared_error, # type: ignore[import]
|
||||
r2_score)
|
||||
|
||||
from preprocessing import inverse_scale
|
||||
@@ -83,7 +83,7 @@ def train_linear_regression_gd(
|
||||
|
||||
# Log statistics
|
||||
epoch_time = time.time() - start_time
|
||||
if epoch % 100 == 0 or epoch == n_epochs - 1:
|
||||
if epoch % 50 == 0 or epoch == n_epochs - 1:
|
||||
print(
|
||||
f"Epoch {epoch}: Loss = {epoch_loss:.6f}, LR: {lr:.6f}, "
|
||||
f"Gradient Norm = {gradient_norm:.4f}, Weight Norm = {weight_norm:.4f}, "
|
||||
@@ -104,7 +104,8 @@ def evaluate_model(y_true, y_pred):
|
||||
"""Evaluate using sklearn's built-in MSE and R² functions."""
|
||||
mse = mean_squared_error(y_true, y_pred)
|
||||
r2 = r2_score(y_true, y_pred)
|
||||
return mse, r2
|
||||
mae = mean_absolute_error(y_true, y_pred)
|
||||
return iter((mse, r2, mae))
|
||||
|
||||
|
||||
import math
|
||||
|
||||
Reference in new issue
Block a user