Flask Web应用程序中的机器学习模型错误

时间:2019-04-21 17:30:59

标签: machine-learning flask model

我已经创建了用于心脏病预测的机器学习模型,现在我想使用FLASK在我的Web应用程序中进行部署。数据集是从Kaggle获得的。每当我运行该应用程序时,我在执行该代码时都会遇到一些问题,它说:

C:\Users\Surface\Desktop\Flask_app>python app.py                                                                          File "app.py", line 42                                                                                                   
 x_data = request.form['x_data']                                                                                                                                 
                              ^                                                                             
IndentationError: unindent does not match any outer indentation level   

任何人都可以引导我谢谢你:)

from flask import Flask,render_template,url_for,request
import numpy as np
import pandas as pd
import pickle
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib

app = Flask(__name__)
@app.route('/')
def home():
    return render_template('home.html')

@app.route('/predict',method=['POST'])
def predict():
    df = pd.read_csv("heart.csv")
    df = df.drop(columns = ['cp', 'thal', 'slope'])

#features and labels
    y = df.target.values
    x_data = df.drop(['target'], axis = 1)

#EXTRACT Features
    x = (x_data - np.min(x_data)) / (np.max(x_data) - np.min(x_data)).values
    x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.2,random_state=0)

# Random Forest Classification
    from sklearn.ensemble import RandomForestClassifier
    rf = RandomForestClassifier(n_estimators = 1000, random_state = 1)
    rf.fit(x_train.T, y_train.T)
    print("Random Forest Algorithm Accuracy Score : {:.2f}%".format(rf.score(x_test.T,y_test.T)*100))


#persist model in a standard format
    from sklearn.externals import joblib
    joblib.dump(rf, 'HAP_model.pkl')
    HAP_model = open('HAP_model.pkl','rb')
    rf = joblib.load(HAP_model)

    if request.method=='POST':
        x_data = request.form['x_data']
    data = [df.drop(['target'], axis = 1)]
    vect = rf.transform(data).toarray()
    my_prediction = rf.predict(vect)
    return render_template('result.html',prediction = my_prediction)


    if __name__ == '__main__':
    app.run(debug=True)

1 个答案:

答案 0 :(得分:1)

可以改善您的预测延迟的一件事是将训练代码从导入hearts.csv转移到将模型另存为咸菜之外。这样,当收到新请求时,您不必每次都重新训练模型。这样,它将增加您的延迟。

您可以使用的另一个解决方案是该库,称为BentoML(www.github.com/bentoml/bentoml)。它是用于将ML模型打包和部署到生产中的库。它使用内置的REST API服务器生成了模型存档。您甚至不必再编写flask应用程序。

针对您的项目。我认为这与scikit-learn示例https://github.com/bentoml/BentoML/blob/master/examples/sklearn-sentiment-clf/sklearn-sentiment-clf.ipynb非常相似。