this model is so fucking ass
This commit is contained in:
1 parent
58205ca3c2
commit
770c1ff1b0
5 files changed
+94
-28
No files matched your search
@@ -1,28 +1,5 @@
|
|||||||
# midterm ml&dm1
|
# midterm ml&dm1
|
||||||
|
|
||||||
## not cool
|
## real
|
||||||
|
|
||||||
currently this shit is a blatant copy. changing soon.
|
not real
|
||||||
|
|
||||||
## how to
|
|
||||||
|
|
||||||
load this thing on google colab dont run on device its so cooked
|
|
||||||
|
|
||||||
how to git? do not `push --force`. prefer pull requests. its like github
|
|
||||||
|
|
||||||
## roadmap
|
|
||||||
|
|
||||||
we will plan to do this in multiple different ways using scikit-learn and pytorch. tensorflow is ancient.
|
|
||||||
|
|
||||||
> [!NOTE]
|
|
||||||
> ~phase out tensorflow. dont use it.~
|
|
||||||
>
|
|
||||||
> its 2 different platforms for 2 different types of people. use whatever ig
|
|
||||||
|
|
||||||
maybe also fit more weather related stats? this dataset is taken from nasa so sources are there
|
|
||||||
|
|
||||||
## collab
|
|
||||||
|
|
||||||
seng tells me this project can be planned without code. basically someone find da waes and other people implements them
|
|
||||||
|
|
||||||
thing is you click [here at scikit learn](https://scikit-learn.org/stable/supervised_learning.html) and tell me how many ways there are you can use to solve a problem. holy fucking shit. too many.
|
|
||||||
+4
-3
@@ -32,9 +32,10 @@ def load_weather_data(file_path: str) -> pd.DataFrame:
|
|||||||
|
|
||||||
# Convert YEAR, MO, DY to datetime index
|
# Convert YEAR, MO, DY to datetime index
|
||||||
if all(col in data.columns for col in ("YEAR", "MO", "DY")):
|
if all(col in data.columns for col in ("YEAR", "MO", "DY")):
|
||||||
data["date"] = pd.to_datetime(
|
dt_conv_table = {
|
||||||
{k: data[v] for v, k in zip(["YEAR", "MO", "DY"], ["year", "month", "day"])}
|
k: data[v] for v, k in zip(["YEAR", "MO", "DY"], ["year", "month", "day"])
|
||||||
)
|
}
|
||||||
|
data["date"] = pd.to_datetime(dt_conv_table)
|
||||||
data.drop(columns=["YEAR", "MO", "DY"], inplace=True)
|
data.drop(columns=["YEAR", "MO", "DY"], inplace=True)
|
||||||
data.set_index("date", inplace=True)
|
data.set_index("date", inplace=True)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
# import pandas as pd
|
# import pandas as pd
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
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]
|
||||||
|
|
||||||
from data_loader import load_weather_data
|
from data_loader import load_weather_data
|
||||||
@@ -6,6 +8,7 @@ from model_train import (
|
|||||||
cosine_lr_schedule,
|
cosine_lr_schedule,
|
||||||
evaluate_model,
|
evaluate_model,
|
||||||
predict_linear_regression,
|
predict_linear_regression,
|
||||||
|
run_autoregressive_prediction,
|
||||||
train_linear_regression_gd,
|
train_linear_regression_gd,
|
||||||
)
|
)
|
||||||
from preprocessing import create_sequences, inverse_scale, scale_data
|
from preprocessing import create_sequences, inverse_scale, scale_data
|
||||||
@@ -80,3 +83,9 @@ plot_scatter(y_test.reshape(-1, 1), predictions.reshape(-1, 1))
|
|||||||
plot_residual_scatter(y_test, predictions)
|
plot_residual_scatter(y_test, predictions)
|
||||||
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)])
|
||||||
|
|
||||||
|
|
||||||
|
# Step 8: Autoregressive Prediction
|
||||||
|
autoregressive_predictions = run_autoregressive_prediction(
|
||||||
|
theta, X_test, y_test, scaler, date_test, data.shape[1]
|
||||||
|
)
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
# from itertools import islice
|
# from itertools import islice
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from sklearn.metrics import mean_squared_error, r2_score # type: ignore[import]
|
from sklearn.metrics import mean_squared_error, r2_score # type: ignore[import]
|
||||||
|
|
||||||
|
from preprocessing import inverse_scale
|
||||||
|
|
||||||
|
|
||||||
def train_linear_regression_gd(
|
def train_linear_regression_gd(
|
||||||
X_train,
|
X_train,
|
||||||
@@ -94,3 +97,78 @@ def cosine_lr_schedule(epoch, max_lr=0.01, min_lr=0.001, total_epochs=1000):
|
|||||||
return (
|
return (
|
||||||
min_lr + (max_lr - min_lr) * (1 + math.cos(math.pi * epoch / total_epochs)) / 2
|
min_lr + (max_lr - min_lr) * (1 + math.cos(math.pi * epoch / total_epochs)) / 2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def autoregressive_prediction(model, initial_sequence, steps, scaler, n_features):
|
||||||
|
"""
|
||||||
|
Perform autoregressive predictions using the model.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
model: The trained model (theta for linear regression).
|
||||||
|
initial_sequence: The initial sequence to start predictions (from test data).
|
||||||
|
steps: Number of steps to predict.
|
||||||
|
scaler: Scaler used for inverse scaling.
|
||||||
|
n_features: Number of features in the data.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
predictions: List of predicted values.
|
||||||
|
"""
|
||||||
|
sequence = initial_sequence.copy() # Start with the initial sequence
|
||||||
|
predictions = []
|
||||||
|
|
||||||
|
for _ in range(steps):
|
||||||
|
# Predict the next value
|
||||||
|
next_prediction = predict_linear_regression(sequence.reshape(1, -1), model)
|
||||||
|
predictions.append(next_prediction[0])
|
||||||
|
|
||||||
|
# Update the sequence: Remove the oldest value and append the new prediction
|
||||||
|
sequence = np.roll(sequence, -1) # Shift sequence left
|
||||||
|
sequence[-1] = next_prediction # Add the new prediction
|
||||||
|
|
||||||
|
# Rescale predictions back to original scale
|
||||||
|
predictions_rescaled = inverse_scale(np.array(predictions), scaler, n_features)
|
||||||
|
return predictions_rescaled
|
||||||
|
|
||||||
|
|
||||||
|
def run_autoregressive_prediction(
|
||||||
|
model, X_test, y_test, scaler, date_test, n_features, steps=None
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Run autoregressive prediction and visualize the results.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
model: The trained model (theta for linear regression).
|
||||||
|
X_test: Test features.
|
||||||
|
y_test: Test labels.
|
||||||
|
scaler: Scaler used for inverse scaling.
|
||||||
|
date_test: Dates corresponding to the test set.
|
||||||
|
n_features: Number of features in the data.
|
||||||
|
steps: Number of steps to predict (default is the length of y_test).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
autoregressive_predictions: Rescaled autoregressive predictions.
|
||||||
|
"""
|
||||||
|
if steps is None:
|
||||||
|
steps = len(y_test) # Default to the length of the test set
|
||||||
|
|
||||||
|
# Use the first test sequence as the initial sequence
|
||||||
|
initial_sequence = X_test[0]
|
||||||
|
|
||||||
|
# Perform autoregressive prediction
|
||||||
|
autoregressive_predictions = autoregressive_prediction(
|
||||||
|
model, initial_sequence, steps, scaler, n_features
|
||||||
|
)
|
||||||
|
|
||||||
|
# Visualize the results
|
||||||
|
plt.figure(figsize=(12, 6))
|
||||||
|
plt.plot(date_test, inverse_scale(y_test, scaler, n_features), label="Actual")
|
||||||
|
plt.plot(date_test, autoregressive_predictions, label="Autoregressive Predictions")
|
||||||
|
plt.title("Autoregressive Predictions vs Actual")
|
||||||
|
plt.xlabel("Date")
|
||||||
|
plt.ylabel("Value")
|
||||||
|
plt.legend()
|
||||||
|
plt.grid(True)
|
||||||
|
plt.tight_layout()
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
return autoregressive_predictions
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from sklearn.preprocessing import MinMaxScaler # type: ignore[import]
|
from sklearn.preprocessing import MinMaxScaler # type: ignore[import]
|
||||||
|
|
||||||
# from sklearn.model_selection import train_test_split
|
# from sklearn.model_selection import train_test_split
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user