source As a beginner in machine learning, it might be easy for anyone to get enough resources about all the algorithms for machine learning and deep learning but when I started to look for references to deploy ML model to production I did not find really any good resources which could help me to deploy my model as I am very new to this field. So, when I succeeded to deploy my model using Flask as an API, I decided to write an article to help others to simply deploy their model. I hope it helps:) In this article, we are going to use simple linear regression algorithm with scikit-learn for simplicity, we will use Flask as it is a very light web framework. We will create three files, model.py server.py request.py In a file, we will develop and train our model, in a we will code to handle POST requests and return the results and finally in the , we will send requests with the features to the server and receive the results. model.py server.py, request.py Let’s begin the coding part model.py As I mentioned above, in this file we will develop our ML model and train it. We will predict the salary of an employee based on his/her experience in the field. You can find the dataset . here import numpy as npimport pandas as pdfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LinearRegressionimport pickleimport requestsimport json Importing the libraries that we are going to use to develop our model. and to manipulate the matrices and data respectively, for splitting data into train and test set and to train our model using . to save our trained model to the disk, to send requests to the server and to print the result in our terminal. numpy pandas sklearn.model_selection sklearn.linear_model LinearRegression pickle requests json dataset = pd.read_csv('Salary_Data.csv')X = dataset.iloc[:, :-1].valuesy = dataset.iloc[:, 1].values We have imported the dataset using pandas and separated the features and label from the dataset. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 0) In this section, we have split our data into train and test size of 0.67 and 0.33 respectively using from train_test_split sklearn. regressor = LinearRegression()regressor.fit(X_train, y_train) y_pred = regressor.predict(X_test) The object is instantiated as a of the class and trained using and Latter the predicted results are stored in the regressor LinearRegression() X_train y_train. y_pred. pickle.dump(regressor, open('model.pkl','wb')) We will save our trained model to the disk using the library. is used to serializing and de-serializing a Python object structure. In which python object is converted into the byte stream. method dumps the object into the file specified in the arguments. pickle Pickle dump() In our case, we want to save our model so that it can be used by the server. So we will save our object to the file named regressor model.pkl. We can again load the model by the following method, model = pickle.load(open('model.pkl','rb'))print(model.predict([[1.8]])) method loads the method and saves the deserialized bytes to Predictions can be done using pickle.load() model. model.predict(). For example, we can predict the salary of the employee who has experience of 1.8 years. Here, our is ready to train and save the model. The whole code of is as follows. model.py model.py # Importing the librariesimport numpy as npimport pandas as pdfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LinearRegressionimport pickleimport requestsimport json # Importing the datasetdataset = pd.read_csv('Salary_Data.csv')X = dataset.iloc[:, :-1].valuesy = dataset.iloc[:, 1].values # Splitting the dataset into the Training set and Test setX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0) # Fitting Simple Linear Regression to the Training setregressor = LinearRegression()regressor.fit(X_train, y_train) # Predicting the Test set resultsy_pred = regressor.predict(X_test) # Saving model to diskpickle.dump(regressor, open('model.pkl','wb')) # Loading model to compare the resultsmodel = pickle.load(open('model.pkl','rb'))print(model.predict([[1.8]])) 2. server.py In this file, we will use the flask web framework to handle the POST requests that we will get from the . request.py Importing the methods and libraries that we are going to use in the code. import numpy as npfrom flask import Flask, request, jsonifyimport pickle Here we have imported to create the array of requested data, to load our trained model to predict. numpy pickle In the following section of the code, we have created the instance of the and loaded the model into the Flask() model. app = Flask(__name__) model = pickle.load(open('model.pkl','rb')) Here, we have bounded with the method In which predict method gets the data from the json passed by the requestor. method takes input from the json and converts it into 2D the results are stored into the variable named and we return this variable after converting it into the json object using flasks method. /api predict(). model.predict() numpy array output jsonify() .route('/api',methods=['POST'])def predict():data = request.get_json(force=True)prediction = model.predict([[np.array(data['exp'])]])output = prediction[0]return jsonify(output) @app Finally, we will run our server by following code section. Here I have used port 5000 and have set since if we get any error we can debug it and solve it. debug=True if __name__ == '__main__':app.run(port=5000, debug=True) Here, our server is ready to serve the requests. Here is the whole code of the server.py. # Import librariesimport numpy as npfrom flask import Flask, request, jsonifyimport pickle app = Flask(__name__) # Load the modelmodel = pickle.load(open('model.pkl','rb')) @app.route('/api',methods=['POST'])def predict():# Get the data from the POST request.data = request.get_json(force=True) # Make prediction using model loaded from disk as per the data. prediction = model.predict(\[\[np.array(data\['exp'\])\]\]) # Take the first value of prediction output = prediction\[0\] return jsonify(output) if __name__ == '__main__':app.run(port=5000, debug=True) 3. request.py As I mentioned earlier that is going to request the server for the predictions. request.py Here is the whole code to make a request to the server. import requests url = ' http://localhost:5000/api' r = requests.post(url,json={'exp':1.8,})print(r.json()) We have used library to make post requests. takes URL and the data to be passed in the POST request and the returned results from the servers are stored into the variable and printed by requests requests.post() r r.json(). Conclusion We have created three files to train and save a model, to handle the request, to make a request to the server respectively. model.py, server.py and request.py After coding all of these files, the sequence to execute the files should be , (in separate terminal) and at the end . model.py server.py request.py You can compare the results of prediction with a as we printing the result at the end of the file. model.py You can find all the coding in my Github repository, . flask-salary-predictor If you found it interesting don’t forget to clap and put down your thoughts below. Thank you :) Check out my other articles as well. Code Your Own Popularity Based Recommendation System WITHOUT a Library in Python Simple linear regression using python without Scikit-Learn