是什么导致样本JSON必须在烧瓶应用中重塑?
我在使用Postman发送JSON的Blask应用程序上遇到了麻烦。该应用程序运行,但是当我尝试使用Postman发送示例JSON时,我会收到一个重塑错误:“如果您的数据具有单个功能或array.Reshape,请使用Array.Reshape(-1,1)重塑您的数据( 1,-1)如果它包含一个样本。”通常,我知道如何在模型中解决这个问题,但是我不确定这是如何适用于示例JSON的。我该如何解决?
烧瓶代码:
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()
通过Postman发送的示例JSON:
{"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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论