Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Updated API version 1 #42

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
# Load our model into memory.
# Please update this path to reflect your own trained model.
static_model = load_model(
path_to_model='assets/trained-models/load_shortfall_simple_lm_regression.pkl')
# path_to_model='assets/trained-models/load_shortfall_simple_lm_regression.pkl')
path_to_model='assets/trained-models/mlr_model.pkl')

print ('-'*40)
print ('Model successfully loaded')
Expand Down
Binary file added assets/trained-models/mlr_model.pkl
Binary file not shown.
63 changes: 53 additions & 10 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import pandas as pd
import pickle
import json
from datetime import datetime, timedelta

def _preprocess_data(data):
"""Private helper function to preprocess data for model prediction.
Expand All @@ -49,16 +50,52 @@ def _preprocess_data(data):
# Load the dictionary as a Pandas DataFrame.
feature_vector_df = pd.DataFrame.from_dict([feature_vector_dict])

# ---------------------------------------------------------------
# NOTE: You will need to swap the lines below for your own data
# preprocessing methods.
#
# The code below is for demonstration purposes only. You will not
# receive marks for submitting this code in an unchanged state.
# ---------------------------------------------------------------

# ----------- Replace this code with your own preprocessing steps --------
predict_vector = feature_vector_df[['Madrid_wind_speed','Bilbao_rain_1h','Valencia_wind_speed']]
feature_vector_df['time'] = pd.to_datetime(feature_vector_df['time'])

# day
feature_vector_df['day'] = feature_vector_df['time'].dt.day
# month
feature_vector_df['month'] = feature_vector_df['time'].dt.month
# year
feature_vector_df['year'] = feature_vector_df['time'].dt.year
# hour
feature_vector_df['hour'] = feature_vector_df['time'].dt.hour
# minute
feature_vector_df['minute'] = feature_vector_df['time'].dt.minute
# second
feature_vector_df['second'] = feature_vector_df['time'].dt.second



feature_vector_df = feature_vector_df.loc[:, ['time','day', 'month', 'year', 'hour', 'minute',
'second', 'Madrid_wind_speed', 'Valencia_wind_deg', 'Bilbao_rain_1h',
'Valencia_wind_speed', 'Seville_humidity', 'Madrid_humidity',
'Bilbao_clouds_all', 'Bilbao_wind_speed', 'Seville_clouds_all',
'Bilbao_wind_deg', 'Barcelona_wind_speed', 'Barcelona_wind_deg',
'Madrid_clouds_all', 'Seville_wind_speed', 'Barcelona_rain_1h',
'Seville_pressure', 'Seville_rain_1h', 'Bilbao_snow_3h',
'Barcelona_pressure', 'Seville_rain_3h', 'Madrid_rain_1h',
'Barcelona_rain_3h', 'Valencia_snow_3h', 'Madrid_weather_id',
'Barcelona_weather_id', 'Bilbao_pressure', 'Seville_weather_id',
'Valencia_pressure', 'Seville_temp_max', 'Madrid_pressure',
'Valencia_temp_max', 'Valencia_temp', 'Bilbao_weather_id',
'Seville_temp', 'Valencia_humidity', 'Valencia_temp_min',
'Barcelona_temp_max', 'Madrid_temp_max', 'Barcelona_temp',
'Bilbao_temp_min', 'Bilbao_temp', 'Barcelona_temp_min',
'Bilbao_temp_max', 'Seville_temp_min', 'Madrid_temp', 'Madrid_temp_min']]
# # ,'load_shortfall_3h']]

feature_vector_df= feature_vector_df.drop(['time'], axis = 1)
feature_vector_df['Valencia_wind_deg'] = feature_vector_df['Valencia_wind_deg'].str.extract('(\d+)')
feature_vector_df['Valencia_wind_deg'] = pd.to_numeric(feature_vector_df['Valencia_wind_deg'])

feature_vector_df['Seville_pressure'] = feature_vector_df['Seville_pressure'].str.extract('(\d+)')
feature_vector_df['Seville_pressure'] = pd.to_numeric(feature_vector_df['Seville_pressure'])

feature_vector_df['Valencia_pressure'] = feature_vector_df['Valencia_pressure'].fillna(1015.0)
# # ----------- Replace this code with your own preprocessing steps --------
predict_vector = feature_vector_df.copy()
# predict_vector = feature_vector_df[['Madrid_wind_speed','Bilbao_rain_1h','Valencia_wind_speed']]
# ------------------------------------------------------------------------

return predict_vector
Expand Down Expand Up @@ -104,7 +141,13 @@ def make_prediction(data, model):
"""
# Data preprocessing.
prep_data = _preprocess_data(data)


# Perform prediction with model and preprocessed data.
prediction = model.predict(prep_data)

print(prediction)


# Format as list for output standardisation.
return prediction[0].tolist()
2 changes: 1 addition & 1 deletion utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

# url = 'http://{public-ip-address-of-remote-machine}:5000/api_v0.1'
url = 'http://127.0.0.1:5000/api_v0.1'

# url = 'http://54.229.191.145:5000/api_v0.1'
# Perform the POST request.
print(f"Sending POST request to web server API at: {url}")
print("")
Expand Down