0

I'm having trouble with a Flask app that uses Postman to send a JSON. The app runs, but when I try to use Postman to send a sample JSON, I get a reshaping error: "Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample." Typically I know how to solve this in, say, a model, but I'm not sure how this applies to a sample JSON. How can I fix this?

Flask code:

app = Flask(__name__)

@app.route("/")
def index():
    return '<h1>Flask Running</h>'

college_model = joblib.load("college_model.pkl")

column_trans = make_column_transformer(
(OneHotEncoder(), ['type_school','school_accreditation',
              'gender','interest','residence','parent_was_in_college']),
remainder='passthrough')

@app.route('/college', methods=['POST'])
def prediction():
    content = request.json
    encode = column_trans.fit_transform(content)
    pred = college_model.predict(encode)
    pred = pred.tolist()
    return jsonify(pred)

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

Sample JSON sent via Postman:

{"type_school": "Academic",
"school_accreditation": "A",
"gender": "Male",
"interest": "Less Interested",
"residence": "Urban",
"parent_age": 56,
"parent_salary": 76000,
"house_area": 83.09,
"average_grades": 85,
"parent_was_in_college": false
}
Tyrone_Slothrop
  • 177
  • 4
  • 11
  • Please, post the full traceback you get – buran Jun 18 '22 at 18:02
  • Does this help? https://stackoverflow.com/questions/35082140/preprocessing-in-scikit-learn-single-sample-depreciation-warning sounds like you may need to wrap the dict object in a list in the JSON request – Anentropic Jun 18 '22 at 18:02
  • That looks promising, but how would I do that in Postman? can you put Python code in a Postman request? – Tyrone_Slothrop Jun 18 '22 at 18:07

0 Answers0