Skip to content

Commit

Permalink
Add scaler to transform
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-perseus committed Dec 2, 2023
1 parent 955121d commit 21ba1c9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
app = Flask(__name__)
CORS(app)

single_prediction = SinglePrediction("deploy/model_scripted.pt", "data/preprocessing/raw_features_2024.csv")
single_prediction = SinglePrediction("deploy/model_scripted.pt", "deploy/scaler.pkl", "data/preprocessing/raw_features_2024.csv")


@app.route('/')
Expand Down
3 changes: 3 additions & 0 deletions data/preprocessing/preprocess_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def get_features_for_model(self):
self.append_time_features()
# self.get_lagged_features()

pd.set_option('display.max_columns', None)
print(self.df.head())

return self.df[feature_columns], len(feature_columns)

def append_time_features(self):
Expand Down
2 changes: 0 additions & 2 deletions data/preprocessing/single_prediction_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def build_dataframe(self, input_date):

# parking_df = self.get_parking_data_df()

print(df.head())

return PreprocessFeatures(df).get_features_for_model()


Expand Down
17 changes: 12 additions & 5 deletions deploy/single_prediction.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import torch
import pandas as pd
from torch.utils.data import TensorDataset, DataLoader
from model.scaler import Scaler
from data.preprocessing.single_prediction_features import SinglePredictionFeatures
from data.metadata.metadata import parking_data_labels

batch_size = 1 # Required for model input


class SinglePrediction:
def __init__(self, model_path, raw_features_path):
def __init__(self, model_path, scaler_path, raw_features_path):
self.scaler = Scaler.load(scaler_path)
self.single_prediction_features = SinglePredictionFeatures(raw_features_path)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = torch.jit.load(model_path, map_location=self.device)
Expand All @@ -23,17 +26,21 @@ def build_dataset(self, df):
def predict_with_model(self, dataloader, features_length):
data = next(iter(dataloader))[0]
data = data.view([batch_size, -1, features_length]).to(self.device)
return self.model(data).cpu()
return self.model(data).detach().cpu().numpy()

def predict_for_date(self, date):
features_df, features_length = self.single_prediction_features.build_dataframe(date)
dataloader = self.build_dataset(features_df)

output = self.predict_with_model(dataloader, features_length)

return [dict(zip(parking_data_labels, row)) for row in output.tolist()]
output_scaled_back = self.scaler.inverse_transform(pd.DataFrame(output, columns=parking_data_labels))

return [dict(zip(parking_data_labels, row)) for row in output_scaled_back.tolist()]


if __name__ == "__main__":
predict = SinglePrediction("model_scripted.pt", "../data/preprocessing/raw_features_2024.csv")
print(predict.predict_for_date("2023-10-09 00:00"))
predict = SinglePrediction("model_scripted.pt", "scaler.pkl", "../data/preprocessing/raw_features_2024.csv")
print(predict.predict_for_date("2023-12-08 08:00"))
print(predict.predict_for_date("2023-12-10 18:00"))
print(predict.predict_for_date("2023-12-12 12:00"))
Empty file added model/__init__.py
Empty file.

0 comments on commit 21ba1c9

Please sign in to comment.