我已经训练了一个模型,并使用python flask作为Web应用程序部署到了localhost。
在Web应用程序上看到的预测以“ LIL”(列表列表)矩阵形式显示
(0,1) 1
(0,3) 1
训练模型时没有得到这个问题,因为我使用了多标签二进制化器的逆变换
clf.predict(X_train,y_train)
pred = clf.predict(X_test)
pred_inverse = lb.inverse_transform(pred)
print(pred_inverse)
当我实现inverse_transform时,我得到的错误为
sklearn.exceptions.NotFittedError: This MultiLabelBinarizer instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
当我尝试使用时:
from sklearn.preprocessing import MultiabelBinarizer
lb = MultiLabelBinarizer()
pred_a = lb.fit_transform(prediction)
我收到错误消息
TypeError: unhashable type: 'lil_matrix'
我的完整代码(server.py)如下
import numpy as np
from flask import Flask, request, jsonify, render_template
from sklearn.externals import joblib
app = Flask(__name__)
model = joblib.load(open('ensemble.sav', 'rb'))
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict',methods=['POST'])
def predict():
'''
For rendering results on HTML GUI
'''
int_features = [int(x) for x in request.form.values()]
final_features = [np.array(int_features)]
prediction = model.predict(final_features)
output = jsonify(prediction[0])
return render_template('index.html', prediction_text='Mood of the song could be {}'.format(output))
@app.route('/predict_api',methods=['POST'])
def predict_api():
'''
For direct API calls trought request
'''
data = request.get_json(force=True)
prediction = model.predict([np.array(list(data.values()))])
output = prediction[0]
return jsonify({'prediction': str(output)})
if __name__ == "__main__":
app.run(debug=True)
有人知道我如何在server.py本身中逆转换我的预测吗?