-1

I would like to return in JSON the new data forecasts (new_data.csv) of a machine learning model that was saved in joblib.

# -*- 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)
Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41

1 Answers1

0

You can use 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)
lapinkoira
  • 8,320
  • 9
  • 51
  • 94