当使用FLASK部署模型时得到这个错误。TypeError: 输入数据不能是一个列表

问题描述 投票:0回答:1

这是我得到的错误。

这是发生在 "prediction = model.predict(final_features) "处的错误。

TypeError: Input data can not be a list.

Traceback (most recent call last)
Open an interactive python shell in this frameprediction = model.predict(final_features)
[console ready]

这是我的app.py代码。

from flask import Flask, request, render_template
import numpy as np
import pickle


# initialize the flask app
app = Flask(__name__)

# open the pickle file in the read mode
model = pickle.load(open('model.pkl', 'rb'))

# root node for API URL
@app.route('/')
def home():
   return render_template('index.html') # rewrite to the index.html file

# create another API
@app.route('/predict', methods=['POST', 'GET'])
def predict():
   '''
   For rendering results on HTML GUI
   '''
# int value of dependent variables
int_features = [int(x) for x in request.form.values()]

# convert the above to the array
final_features = [np.array(int_features)]

# perform prediction
prediction = model.predict(final_features)
print(prediction)

# get the prediction
output = round(prediction[0], 2)

return render_template('index.html', prediction_text='The predicted number of hours you listen to K-Pop is {} hours'.format(output))

if __name__ == "__main__":
   app.run(debug=True)

不知道为什么我得到一个错误,输入数据不能是一个列表。

python-3.x flask deployment model web-deployment
1个回答
0
投票

你可以尝试修改

# convert the above to the array
final_features = [np.array(int_features)]

# convert the above to the array
final_features = np.array(int_features)

还: print(final_features)

要把网页输入转换为正确的格式,总是很棘手。我觉得是某个地方出现了小bug。

© www.soinside.com 2019 - 2024. All rights reserved.