This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathmodel.py
107 lines (83 loc) · 3.37 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
"""
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 --------
predict_vector = feature_vector_df[['Pickup Lat','Pickup Long',
'Destination Lat','Destination Long']]
# ------------------------------------------------------------------------
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()