This commit is contained in:
lda committed 2025-05-13 01:59:42 +07:00
1 parent 2317d9a622
commit 9764ce28db
3 files changed
+34 -15

No files matched your search

+30 -12
View File
@@ -1,4 +1,5 @@
# import pandas as pd # import pandas as pd
import os
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
import numpy as np import numpy as np
from sklearn.model_selection import train_test_split # type: ignore[import] from sklearn.model_selection import train_test_split # type: ignore[import]
@@ -35,8 +36,11 @@ from visualize import (
) )
plt.ioff() plt.ioff()
data_file = "hanoi_weather_data.csv"
model_file = "theta.npy"
# Step 1: Load data # Step 1: Load data
data = load_weather_data("hanoi_weather_data.csv") data = load_weather_data(data_file)
plot_value_distribution(data) plot_value_distribution(data)
plot_time_series(data, feature_names=["QV2M"], title="QV2M Time Series") 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( # linear_lr = lambda epoch: linear_decay_schedule(
# epoch, initial_lr=0.01, min_lr=0.001, total_epochs=1000 # epoch, initial_lr=0.01, min_lr=0.001, total_epochs=1000
# ) # )
# cycle_lr = lambda epoch: cyclical_lr_schedule( cycle_lr = lambda epoch: cyclical_lr_schedule(
# epoch, base_lr=0.001, max_lr=0.01, step_size=200 epoch, base_lr=0.001, max_lr=0.01, step_size=200
# ) )
# poly_lr = lambda epoch: polynomial_decay_schedule( # poly_lr = lambda epoch: polynomial_decay_schedule(
# epoch, initial_lr=0.01, min_lr=0.001, total_epochs=1000, power=2 # epoch, initial_lr=0.01, min_lr=0.001, total_epochs=1000, power=2
# ) # )
# warmup_lr = lambda epoch: warmup_schedule( # warmup_lr = lambda epoch: warmup_schedule(
# epoch, warmup_epochs=10, initial_lr=0.001, max_lr=0.01 # 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 # Step 4: Train model with gradient descent
if not trained:
theta, losses = train_linear_regression_gd( theta, losses = train_linear_regression_gd(
X_train, X_train,
y_train, y_train,
lr_schedule=lambda epoch: cyclical_lr_schedule( lr_schedule=cycle_lr,
epoch, base_lr=0.0005, max_lr=0.01, step_size=150
),
n_epochs=1000, n_epochs=1000,
batch_size=32, batch_size=32,
patience=80, patience=50,
l2_reg=0.001, # Enable L2 regularization
l1_reg=0.0, # Disable L1 regularization
) )
# Save model
np.save(model_file, theta)
else:
# Load model
theta = np.load(model_file)
predictions = predict_linear_regression(X_test, theta) predictions = predict_linear_regression(X_test, theta)
# Step 5: Evaluate # Step 5: Evaluate
mse, r2 = evaluate_model(y_test, predictions) mse, r2, mae = evaluate_model(y_test, predictions)
print(f"MSE: {mse:.4f}, R2: {r2:.4f}") print(f"MSE: {mse:.4f}, R2: {r2:.4f}, MAE: {mae:.4f}")
# Step 6: Rescale predictions # Step 6: Rescale predictions
y_test_rescaled = inverse_scale(y_test, scaler, data.shape[1]) y_test_rescaled = inverse_scale(y_test, scaler, data.shape[1])
@@ -115,6 +132,7 @@ 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_residual_scatter(y_test, predictions) plot_residual_scatter(y_test, predictions)
# == inspect_residuals(predictions, residuals) # == inspect_residuals(predictions, residuals)
if losses:
plot_loss_curve_with_annotations(losses) plot_loss_curve_with_annotations(losses)
plot_feature_importance(theta, feature_names=[f"QV2M_t-{i}" for i in range(seq_length)]) plot_feature_importance(theta, feature_names=[f"QV2M_t-{i}" for i in range(seq_length)])
+4 -3
View File
@@ -3,7 +3,7 @@ import time
import numpy as np import numpy as np
from matplotlib import pyplot as plt 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) r2_score)
from preprocessing import inverse_scale from preprocessing import inverse_scale
@@ -83,7 +83,7 @@ def train_linear_regression_gd(
# Log statistics # Log statistics
epoch_time = time.time() - start_time 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( print(
f"Epoch {epoch}: Loss = {epoch_loss:.6f}, LR: {lr:.6f}, " f"Epoch {epoch}: Loss = {epoch_loss:.6f}, LR: {lr:.6f}, "
f"Gradient Norm = {gradient_norm:.4f}, Weight Norm = {weight_norm:.4f}, " 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.""" """Evaluate using sklearn's built-in MSE and R² functions."""
mse = mean_squared_error(y_true, y_pred) mse = mean_squared_error(y_true, y_pred)
r2 = r2_score(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 import math
BIN
View File
Binary file not shown.