在烧瓶中将csv作为json返回

时间:2017-09-18 14:15:11

标签: python flask

我想在JSON中返回保存在joblib中的机器学习模型的新数据预测(new_data.csv)。

# -*- coding: utf-8 -*-
from flask import Flask,request, jsonify
from sklearn.externals import joblib
import pandas as pd

app = Flask(__name__)

@app.route('/predict', methods=['GET', 'POST'])

def predict() :    
    json_ = request.json
    new = pd.read_csv('new_data.csv')
    json_vector = new.transform(json_)
    query = pd.DataFrame(json_vector)
    prediction = regr.predict(query)
    return json.dumps({'prediction': list({{prediction}})})

if __name__ == '__main__' :
     regr = joblib.load('model.pkl')
     app.run(port=8080, debug=True)

1 个答案:

答案 0 :(得分:0)

您可以使用jsonify:

from flask import jsonify

def predict() :    
    json_ = request.json
    new = pd.read_csv('new_data.csv')
    json_vector = new.transform(json_)
    query = pd.DataFrame(json_vector)
    prediction = regr.predict(query)
    data = {'prediction': list({{prediction}})}
    return jsonify(data)