为什么这不起作用,我已经安装了sklearn。当我尝试执行它时,它显示错误。

huangapple go评论71阅读模式
英文:

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&#39;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&#39;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({&#39;placement&#39;: 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

huangapple
  • 本文由 发表于 2023年3月4日 00:52:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定