英文:
why this is not working, I have already installed sklearn. it is showing the error when I'm trying to execute it
问题
"为什么这不起作用,我已经安装了sklearn。
我正在尝试为ML模型创建API,但在这样做时出现了很多错误。如果有人已经做过这个,请帮助我。
我正在使用POSTMAN发送HTML请求,但我甚至无法创建API。
from flask import Flask, request, jsonify
import pickle
import numpy as np
app = Flask(__name__)
model=pickle.load(open('model.pkl','rb'))
@app.route('/')
def home():
return "Hello world";
@app.route('/predict', methods=['GET', 'POST'])
def predict():
cgpa = request.form.get('cgpa')
iq = request.form.get('iq')
profile_score = request.form.get('profile_score')
# result = {'cgpa': cgpa, 'iq': iq, 'profile_score': profile_score}
input_query = np.array([[cgpa, iq, profile_score]])
result = model.predict(input_query)[0]
return jsonify({'placement': str(result)})
if __name__ == '__main__':
app.run()
我得到了这个输出:
FLASK_APP = app.py
FLASK_ENV = development
FLASK_DEBUG = 0
在文件夹C:/Users/rohit/PycharmProjects/PlacePredictor
C:\Users\rohit\PycharmProjects\PlacePredictor\venv\Scripts\python.exe -m flask run
用法:python -m flask run [OPTIONS]
尝试'python -m flask run --help'以获得帮助。
错误:在导入'app'时引发了ImportError:
Traceback (most recent call last):
File "C:\Users\rohit\PycharmProjects\PlacePredictor\venv\Lib\site-packages\flask\cli.py", line 218, 在locate_app
__import__(module_name)
File "C:\Users\rohit\PycharmProjects\PlacePredictor\app.py", line 5, 在<module>
model=pickle.load(open('model.pkl','rb'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: 未找到模块'sklearn'
"
这是你提供的代码和错误信息的翻译。
<details>
<summary>英文:</summary>
Why this is not working, I have already installed sklearn.
I'm trying to create api for a ML model but getting so many errors in doing so . if anyone has done this please help me out.
I am using POSTMAN for sending html request but I'm not even able to create the API.
from flask import Flask, request, jsonify
import pickle
import numpy as np
app = Flask(name)
model=pickle.load(open('model.pkl','rb'))
@app.route('/')
def home():
return "Hello world"
@app.route('/predict', methods=['GET', 'POST'])
def predict():
cgpa = request.form.get('cgpa')
iq = request.form.get('iq')
profile_score = request.form.get('profile_score')
result = {'cgpa': cgpa, 'iq': iq, 'profile_score': profile_score}
input_query = np.array([[cgpa, iq, profile_score]])
result = model.predict(input_query)[0]
return jsonify({'placement': str(result)})
if name == 'main':
app.run()
I'm getting this output :
FLASK_APP = app.py
FLASK_ENV = development
FLASK_DEBUG = 0
In folder C:/Users/rohit/PycharmProjects/PlacePredictor
C:\Users\rohit\PycharmProjects\PlacePredictor\venv\Scripts\python.exe -m flask run
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.
Error: While importing 'app', an ImportError was raised:
Traceback (most recent call last):
File "C:\Users\rohit\PycharmProjects\PlacePredictor\venv\Lib\site-packages\flask\cli.py", line 218, in locate_app
import(module_name)
File "C:\Users\rohit\PycharmProjects\PlacePredictor\app.py", line 5, in <module>
model=pickle.load(open('model.pkl','rb'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'sklearn'
Process finished with exit code 2```
答案1
得分: 0
Add import statement for sklearn in the flask file. You are unpickling a sklearn model but the flask package does not know sklearn; therefore, you also have to add import sklearn to the import section.
英文:
Add import statement for sklearn in the flask file. You are unpickling a sklearn model but the flask package doesnot know sklearn therefore you also have to add import sklearn to the import section
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论