-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
122 lines (90 loc) · 4.1 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
"""
Helper functions for the pretrained model to be used within our API.
Author: Explore Data Science Academy.
Note:
---------------------------------------------------------------------
Plase 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
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 --------
feature_vector_df = feature_vector_df[(feature_vector_df['Commodities'] == 'APPLE GOLDEN DELICIOUS')].drop(columns='Commodities')
feature_vector_df['Date'] = pd.to_datetime(feature_vector_df['Date'])
feature_vector_df['Day'] = feature_vector_df['Date'].dt.day
feature_vector_df['Month'] = feature_vector_df['Date'].dt.month
feature_vector_df.drop(['Date'], inplace = True, axis = 1)
feature_vector_df.columns = ['province', 'container', 'size_grade', 'weight_kg', 'low_price',
'high_price', 'sales_total', 'total_qty_sold','total_kg_sold',
'stock_on_hand', 'avg_price_per_kg', 'day', 'month']
predict_vector = pd.get_dummies(feature_vector_df,drop_first=False)
# ------------------------------------------------------------------------
return predict_vector
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'))
def make_prediction(data, model):
"""Prepare request data for model prediciton.
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 standerdisation.
return prediction[0].tolist()