forked from Explore-AI/load-shortfall-regression-predict-api
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
155 lines (115 loc) · 4.95 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
Helper functions for the pretrained model to be used within our API.
Author: Explore Data Science Academy.
Note:
---------------------------------------------------------------------
Please follow the instructions provided within the README.md file
located within this directory for guidance on how to use this script
correctly.
Importantly, you will need to modify this file by adding
your own data preprocessing steps within the `_preprocess_data()`
function.
----------------------------------------------------------------------
Description: This file contains several functions used to abstract aspects
of model interaction within the API. This includes loading a model from
file, data preprocessing, and model prediction.
"""
# Helper Dependencies
import numpy as np
import pandas as pd
import pickle
import json
from sklearn.preprocessing import StandardScaler
def _preprocess_data(data):
"""Private helper function to preprocess data for model prediction.
NB: If you have utilised feature engineering/selection in order to create
your final model you will need to define the code here.
Parameters
----------
data : str
The data payload received within POST requests sent to our API.
Returns
-------
Pandas DataFrame : <class 'pandas.core.frame.DataFrame'>
The preprocessed data, ready to be used our model for prediction.
"""
# Convert the json string to a python dictionary object
feature_vector_dict = json.loads(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']]
def feature_engineering(data):
#drop all id cols
data = data.drop([col for col in data if 'id' in col], axis ='columns')
#drop the unnamed col in both
data = data.drop(['Unnamed: 0'],axis=1)
#drop cols with large missing values
data = data.drop(['Seville_pressure','Valencia_wind_deg'],axis='columns')
#code the time col
time = pd.to_datetime(data['time'])
data['time'] = time
data['Day'] = data['time'].dt.day
data['month'] = data['time'].dt.month
data['hour'] = data['time'].dt.hour
similar_list = ['temp','pressure','rain','wind','snow','cloud']
for se in similar_list:
temp_features = [col for col in data.columns.tolist() if se in col]
data['av_spain_'+se] = data[temp_features].mean(axis=1)
data = data.drop(temp_features, axis='columns')
#drop time col
data = data.drop(['time'],axis='columns')
#scale data
scaler = StandardScaler()
cols = data.columns.tolist()
#scale data
X_scaled = scaler.fit_transform(data)
X_standardise = pd.DataFrame(X_scaled,columns=cols)
#after normalizing, we can fill nan with zero
X_standardise = X_standardise.fillna(0)
return X_standardise
# ------------------------------------------------------------------------
return feature_engineering(feature_vector_df)
def load_model(path_to_model:str):
"""Adapter function to load our pretrained model into memory.
Parameters
----------
path_to_model : str
The relative path to the model weights/schema to load.
Note that unless another file format is used, this needs to be a
.pkl file.
Returns
-------
<class: sklearn.estimator>
The pretrained model loaded into memory.
"""
return pickle.load(open(path_to_model, 'rb'))
""" You may use this section (above the make_prediction function) of the python script to implement
any auxiliary functions required to process your model's artifacts.
"""
def make_prediction(data, model):
"""Prepare request data for model prediction.
Parameters
----------
data : str
The data payload received within POST requests sent to our API.
model : <class: sklearn.estimator>
An sklearn model object.
Returns
-------
list
A 1-D python list containing the model prediction.
"""
# Data preprocessing.
prep_data = _preprocess_data(data)
# Perform prediction with model and preprocessed data.
prediction = model.predict(prep_data)
# Format as list for output standardisation.
return prediction[0].tolist()