├── .gitignore ├── README.md ├── api.py ├── assets ├── imgs │ ├── API.png │ ├── EDSA_logo.png │ ├── Flask_logo.png │ ├── What_is_an_API.png │ └── fork_repo.png └── trained-models │ └── load_shortfall_simple_lm_regression.pkl ├── model.py └── utils ├── data ├── df_test.csv └── df_train.csv ├── request.py └── train_model.py /.gitignore: -------------------------------------------------------------------------------- 1 | ### Python ### 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flask-based Model API 2 | #### EXPLORE Data Science Academy Regression Predict 3 | 4 | ### Table of Contents 5 | 6 | - [Flask-based Model API](#flask-based-model-api) 7 | - [EXPLORE Data Science Academy Regression Predict](#explore-data-science-academy-regression-predict) 8 | - [Table of Contents](#table-of-contents) 9 | - [1) Overview](#1-overview) 10 | - [1.1) Wait, what is an API again?](#11-wait-what-is-an-api-again) 11 | - [1.2) How our API will work](#12-how-our-api-will-work) 12 | - [Description of files](#description-of-files) 13 | - [2) Usage Instructions](#2-usage-instructions) 14 | - [2.1) Creating a copy of this repo](#21-creating-a-copy-of-this-repo) 15 | - [2.2) Running the API on your local machine](#22-running-the-api-on-your-local-machine) 16 | - [2.3) Updating the API to use your own model](#23-updating-the-api-to-use-your-own-model) 17 | - [Prerequisites](#prerequisites) 18 | - [Making the changes](#making-the-changes) 19 | - [2.4) Running the API on a remote AWS EC2 instance](#24-running-the-api-on-a-remote-aws-ec2-instance) 20 | - [3) FAQ](#3-faq) 21 | 22 | ## 1) Overview 23 | 24 | This repository forms the basis of *Task 2* for the **Regression Predict** within EDSA's Data Science course. It hosts template code which will enable students to deploy their own developed models through a web server-based API. 25 | 26 | #### 1.1) Wait, what is an API again? 27 | 28 | [![What is an API](assets/imgs/What_is_an_API.png)](https://youtu.be/s7wmiS2mSXY) 29 | 30 | 31 | An API - or Application Programming Interface - refers to a set of procedures and protocols that allows us to send and request information between ourselves and remote applications. You can think of this as a channel of communication to a remote server using specific commands that enable you to use their applications without needing to host that functionality yourself. Many types of APIs exist, but for this predict task we are interested specifically in Web APIs. These allow us to send and receive information using web development languages, such as HTML and JSON. The video above provides a simple and intuitive explanation of how APIs operate. 32 | 33 | #### 1.2) How our API will work 34 | 35 |

36 | Flask Web-server 39 |
40 | A representation of the API framework that will be created within this predict task. 41 |

42 | 43 | ##### Description of files 44 | 45 | Several files within this repository enable the correct functioning of our API. We provide a high-level description of these salient files within the table below: 46 | 47 | | File Name | Description | 48 | | :--------------------- | :-------------------- | 49 | | `api.py` | Flask web server application definition and instantiation. | 50 | | `model.py` | Contains helper functions to separate model specific code from our API definition. | 51 | | `utils/request.py` | Simple script to simulate a POST request sent to our API. | 52 | | `utils/train_model.py` | Code used to train the simple model used for demonstration of the API's functioning. | 53 | 54 | ## 2) Usage Instructions 55 | 56 | #### 2.1) Creating a copy of this repo 57 | 58 | | ⚡️ WARNING ⚡️ | 59 | |:--------------------| 60 | |Do **NOT** *clone* this repository. Instead follow the instructions in this section to *fork* the repo.| 61 | 62 | As described within the Predict instructions for the Regression Sprint, this code represents a *template* from which you can base your own model's API. As such, in order to modify the template to serve your own model (and the associated code changes which are required for this), you will need to **[fork](https://help.github.com/en/github/getting-started-with-github/fork-a-repo)** this repository. Failing to do this will lead to complications when trying to work on the API remotely. 63 | 64 | 65 | ![Fork Repo](assets/imgs/fork_repo.png) 66 | 67 | To fork the repo, simply ensure that you are logged into your GitHub account, and then click on the 'fork' button at the top of this page as indicated within the figure above. 68 | 69 | 70 | #### 2.2) Running the API on your local machine 71 | 72 | As a first step to becoming familiar with our API's functioning, we recommend setting up a running instance on your own local machine. 73 | 74 | To do this, follow the steps below by running the given commands within a Git bash (Windows), or terminal (Mac/Linux): 75 | 76 | 1. Ensure that you have the prerequisite Python libraries installed on your local machine: 77 | 78 | ```bash 79 | pip install -U flask numpy pandas scikit-learn 80 | ``` 81 | 82 | 2. Clone the *forked* repo to your local machine. 83 | 84 | ```bash 85 | git clone https://github.com/{your-account-name}/load-shortfall-regression-predict-api.git 86 | ``` 87 | 88 | 3. Navigate to the base of the cloned repo, and run the API web-server initialisation script. 89 | 90 | 91 | ```bash 92 | cd load-shortfall-regression-predict-api/ 93 | python api.py 94 | ``` 95 | 96 | If the web server was able to initialise successfully, the following message should be displayed within your bash/terminal session: 97 | 98 | ``` 99 | ---------------------------------------- 100 | Model successfully loaded 101 | ---------------------------------------- 102 | * Serving Flask app "api" (lazy loading) 103 | * Environment: production 104 | WARNING: This is a development server. Do not use it in a production deployment. 105 | Use a production WSGI server instead. 106 | * Debug mode: off 107 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 108 | ``` 109 | 110 | 4. Leave the web server script running within the current bash/terminal session. Open a new session, and navigate to the `utils` subfolder of the cloned repo. 111 | 112 | ``` 113 | cd {your/custom/path}/load-shortfall-regression-predict-api/utils/ 114 | ``` 115 | 116 | 5. Run the `request.py` script located within the utils subfolder to simulate a POST request for our running API. 117 | 118 | ``` 119 | python request.py 120 | ``` 121 | 122 | 123 | If you receive an error at this point, please ensure that the web server is still running in your original bash/terminal session. If the script ran successfully, you should receive similar output to the message shown below: 124 | 125 | 126 | 127 | ``` 128 | Sending POST request to web server API at: http://127.0.0.1:5000/api_v0.1 129 | 130 | Querying API with the following data: 131 | [8764, '2018-01-01 03:00:00', 4.6666666667, 'level_8', 0.0, 5.3333333333, 89.0, 78.0, 0.0, 3.6666666667000003, 0.0, 143.3333333333, 4.6666666667, 266.6666666667, 0.0, 0.6666666667, 0.0, 'sp25', 0.0, 0, 1020.3333333333001, 0.0, 0.0, 0.0, 0, 800.0, 800.3333333333001, 1026.6666666667, 800.0, nan, 282.48333333330004, 1030.3333333333, 284.15, 284.15, 721.0, 281.67333333330004, 53.6666666667, 284.15, 284.8166666667, 280.48333333330004, 284.19, 277.8166666667, 281.01, 283.48333333330004, 284.15, 281.15, 279.1933333333, 278.15] 132 | 133 | Received POST response: 134 | ************************************************** 135 | API prediction result: 9518.37493239743 136 | The response took: 0.010888 seconds 137 | ************************************************** 138 | ``` 139 | 140 | Congratulations! You've now officially deployed your first web server API, and have successfully received a response from it. 141 | 142 | With these steps completed, we're now ready to both modify the template code to place our own model within the API, and to host this API within an AWS EC2 instance. These processes are outlined within the sections below. 143 | 144 | #### 2.3) Updating the API to use your own model 145 | | ℹ️ NOTE ℹ️ | 146 | |:--------------------| 147 | |We strongly encourage you to be familiar with running the API as described in Section 2.2 before attempting to use your own model.| 148 | 149 | ##### Prerequisites 150 | Before you can update the API code-base to use your own custom model, you will need to have the following: 151 | 152 | - Your own `sklearn` model, trained and saved as a `.pkl` file. 153 | 154 | For a simple example of how to pickle your model, review the script found in `utils/train_model.py`. For further instructions, consult the *'Saving and Restoring Models in Python'* train in Athena. 155 | 156 | (Note: You are not limited to use only a single model within the API. Furthermore, other `sklearn` structures which have saved parameters may be required for your model to function as well. Obviously, you are expected to handle the loading of such structures in a similar way as described within this section.) 157 | 158 | - Code for the data preprocessing pipeline used to train your model. 159 | 160 | This code should cover aspects such as data cleaning, feature engineering, feature selection, and feature transformations. 161 | 162 | The requirement of this code is vital as your API is built to provide a standard interface for POST requests. I.e. someone asking your API to make a prediction shouldn't have to worry about what specific features your model uses internally. Instead, anyone who sends a request with the standard features within the public dataset, should expect to receive a prediction result. This design principle makes it far easier to swap out an old model for a newer one, even if ends up using radically different features. 163 | 164 | ##### Making the changes 165 | 166 | Once you've gathered the prerequisites from the above section, making the changes to API is relatively straight forward. It involves three steps: 167 | 168 | 1. Place your `.pkl` file within the `assets/trained-models/` directory of the repo. 169 | 170 | 2. Modify the `api.py` file by changing the `path_to_model` variable to reflect the new model `.pkl` file. 171 | 172 | 3. Modify the `model.py` file by adding your data preprocessing code to the `_preprocess_data()` helper function. 173 | 174 | If the following steps were carried out successfully, running the API should now produce a new prediction result. 175 | 176 | #### 2.4) Running the API on a remote AWS EC2 instance 177 | | ℹ️ NOTE ℹ️ | 178 | |:--------------------| 179 | |You will only be able to work on this section of the API setup once you've completed the *'Introduction to Amazon AWS - Part I'* train on Athena.| 180 | 181 | The following steps will enable you to run your web server API on a remote EC2 instance, allowing it to the queried by any device/application which has internet access. 182 | 183 | Within these setup steps, we will be using a remote EC2 instance, which we will refer to as the ***Host***, in addition to our local machine, which we will call the ***Client***. We use these designations for convenience, and to align our terminology with that of common web server practices. In cases where commands are provided, use Git bash (Windows) or Terminal (Mac/Linux) to enter these. 184 | 185 | 1. Ensure that you have access to a running AWS EC2 instance with an assigned public IP address. Instructions for this process are found within the *'Introduction to Amazon AWS - Part I'* train on Athena. 186 | 187 | 2. Install the prerequisite python libraries on both the Host (EC2 instance), and Client (local machine): 188 | 189 | ```bash 190 | pip install -U flask numpy pandas scikit-learn 191 | ``` 192 | 193 | 3. Clone your copy of the API repo onto both the Host and Client machines, then navigate to the base of the cloned repo: 194 | 195 | ```bash 196 | git clone https://github.com/{your-account-name}/load-shortfall-regression-predict-api-template.git 197 | cd load-shortfall-regression-predict-api-template/ 198 | ``` 199 | **[On the Host]:** 200 | 201 | 4. Run the API web-server initialisation script. 202 | 203 | ```bash 204 | python api.py 205 | ``` 206 | 207 | If this command ran successfully, the following output should be observed on the Host: 208 | 209 | ``` 210 | ---------------------------------------- 211 | Model successfully loaded 212 | ---------------------------------------- 213 | * Serving Flask app "api" (lazy loading) 214 | * Environment: production 215 | WARNING: This is a development server. Do not use it in a production deployment. 216 | Use a production WSGI server instead. 217 | * Debug mode: off 218 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 219 | ``` 220 | 221 | **[On the Client]:** 222 | 223 | 5. Navigate to the `utils` subdirectory within the repo. 224 | 225 | ```bash 226 | cd utils/ 227 | ``` 228 | 229 | 6. Open the `request.py` file using your favorite text editor. 230 | 231 | Change the value of the `url` variable to reflect the ***public IP address*** of the Host. (Instructions for getting the public IP address are provided within the *‘Introduction to Amazon AWS - Part I’* train on Athena.) 232 | 233 | ```bash 234 | url = 'http://{public-ip-address-of-remote-machine}:5000/api_v0.1' 235 | ``` 236 | 237 | 7. Once the editing is completed, close the file and run it: 238 | 239 | ```bash 240 | python request.py 241 | ``` 242 | 243 | If the command ran successfully, you should see output similar to the following: 244 | 245 | ``` 246 | Sending POST request to web server API at: http://54.229.152.221:5000/api_v0.1 247 | 248 | Querying API with the following data: 249 | [8764, '2018-01-01 03:00:00', 4.6666666667, 'level_8', 0.0, 5.3333333333, 89.0, 78.0, 0.0, 3.6666666667000003, 0.0, 143.3333333333, 4.6666666667, 266.6666666667, 0.0, 0.6666666667, 0.0, 'sp25', 0.0, 0, 1020.3333333333001, 0.0, 0.0, 0.0, 0, 800.0, 800.3333333333001, 1026.6666666667, 800.0, nan, 282.48333333330004, 1030.3333333333, 284.15, 284.15, 721.0, 281.67333333330004, 53.6666666667, 284.15, 284.8166666667, 280.48333333330004, 284.19, 277.8166666667, 281.01, 283.48333333330004, 284.15, 281.15, 279.1933333333, 278.15] 250 | 251 | Received POST response: 252 | ************************************************** 253 | API prediction result: 9518.37493239743 254 | The response took: 0.010888 seconds 255 | ************************************************** 256 | ``` 257 | 258 | If you have completed the steps in 2.3), then the prediction result should differ from the one given above. 259 | 260 | **[On the Host]** 261 | 262 | You should also see an update to the web server output, indicating that it was contacted by the Client (the values of this string will differ for your output): 263 | 264 | ``` 265 | 102.165.194.240 - - [08/June/2021 07:31:31] "POST /api_v0.1 HTTP/1.1" 200 - 266 | ``` 267 | 268 | If you are able to see these messages on both the Host and Client, then your API has successfully been deployed to the Web. Snap ⚡️! 269 | 270 | ## 3) FAQ 271 | 272 | This section of the repo will be periodically updated to represent common questions which may arise around its use. If you detect any problems/bugs, please [create an issue](https://help.github.com/en/github/managing-your-work-on-github/creating-an-issue) and we will do our best to resolve it as quickly as possible. 273 | 274 | We wish you all the best in your learning experience :rocket: 275 | 276 |

277 | EXPLORE Data Science Academy Logo 280 |
281 |

282 | -------------------------------------------------------------------------------- /api.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Simple Flask-based API for Serving an sklearn Model. 4 | 5 | Author: Explore Data Science Academy. 6 | 7 | Note: 8 | --------------------------------------------------------------------- 9 | Please follow the instructions provided within the README.md file 10 | located within this directory for guidance on how to use this script 11 | correctly. 12 | --------------------------------------------------------------------- 13 | 14 | Description: This file instantiates a Flask webserver 15 | as a means to create a simple API used to deploy models trained within 16 | the sklearn framework. 17 | 18 | """ 19 | 20 | # API Dependencies 21 | import pickle 22 | import json 23 | import numpy as np 24 | from model import load_model, make_prediction 25 | from flask import Flask, request, jsonify 26 | 27 | # Application definition 28 | app = Flask(__name__) 29 | 30 | # Load our model into memory. 31 | # Please update this path to reflect your own trained model. 32 | static_model = load_model( 33 | path_to_model='assets/trained-models/load_shortfall_simple_lm_regression.pkl') 34 | 35 | print ('-'*40) 36 | print ('Model successfully loaded') 37 | print ('-'*40) 38 | 39 | """ You may use this section (above the app routing function) of the python script to implement 40 | any auxiliary functions required to process your model's artifacts. 41 | """ 42 | 43 | 44 | # Define the API's interface. 45 | # Here the 'model_prediction()' function will be called when a POST request 46 | # is sent to our interface located at: 47 | # http:{Host-machine-ip-address}:5000/api_v0.1 48 | @app.route('/api_v0.1', methods=['POST']) 49 | def model_prediction(): 50 | # We retrieve the data payload of the POST request 51 | data = request.get_json(force=True) 52 | # We then preprocess our data, and use our pretrained model to make a 53 | # prediction. 54 | output = make_prediction(data, static_model) 55 | # We finally package this prediction as a JSON object to deliver a valid 56 | # response with our API. 57 | return jsonify(output) 58 | 59 | # Configure Server Startup properties. 60 | # Note: 61 | # When developing your API, set `debug=True` 62 | # This will allow Flask to automatically restart itself everytime you 63 | # update your API code. 64 | if __name__ == '__main__': 65 | app.run(host='0.0.0.0', port=5000, debug=False) 66 | -------------------------------------------------------------------------------- /assets/imgs/API.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Explore-AI/load-shortfall-regression-predict-api/4335715712db7bdde9c884f802d374f6b8386a53/assets/imgs/API.png -------------------------------------------------------------------------------- /assets/imgs/EDSA_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Explore-AI/load-shortfall-regression-predict-api/4335715712db7bdde9c884f802d374f6b8386a53/assets/imgs/EDSA_logo.png -------------------------------------------------------------------------------- /assets/imgs/Flask_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Explore-AI/load-shortfall-regression-predict-api/4335715712db7bdde9c884f802d374f6b8386a53/assets/imgs/Flask_logo.png -------------------------------------------------------------------------------- /assets/imgs/What_is_an_API.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Explore-AI/load-shortfall-regression-predict-api/4335715712db7bdde9c884f802d374f6b8386a53/assets/imgs/What_is_an_API.png -------------------------------------------------------------------------------- /assets/imgs/fork_repo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Explore-AI/load-shortfall-regression-predict-api/4335715712db7bdde9c884f802d374f6b8386a53/assets/imgs/fork_repo.png -------------------------------------------------------------------------------- /assets/trained-models/load_shortfall_simple_lm_regression.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Explore-AI/load-shortfall-regression-predict-api/4335715712db7bdde9c884f802d374f6b8386a53/assets/trained-models/load_shortfall_simple_lm_regression.pkl -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Helper functions for the pretrained model to be used within our API. 4 | 5 | Author: Explore Data Science Academy. 6 | 7 | Note: 8 | --------------------------------------------------------------------- 9 | Please follow the instructions provided within the README.md file 10 | located within this directory for guidance on how to use this script 11 | correctly. 12 | 13 | Importantly, you will need to modify this file by adding 14 | your own data preprocessing steps within the `_preprocess_data()` 15 | function. 16 | ---------------------------------------------------------------------- 17 | 18 | Description: This file contains several functions used to abstract aspects 19 | of model interaction within the API. This includes loading a model from 20 | file, data preprocessing, and model prediction. 21 | 22 | """ 23 | 24 | # Helper Dependencies 25 | import numpy as np 26 | import pandas as pd 27 | import pickle 28 | import json 29 | 30 | def _preprocess_data(data): 31 | """Private helper function to preprocess data for model prediction. 32 | 33 | NB: If you have utilised feature engineering/selection in order to create 34 | your final model you will need to define the code here. 35 | 36 | 37 | Parameters 38 | ---------- 39 | data : str 40 | The data payload received within POST requests sent to our API. 41 | 42 | Returns 43 | ------- 44 | Pandas DataFrame : 45 | The preprocessed data, ready to be used our model for prediction. 46 | """ 47 | # Convert the json string to a python dictionary object 48 | feature_vector_dict = json.loads(data) 49 | # Load the dictionary as a Pandas DataFrame. 50 | feature_vector_df = pd.DataFrame.from_dict([feature_vector_dict]) 51 | 52 | # --------------------------------------------------------------- 53 | # NOTE: You will need to swap the lines below for your own data 54 | # preprocessing methods. 55 | # 56 | # The code below is for demonstration purposes only. You will not 57 | # receive marks for submitting this code in an unchanged state. 58 | # --------------------------------------------------------------- 59 | 60 | # ----------- Replace this code with your own preprocessing steps -------- 61 | predict_vector = feature_vector_df[['Madrid_wind_speed','Bilbao_rain_1h','Valencia_wind_speed']] 62 | # ------------------------------------------------------------------------ 63 | 64 | return predict_vector 65 | 66 | def load_model(path_to_model:str): 67 | """Adapter function to load our pretrained model into memory. 68 | 69 | Parameters 70 | ---------- 71 | path_to_model : str 72 | The relative path to the model weights/schema to load. 73 | Note that unless another file format is used, this needs to be a 74 | .pkl file. 75 | 76 | Returns 77 | ------- 78 | 79 | The pretrained model loaded into memory. 80 | 81 | """ 82 | return pickle.load(open(path_to_model, 'rb')) 83 | 84 | 85 | """ You may use this section (above the make_prediction function) of the python script to implement 86 | any auxiliary functions required to process your model's artifacts. 87 | """ 88 | 89 | def make_prediction(data, model): 90 | """Prepare request data for model prediction. 91 | 92 | Parameters 93 | ---------- 94 | data : str 95 | The data payload received within POST requests sent to our API. 96 | model : 97 | An sklearn model object. 98 | 99 | Returns 100 | ------- 101 | list 102 | A 1-D python list containing the model prediction. 103 | 104 | """ 105 | # Data preprocessing. 106 | prep_data = _preprocess_data(data) 107 | # Perform prediction with model and preprocessed data. 108 | prediction = model.predict(prep_data) 109 | # Format as list for output standardisation. 110 | return prediction[0].tolist() 111 | -------------------------------------------------------------------------------- /utils/request.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Simple Script to test the API once deployed 4 | 5 | Author: Explore Data Science Academy. 6 | 7 | Note: 8 | --------------------------------------------------------------------- 9 | Please follow the instructions provided within the README.md file 10 | located at the root of this repo for guidance on how to use this 11 | script correctly. 12 | ---------------------------------------------------------------------- 13 | 14 | Description: This file contains code used to formulate a POST request 15 | which can be used to develop/debug the Model API once it has been 16 | deployed. 17 | 18 | """ 19 | 20 | # Import dependencies 21 | import requests 22 | import pandas as pd 23 | import numpy as np 24 | 25 | # Load data from file to send as an API POST request. 26 | # We prepare a DataFrame with the public test set + riders data 27 | # from the Kaggle challenge. 28 | test = pd.read_csv('./data/df_test.csv') 29 | 30 | 31 | # Convert our DataFrame to a JSON string. 32 | # This step is necessary in order to transmit our data via HTTP/S 33 | feature_vector_json = test.iloc[1].to_json() 34 | 35 | # Specify the URL at which the API will be hosted. 36 | # NOTE: When testing your instance of the API on a remote machine 37 | # replace the URL below with its public IP: 38 | 39 | # url = 'http://{public-ip-address-of-remote-machine}:5000/api_v0.1' 40 | url = 'http://127.0.0.1:5000/api_v0.1' 41 | 42 | # Perform the POST request. 43 | print(f"Sending POST request to web server API at: {url}") 44 | print("") 45 | print(f"Querying API with the following data: \n {test.iloc[1].to_list()}") 46 | print("") 47 | # Here `api_response` represents the response we get from our API 48 | api_response = requests.post(url, json=feature_vector_json) 49 | 50 | # Display the prediction result 51 | print("Received POST response:") 52 | print("*"*50) 53 | print(f"API prediction result: {api_response.json()[0]}") 54 | print(f"The response took: {api_response.elapsed.total_seconds()} seconds") 55 | print("*"*50) 56 | -------------------------------------------------------------------------------- /utils/train_model.py: -------------------------------------------------------------------------------- 1 | """ 2 | Simple file to create a sklearn model for deployment in our API 3 | 4 | Author: Explore Data Science Academy 5 | 6 | Description: This script is responsible for training a simple linear 7 | regression model which is used within the API for initial demonstration 8 | purposes. 9 | 10 | """ 11 | 12 | # Dependencies 13 | import pandas as pd 14 | import pickle 15 | from sklearn.linear_model import LinearRegression 16 | 17 | # Fetch training data and preprocess for modeling 18 | train = pd.read_csv('./data/df_train.csv') 19 | 20 | y_train = train[['load_shortfall_3h']] 21 | X_train = train[['Madrid_wind_speed','Bilbao_rain_1h','Valencia_wind_speed']] 22 | 23 | # Fit model 24 | lm_regression = LinearRegression(normalize=True) 25 | print ("Training Model...") 26 | lm_regression.fit(X_train, y_train) 27 | 28 | # Pickle model for use within our API 29 | save_path = '../assets/trained-models/load_shortfall_simple_lm_regression.pkl' 30 | print (f"Training completed. Saving model to: {save_path}") 31 | pickle.dump(lm_regression, open(save_path,'wb')) 32 | --------------------------------------------------------------------------------