我想在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)
答案 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)