这是我得到的错误。
这是发生在 "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)
不知道为什么我得到一个错误,输入数据不能是一个列表。
你可以尝试修改
# 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。