10 Commits
12 changed files with 18593 additions and 15647 deletions

No files matched your search

+2 -1
View File
@@ -184,4 +184,5 @@ ipython_config.py
.conda/
.conda*/
dead/
+6
View File
@@ -0,0 +1,6 @@
{
"recommendations": [
"ms-python.python",
"ms-toolsai.jupyter"
]
}
+4 -2
View File
@@ -14,8 +14,10 @@ how to git? do not `push --force`. prefer pull requests. its like github
we will plan to do this in multiple different ways using scikit-learn and pytorch. tensorflow is ancient.
> [!IMPORTANT]
> phase out tensorflow. dont use it.
> [!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
File diff suppressed because it is too large. Load diff
-14429
View File
File diff suppressed because it is too large. Load diff
+19 -3
View File
@@ -1,10 +1,11 @@
import urllib.parse
import urllib.request
from datetime import datetime
from global_config import DATA_FILE_NAME
# Define parameters
latitude, longitude = 21.04815477090267, 105.80123159165096 # lmao
start_date = "2010-01-01"
start_date = "1981-01-01"
end_date = datetime.now().strftime("%Y-%m-%d") # Today's date
# Define desired parameters
@@ -16,9 +17,24 @@ end_date = datetime.now().strftime("%Y-%m-%d") # Today's date
# WS10M MERRA-2 Wind Speed at 10 Meters (m/s)
# WD10M MERRA-2 Wind Direction at 10 Meters (Degrees)
# more at https://power.larc.nasa.gov/data-access-viewer/
weather_params = [
# "T2M",
# "T2M_MAX",
# "T2M_MIN",
# "T2MDEW",
# "PRECTOTCORR",
"QV2M",
# "RH2M",
# "PS",
# "CLOUD_AMT",
# "WS10M",
# "WD10M",
# "ALLSKY_KT",
# "ALLSKY_SFC_SW_DWN",
]
params = {
"parameters": "QV2M,T2M,PRECTOTCORR",
"parameters": ",".join(weather_params),
"community": "RE",
"longitude": longitude,
"latitude": latitude,
@@ -40,7 +56,7 @@ try:
with urllib.request.urlopen(url) as response:
if response.status == 200:
csv_data = response.read().decode("utf-8")
with open("hanoi_weather_data.csv", "w", encoding="utf-8") as file:
with open(DATA_FILE_NAME, "w", encoding="utf-8") as file:
file.write(csv_data)
print("Data successfully downloaded and saved to 'hanoi_weather_data.csv'.")
else:
+2100
View File
File diff suppressed because it is too large. Load diff
+163
View File
@@ -0,0 +1,163 @@
from sklearn.model_selection import train_test_split
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
from sklearn.preprocessing import MinMaxScaler
import numpy as np
from load_data import load_this_data
a = load_this_data()
# time series forecasting
scaler = MinMaxScaler()
print(a.columns)
scaled_data = scaler.fit_transform(a)
def create_sequences(data, seq_length):
"see this? return[0] is the big x and return[1] is the big y. and h(x) = y. and h(x) is the model."
xs = []
ys = []
for i in range(len(data) - seq_length):
x = data[i : (i + seq_length)]
y = data[i + seq_length]
xs.append(x)
ys.append(y)
return np.array(xs), np.array(ys)
seq_length = 20
X, y = create_sequences(scaled_data, seq_length)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
from torch.utils.data import TensorDataset, DataLoader
X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
y_train_tensor = torch.tensor(y_train, dtype=torch.float32)
train_dataset = TensorDataset(X_train_tensor, y_train_tensor)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
import torch.nn as nn
class LSTMModel(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size):
super(LSTMModel, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
out, _ = self.lstm(x, (h0, c0))
out = self.fc(out[:, -1, :]) # Take the output from the last time step
return out
model = LSTMModel(
input_size=X_train.shape[2],
hidden_size=64,
num_layers=2,
output_size=y_train.shape[1],
)
model.to(device)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
train_losses = []
val_losses = []
for epoch in range(100):
model.train()
running_train_loss = 0.0
# inputs, targets
for batch_X, batch_y in train_loader:
batch_X = batch_X.to(device)
batch_y = batch_y.to(device)
outputs = model(batch_X)
loss = criterion(outputs, batch_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_train_loss += loss.item() * batch_X.size(0)
if (epoch + 1) % 10 == 0:
print(f"Epoch [{epoch+1}/100], Loss: {loss.item():.4f}")
epoch_train_loss = running_train_loss / len(train_loader.dataset)
train_losses.append(epoch_train_loss)
model.eval()
X_test_tensor = torch.tensor(X_test, dtype=torch.float32).to(device)
y_test_tensor = torch.tensor(y_test, dtype=torch.float32).to(device)
val_dataset = TensorDataset(X_test_tensor, y_test_tensor)
val_loader = DataLoader(val_dataset, batch_size=64, shuffle=False)
# with torch.no_grad():
# running_val_loss = 0.0
# predictions = model(X_test_tensor)
# test_loss = criterion(predictions, y_test_tensor)
# print(f"Test Loss: {test_loss.item():.4f}")
running_val_loss = 0.0
predictions = []
with torch.no_grad():
for inputs, targets in val_loader:
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
loss = criterion(outputs, targets)
running_val_loss += loss.item() * inputs.size(0)
predictions.append(outputs.cpu().numpy()) # Collect all batches
epoch_val_loss = running_val_loss / len(val_loader.dataset) # len(val_dataset)
val_losses.append(epoch_val_loss)
predictions = np.concatenate(predictions, axis=0)
import matplotlib.pyplot as plt
y_test_cpu = y_test_tensor.cpu().numpy()
predictions_rescaled = scaler.inverse_transform(predictions)
y_test_rescaled = scaler.inverse_transform(y_test_cpu)
fig, (ax1, ax2) = plt.subplots(
1, 2, figsize=(14, 5), gridspec_kw={"width_ratios": [1, 3]}
)
# Loss curve on the left
ax1.plot(train_losses, label="Train Loss")
ax1.plot(val_losses, label="Val Loss")
ax1.set_title("Loss Curve")
ax1.set_xlabel("Epoch")
ax1.set_ylabel("Loss")
ax1.legend()
ax1.grid(True)
# Prediction vs Actual on the right
ax2.plot(y_test_rescaled, label="Actual")
ax2.plot(predictions_rescaled, label="Predicted")
ax2.set_title("Actual vs. Predicted")
ax2.set_xlabel("Sample")
ax2.set_ylabel("Value")
ax2.legend()
plt.tight_layout()
plt.show()
+1
View File
@@ -0,0 +1 @@
DATA_FILE_NAME = "data/hanoi_weather_data.csv"
-51
View File
@@ -5,9 +5,6 @@ import io
# from itertools import islice
from typing import Union
import pandas as pd
def cut_headers(stream: Union[io.StringIO, io.FileIO, io.TextIOWrapper]):
headers = []
exist_startheader = False
@@ -29,51 +26,3 @@ def cut_headers(stream: Union[io.StringIO, io.FileIO, io.TextIOWrapper]):
stream.seek(0)
return "", stream
return "\n".join(headers), stream
# if __name__ == "__main__":
# with open("hanoi_qv2m_latest.csv", "r", encoding="utf-8") as file:
# header, file = cut_headers(file)
# print("Extracted Header:")
# print(header)
# data = pd.read_csv(file)
# print("\nFirst 5 Lines of Data:")
# print(data.head())
# print(data.isnull().sum())
def load_data(
weather_file_name, columns=["Year", "Month", "Day", "Humidity"].copy(), na=-999
):
with open(weather_file_name, "r", encoding="utf-8") as file:
_header, file = cut_headers(file)
data = pd.read_csv(file, na_values=na)
# print(header)
data.columns = columns
return data
def date_combine(data: pd.DataFrame):
data["Date"] = pd.to_datetime(data[["Year", "Month", "Day"]])
data = data.drop(columns=["Year", "Month", "Day"])
data = data.set_index("Date")
return data
def validate_view(data: pd.DataFrame):
print(f"Shape with N/A values: {data.shape}")
isn = data.isnull().sum().iloc[0]
print(f"Count of N/A values: {isn}")
data = data.dropna() # Drop the rows with missing values
isn = data.isnull().sum().iloc[0]
print(f"Count after drop: {isn}")
print(data.describe())
return data
# if __name__ == "__main__":
def load_this_data(filename):
d = load_data(filename)
d = date_combine(d)
d = validate_view(d)
return d
-1161
View File
File diff suppressed because it is too large. Load diff
+85
View File
@@ -0,0 +1,85 @@
import sys
import numpy as np
import pandas as pd
from sklearn.metrics import r2_score
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from global_config import DATA_FILE_NAME
from load_data import cut_headers
# 1. Load data
with open(DATA_FILE_NAME, "r", encoding="utf-8") as file:
data_header, csv_file = cut_headers(file)
data = pd.read_csv(csv_file, na_values=-999)
if all(z in data.columns for z in ("YEAR", "MO", "DY")):
data["date"] = pd.to_datetime(
{k: data[v] for v, k in (zip(["YEAR", "MO", "DY"], ["year", "month", "day"]))}
)
data.drop(inplace=True, columns=["YEAR", "MO", "DY"])
data.set_index(["date"], inplace=True)
data.dropna(inplace=True)
# 2. Normalize data
scaler = MinMaxScaler()
print(data.columns)
scaled_data = scaler.fit_transform(data)
# 3. Create sequences for supervised learning
def create_sequences(data, seq_length):
xs, ys = [], []
for i in range(len(data) - seq_length):
x = data[i : i + seq_length].flatten()
y = data[i + seq_length]
xs.append(x)
ys.append(y)
return np.array(xs), np.array(ys)
seq_length = 50
X, y = create_sequences(scaled_data, seq_length)
# 4. Split into training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.02, shuffle=False)
# 5. Train linear regression model
model = RandomForestRegressor(
n_estimators=200, # Number of trees
# max_features='sqrt', # Features considered at each split
# max_depth=30, # Maximum depth of each tree
# n_jobs=-1, # Use all CPU cores
random_state=42 # Reproducibility
)
model.fit(X_train, y_train)
# 6. Make predictions
predictions = model.predict(X_test)
# 7. Evaluate performance
mse = mean_squared_error(y_test, predictions)
r2 = r2_score(y_test, predictions)
print(f"Test MSE: {mse:.4f}")
print(f"Test r2: {r2:.4f}")
print(f"Score: {model.score(X_test, y_test)}")
# 8. Inverse transform predictions
predictions_rescaled = scaler.inverse_transform(predictions.reshape(-1, data.shape[1]))
y_test_rescaled = scaler.inverse_transform(y_test.reshape(-1, data.shape[1]))
# 9. Plot results
for i, column in enumerate(data.columns):
plt.figure(figsize=(10, 5))
plt.plot(y_test_rescaled[:, i], label=f"Actual {column}")
plt.plot(predictions_rescaled[:, i], label=f"Predicted {column}")
plt.title(f"Actual vs. Predicted for {column}")
plt.xlabel("Sample")
plt.ylabel("Value")
plt.legend()
plt.tight_layout()
plt.show()