推荐 :手把手教你用Flask轻松部署机器学习模型(附代码&链接)

2019 年 11 月 6 日 数据分析
作者: Abhinav Sagar  翻译: 申利彬  校对: 吴金笛
本文 3200字 ,建议阅读 7分钟

本文可以让你把训练好的机器学习模型使用Flask API 投入生产环境。


本文旨在让您把训练好的机器学习模型通过Flask API 投入到生产环境 。

当数据科学或者机器学习工程师使用Scikit-learn、Tensorflow、Keras 、PyTorch等框架部署机器学习模型时,最终的目的都是使其投入生产。通常,我们在做机器学习项目的过程中,将注意力集中在数据分析,特征工程,调整参数等方面。但是,我们往往会忘记主要目标,即从模型预测结果中获得实际的价值。

部署机器学习模型或者将模型投入生产,意味着将模型提供给最终的用户或系统使用。

然而机器学习模型部署具有一定的复杂性,本文可以让你把训练好的机器学习模型使用Flask API 投入生产环境。

我将使用线性回归,通过利率和前两个月的销售额来预测第三个月的销售额。

线性回归是什么?

线性回归模型的目标是找出一个或多个特征(自变量)和一个连续目标变量(因变量)之间的关系。如果只有一个特征,则称为单变量线性回归;如果有多个特征,则称为多元线性回归。

线性回归的假设
线性回归模型可以用下面的等式表示:


线性回归图解
为什么使用Flask?
  • 容易上手使用

  • 内置开发工具和调试工具

  • 集成单元测试功能

  • 平稳的请求调度

  • 详尽的文档


项目结构
这个项目分为四个部分:
1. model.py -- 包含机器学习模型的代码,用于根据前两个月的销售额预测第三个月的销售额。
2. app.py – 包含用于从图形用户界面(GUI)或者API调用获得详细销售数据的Flask API,Flask API根据我们的模型计算预测值并返回。
3. request.py -- 使用requests模块调用app.py中定义的API并显示返回值。
4. HTML/CSS – 包含HTML模板和CSS风格代码,允许用户输入销售细节并显示第三个月的预测值。


部署机器学习模型的Pipeline

环境和工具
1. Scikit-learn
2. Pandas
3. Numpy
4. Flask

代码在哪里呢?


从代码开始,完整的项目可以在github上找到 (https://github.com/abhinavsagar/Machine-Learning-Deployment-Tutorials)。

我们使用HTML构建前端,让用户输入数据。这里有三个区域需要用户去填写—利率,第一个月的销售额以及第二个月的销售额。
<!DOCTYPE html><html ><head>  <meta charset="UTF-8">   <title>Deployment Tutorial 1</title>    <link href='https://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>   <link href='https://fonts.googleapis.com/css?family=Arimo' rel='stylesheet' type='text/css'>   <link href='https://fonts.googleapis.com/css?family=Hind:300' rel='stylesheet' type='text/css'>   <link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>   <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"></head><body style="background: #000;">    <div><h1>Sales Forecasting    </h1>         <!-- Main Input For Receiving Query to our ML -->         <form action="{{ url_for('predict')}}"method="post">          <input type="text" name="rate" placeholder="rate" required="required" />                  <input type="text" name="sales in first month" placeholder="sales in first month" required="required" />                 <input type="text" name="sales in second month" placeholder="sales in second month" required="required" />                         <button type="submit" class="btn btn-primary btn-block btn-large">Predict sales in third month</button>                         </form>                           <br>                             <br>   {{ prediction_text }}                             </div>                            </body>                            </html>


接下来,使用CSS对输入按钮、登录按钮和背景进行了一些样式设置。


@import url(https://fonts.googleapis.com/css?family=Open+Sans);html { width: 100%; height:100%; overflow:hidden; }body {width: 100%;height:100%;font-family: 'Helvetica';background: #000;color: #fff;font-size: 24px;text-align:center;letter-spacing:1.4px;}.login {position: absolute;top: 40%;left: 50%;margin: -150px 0 0 -150px;width:400px;height:400px;}
login h1 { color: #fff; text-shadow: 0 0 10px rgba(0,0,0,0.3); letter-spacing:1px;  text-align:center;   }input {width: 100%;  margin-bottom: 10px;  background: rgba(0,0,0,0.3);  border: none;  outline: none;  padding: 10px;  font-size: 13px;  color: #fff;  text-shadow: 1px 1px 1px rgba(0,0,0,0.3);  border: 1px solid rgba(0,0,0,0.3);  border-radius: 4px;  box-shadow: inset 0 -5px 45px rgba(100,100,100,0.2), 0 1px 1px rgba(255,255,255,0.2);  -webkit-transition: box-shadow .5s ease;  -moz-transition: box-shadow .5s ease;  -o-transition: box-shadow .5s ease;  -ms-transition: box-shadow .5s ease;  transition: box-shadow .5s ease;  }

我为这个项目创建了一个定制的销售数据集,它有四列——利率、第一个月的销售额、第二个月的销售额和第三个月的销售额。


我们现在构建一个机器学习模型来预测第三个月的销售额。首先使用Pandas解决缺失值问题,当一项或多项指标没有信息时,就会有缺失值发生。使用0填充利率这一列的缺失值,平均值填充第一个月销售额中的缺失值,采用线性回归的机器学习算法。

序列化和反序列化

简而言之,序列化是一种在磁盘上写入python对象的方法,该对象可以传输到任何地方,然后通过python脚本反序列化(读)回去。

序列化 反序列化
使用Pickling将是python对象形式的模型转为字符流形式,其思想是这个字符流中包含了在另一个python脚本中重建这个对象所需的所有信息。
import numpy as npimport matplotlib.pyplot as pltimport pandas as pdimport pickledataset = pd.read_csv('sales.csv')dataset['rate'].fillna(0, inplace=True)dataset['sales_in_first_month'].fillna(dataset['sales_in_first_month'].mean(), inplace=True)X = dataset.iloc[:, :3]def convert_to_int(word):    word_dict = {'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7, 'eight':8,                'nine':9, 'ten':10, 'eleven':11, 'twelve':12, 'zero':0, 0: 0}    return word_dict[word]X['rate'] = X['rate'].apply(lambda x : convert_to_int(x))y = dataset.iloc[:, -1]from sklearn.linear_model import LinearRegressionregressor = LinearRegression()
regressor.fit(X, y)pickle.dump(regressor, open('model.pkl','wb'))model = pickle.load(open('model.pkl','rb'))print(model.predict([[4, 300, 500]]))

下一部分是构建一个API,反序列化这个模型为python对象格式,并通过图形用户界面(GUI)获取详细销售数据,根据模型计算预测值。我使用index.html设置主页,并在使用POST请求方式提交表单数据时,获取预测的销售值。

可以通过另一个POST请求将结果发送给results并展示出来。它接收JSON格式的输入,并使用训练好的模型预测出可以被API端点接受的JSON格式的预测值。
import numpy as npfrom flask import Flask, request, jsonify, render_templateimport pickleapp = Flask(__name__)model = pickle.load(open('model.pkl', 'rb'))@app.route('/')def home():    return render_template('index.html')  @app.route('/predict',methods=['POST'])  def predict():      int_features = [int(x) for x in request.form.values()]         final_features = [np.array(int_features)]             prediction = model.predict(final_features)               output = round(prediction[0], 2)                   return render_template('index.html', prediction_text='Sales should              be $ {}'.format(output))@app.route('/results',methods=['POST'])def results():    data = request.get_json(force=True)    prediction = model.predict([np.array(list(data.values()))])    output = prediction[0]    return jsonify(output)if __name__ == "__main__":app.run(debug=True)
最后使用requests模块调用在app.py中定义的APIs,它的结果是第三个月销售额的预测值。
import requestsurl = 'http://localhost:5000/results'r = requests.post(url,json={'rate':5, 'sales_in_first_month':200, 'sales_in_second_month':400})print(r.json()) Results

使用下面的命令运行Web应用程序。
python app.py


在web浏览器中打开 http://127.0.1:5000/ ,将显示如下所示的GUI.

原文标题:
How to Easily Deploy Machine Learning Models Using Flask
原文链接:
https://www.kdnuggets.com/2019/10/easily-deploy-machine-learning-models-using-flask.html

译者简介:申利彬,研究生在读,主要研究方向大数据机器学习。目前在学习深度学习在NLP上的应用,希望在THU数据派平台与爱好大数据的朋友一起学习进步。

END


转自:数据派THU 公众号

版权声明:本号内容部分来自互联网,转载请注明原文链接和作者,如有侵权或出处有误请和我们联系。


合作请加QQ:365242293  

数据分析(ID : ecshujufenxi )互联网科技与数据圈自己的微信,也是WeMedia自媒体联盟成员之一,WeMedia联盟覆盖5000万人群。

登录查看更多
2

相关内容

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. And before you ask: It's BSD licensed! flask.pocoo.org/
【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
188+阅读 · 2020年6月29日
【实用书】Python机器学习Scikit-Learn应用指南,247页pdf
专知会员服务
255+阅读 · 2020年6月10日
【干货书】机器学习Python实战教程,366页pdf
专知会员服务
329+阅读 · 2020年3月17日
Sklearn 与 TensorFlow 机器学习实用指南,385页pdf
专知会员服务
126+阅读 · 2020年3月15日
TensorFlow Lite指南实战《TensorFlow Lite A primer》,附48页PPT
专知会员服务
67+阅读 · 2020年1月17日
Python机器学习课程(代码与教程)
专知
34+阅读 · 2019年5月13日
手把手教你用Python库Keras做预测(附代码)
数据派THU
14+阅读 · 2018年5月30日
Python & 机器学习之项目实践 | 赠书
人工智能头条
12+阅读 · 2017年12月26日
【机器学习】推荐13个机器学习框架
产业智能官
8+阅读 · 2017年9月10日
Arxiv
7+阅读 · 2018年6月1日
Arxiv
6+阅读 · 2018年3月28日
VIP会员
相关资讯
Python机器学习课程(代码与教程)
专知
34+阅读 · 2019年5月13日
手把手教你用Python库Keras做预测(附代码)
数据派THU
14+阅读 · 2018年5月30日
Python & 机器学习之项目实践 | 赠书
人工智能头条
12+阅读 · 2017年12月26日
【机器学习】推荐13个机器学习框架
产业智能官
8+阅读 · 2017年9月10日
Top
微信扫码咨询专知VIP会员