0
我正在尝试创建一个用于预测工作薪水的Web应用程序。我已经在我的电脑上离线训练了我的模型,现在正在尝试使Flask应用根据用户输入做出预测。使用1条记录为来自用户输入的预测创建虚拟变量
Training script: https://github.com/datomnurdin/webscraping-indeed/blob/master/analyzer.ipynb
Model: https://github.com/datomnurdin/webscraping-indeed/blob/master/model.pkl
Index dict: https://github.com/datomnurdin/webscraping-indeed/blob/master/cat
Flask web app: https://github.com/datomnurdin/job-salary-prediction
所以现在我的特征向量是470长。我用腌菜保存了我的模型,现在正试图根据用户输入进行预测。现在用户输入的形式是3个变量(城市,标题,摘要)。
但我得到这个错误信息,因为我不知道如何将用户输入转换为我的模型的特征向量。
错误消息
[2017-11-25 19:21:36,504] ERROR in app: Exception on /predict [POST]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2164, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1743, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1646, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1741, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1727, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/ZERO/Documents/Github/job-salary-prediction/app.py", line 27, in get_delay
logmodel = pickle.load(pkl_file)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1384, in load
return Unpickler(file).load()
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 864, in load
dispatch[key](self)
KeyError: '\x00'
瓶的Web应用程序
from flask import Flask, request, render_template
import pickle
import numpy as np
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/predict',methods=['POST','GET'])
def get_delay():
if request.method=='POST':
result=request.form
#Prepare the feature vector for prediction
pkl_file = open('cat', 'rb')
index_dict = pickle.load(pkl_file)
new_vector = np.zeros(len(index_dict))
try:
new_vector[index_dict['city']] = 1
except:
pass
try:
new_vector[index_dict['title']] = 1
except:
pass
try:
new_vector[index_dict['summary']] = 1
except:
pass
pkl_file = open('model.pkl', 'rb')
logmodel = pickle.load(pkl_file)
prediction = logmodel.predict(new_vector)
return render_template('result.html',prediction=prediction)
if __name__ == '__main__':
app.run()
什么是用户输入转换为我的模型特征向量的最有效方法是什么?