是什么导致样本JSON必须在烧瓶应用中重塑?

发布于 2025-02-08 12:13:43 字数 1154 浏览 1 评论 0原文

我在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文