我正在尝试使用预测函数来获取模型的输出。
我将coo矩阵(x_test)转换为numpy(x_test_final)并打印其形状。现在,我正在尝试使用预测功能。
def predict1():
str_features = [str(x) for x in request.form.values()]
description= process(str_features[0])
x1_tfidf_load_sub = pickle.load(open("vocab1.pickle", 'rb'))
x2_tfidf_load_sub = pickle.load(open("vocab2.pickle", 'rb'))
description= [description]
closure= [str_features[1]]
tfidf_vectorizer1 = TfidfVectorizer(vocabulary=x1_tfidf_load_sub,use_idf=False, norm=None)
X1_test = tfidf_vectorizer1.fit_transform(description)
tfidf_vectorizer2 = TfidfVectorizer(vocabulary=x2_tfidf_load_sub,use_idf=False, norm=None)
X2_test = tfidf_vectorizer2.fit_transform(closure)
x_test = hstack((X1_test, X2_test))
x_test_final=x_test.toarray()
print('###########')
print(x_test_final.shape)
print('###########')
prediction = model.predict(x_test_final)
但是出现以下错误:
###########
(1, 133809)
###########
127.0.0.1 - - [04/May/2020 11:38:59] "[35m[1mPOST /predict HTTP/1.1[0m" 500 -
Traceback (most recent call last):
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Pratiksha_S\Desktop\Deployment-flask-master\Deployment-flask-master\app.py", line 69, in predict1
prediction = model.predict(x_test_final)
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sklearn\naive_bayes.py", line 77, in predict
jll = self._joint_log_likelihood(X)
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sklearn\naive_bayes.py", line 770, in _joint_log_likelihood
return (safe_sparse_dot(X, self.feature_log_prob_.T) +
File "C:\Users\Pratiksha_S\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sklearn\utils\extmath.py", line 151, in safe_sparse_dot
ret = a @ b
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 133896 is different from 133809)
127.0.0.1 - - [04/May/2020 11:38:59] "[37mGET /predict?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1[0m" 200 -
127.0.0.1 - - [04/May/2020 11:38:59] "[37mGET /predict?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1[0m" 200 -
127.0.0.1 - - [04/May/2020 11:38:59] "[37mGET /predict?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1[0m" 200 -
127.0.0.1 - - [04/May/2020 11:38:59] "[37mGET /predict?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1[0m" 200 -
127.0.0.1 - - [04/May/2020 11:38:59] "[37mGET /predict?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1[0m" 200 -
133896的大小是多少?为什么会输出此错误?
我在这里使用的模型是多项朴素贝叶斯。 当我在Jupyter Notebook中运行此模型时,它的工作原理绝对不错。当我导入泡菜文件并在flask代码中使用它时,出现此错误。