简体   繁体   English

ModuleNotFoundError:没有名为“sklearn.linear_model.base”的模块

[英]ModuleNotFoundError: No module named 'sklearn.linear_model.base'

在此处输入图像描述

Tried to deploy a ML model in AWS and getting this error on the same.试图在 AWS 中部署 ML model 并在同一台机器上收到此错误。 Have updated scikit-learn library but still having this issue.已更新 scikit-learn 库,但仍有此问题。

I had the exact same problem when moving to a new Python environment.迁移到新的 Python 环境时,我遇到了完全相同的问题。 I couldn't easily bring my conda env with me to a new OS, so had to hack it together.我不能轻易地将我的 conda env 带到一个新的操作系统上,所以不得不把它一起破解。 If you can bring your python environment with you, that should be your first bet--see the conda page: Managing Conda Environments .如果您可以随身携带 python 环境,那应该是您的第一个赌注——请参阅 conda 页面: 管理 Conda 环境

The problem is that namespace linear_model.base of sklearn appears to have been removed somewhere between the build where I built the model and the latest build--so if you want your model built with an older version to work, you'll need to create (or recreate) an environment that has the same version of scikit-learn.问题是 sklearn 的命名空间linear_model.base似乎已在我构建 model 的构建和最新构建之间的某个位置被删除 - 所以如果你希望你的 model 使用旧版本构建,你需要创建(或重新创建)具有相同版本 scikit-learn 的环境。 Updating the module becomes the problem, not the solution .更新模块成为问题,而不是解决方案 For me, scikit-learn 0.19.1 was what I used to build the model, so I needed to revert to that, which required me to build a new environment based on python 3.6.对我来说,scikit-learn 0.19.1 是我用来构建 model 的,所以我需要恢复到那个,这需要我构建一个基于 python 3.6 的新环境。 Not ideal... but it worked.不理想……但它奏效了。

Make sure when you create the new environment, you install the specific version of scikit-learn first , so all subsequent dependencies will have to be compatible.确保在创建新环境时,首先安装特定版本的 scikit-learn ,因此所有后续依赖项都必须兼容。 If you need a specific version of Python, you will create a new environment by:如果您需要特定版本的 Python,您将通过以下方式创建新环境:

conda create --name youNameHere python=X.Y

and then ensure you have the right version of scikit-learn with something like this:然后确保您拥有正确版本的 scikit-learn,如下所示:

conda install scikit-learn=A.BC.D

where "A.BC.D" is your version number.其中“A.BC.D”是您的版本号。

I'm not sure how much help that is--you may want to install conda on the AWS instance if you have the ability to do so.我不确定这有多大帮助——如果你有能力的话,你可能想在 AWS 实例上安装 conda。 Here's a good resource on how to do it from the command line if you don't want to just move to another AMI that already has it loaded: https://dev.to/waylonwalker/installing-miniconda-on-linux-from-the-command-line-4ad7如果您不想移动到另一个已经加载它的 AMI,这里有一个很好的资源,关于如何从命令行执行它: https://dev.to/waylonwalker/installing-miniconda-on-linux-from -the-command-line-4ad7

I had similar, but fixed as below:我有类似的,但固定如下:

No longer getting "No module named 'sklearn.linear_model.base'" I am using Python 3.9不再收到“没有名为 'sklearn.linear_model.base' 的模块”我正在使用 Python 3.9

You need to edit the following / replace the 2 lines commented out with with these 2 lines and it works - original code left in below for completeness.您需要编辑以下内容/用这 2 行替换注释掉的 2 行,并且它可以工作 - 为了完整起见,原始代码留在下面。

from sklearn.linear_model import LinearRegression                               
regressor = LinearRegression()                                                  
                                                                              
#from sklearn.tree import DecisionTreeRegression                                
#regressor = DecisionTreeRegression()

Cause:You have files from a different source and now you have them in a new environment Just do this:Go to your.py file that has the scikit library and note the version Go go your directory and pip install scikit-learn==x.xx.x where x.xx.x is the exact number existing in the.py file See a real example I had when testing some app from github Below is a real example in cmd原因:您有来自不同来源的文件,现在您将它们放在新环境中只需执行以下操作:Go 到具有 scikit 库的 .py 文件并注意版本 Go go 您的目录和 pip 安装 scikit-learn==x .xx.x 其中 x.xx.x 是 .py 文件中存在的确切数字 请参阅我在测试 github 中的某些应用程序时遇到的真实示例 下面是 cmd 中的真实示例

C:\todel>python app.py
Traceback (most recent call last):
  File "app.py", line 6, in <module>
    model = pickle.load(open('model.pkl', 'rb'))
ModuleNotFoundError: No module named 'sklearn.linear_model.base'

C:\todel>pip install scikit-learn=0.23.2
ERROR: Invalid requirement: 'scikit-learn=0.23.2'
Hint: = is not a valid operator. Did you mean == ?
WARNING: You are using pip version 20.1.1; however, version 22.0.3 is available.
You should consider upgrading via the 'c:\pyapp\python.exe -m pip install --upgrade pip' command.

C:\todel>pip install scikit-learn==0.23.2
Collecting scikit-learn==0.23.2
  Downloading scikit_learn-0.23.2-cp37-cp37m-win_amd64.whl (6.8 MB)
     |████████████████████████████████| 6.8 MB 504 kB/s
Requirement already satisfied: joblib>=0.11 in c:\pyapp\lib\site-packages (from scikit-learn==0.23.2) (1.1.0)
Requirement already satisfied: scipy>=0.19.1 in c:\pyapp\lib\site-packages (from scikit-learn==0.23.2) (1.4.1)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\pyapp\lib\site-packages (from scikit-learn==0.23.2) (3.0.0)
Requirement already satisfied: numpy>=1.13.3 in c:\pyapp\lib\site-packages (from scikit-learn==0.23.2) (1.19.2)
Installing collected packages: scikit-learn
  Attempting uninstall: scikit-learn
    Found existing installation: scikit-learn 1.0.2
    Uninstalling scikit-learn-1.0.2:
      Successfully uninstalled scikit-learn-1.0.2
Successfully installed scikit-learn-0.23.2
WARNING: You are using pip version 20.1.1; however, version 22.0.3 is available.
You should consider upgrading via the 'c:\pyapp\python.exe -m pip install --upgrade pip' command.

C:\todel>python app.py
C:\pyapp\lib\site-packages\sklearn\utils\deprecation.py:143: FutureWarning: The sklearn.linear_model.base module is  deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.linear_model. Anything that cannot be imported from sklearn.linear_model is now part of the private API.
  warnings.warn(message, FutureWarning)
C:\pyapp\lib\site-packages\sklearn\base.py:334: UserWarning: Trying to unpickle estimator LinearRegression from version 0.20.2 when using version 0.23.2. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
C:\pyapp\lib\site-packages\sklearn\utils\deprecation.py:143: FutureWarning: The sklearn.linear_model.base module is  deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.linear_model. Anything that cannot be imported from sklearn.linear_model is now part of the private API.
  warnings.warn(message, FutureWarning)
C:\pyapp\lib\site-packages\sklearn\base.py:334: UserWarning: Trying to unpickle estimator LinearRegression from version 0.20.2 when using version 0.23.2. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
 * Debugger is active!
 * Debugger PIN: 137-011-919
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有名为“sklearn.linear_model.base”的模块 - No module named 'sklearn.linear_model.base' ModuleNotFoundError:没有名为“sklearn.linear_model._base”的模块 - ModuleNotFoundError: No module named 'sklearn.linear_model._base' modulenotfounderror:在 python flask 上没有名为“sklearn.linear_model.logistic”的模块 - modulenotfounderror: no module named 'sklearn.linear_model.logistic' on python flask ModuleNotFoundError:没有名为“sklearn.linear_model._logistic”的模块 - ModuleNotFoundError: No module named 'sklearn.linear_model._logistic' 出现错误:ModuleNotFoundError: No module named 'sklearn.linear_model._logistic' in Heroku - Getting an error : ModuleNotFoundError: No module named 'sklearn.linear_model._logistic' in Heroku modulenotfounderror:没有名为“ sklearn”的模块 - modulenotfounderror : no module named 'sklearn' ModuleNotFoundError:没有名为“sklearn”的模块 - ModuleNotFoundError: No module named 'sklearn' ModuleNotFoundError: 没有名为“sklearn.model_selection”的模块; &#39;sklearn&#39; 不是一个包 - ModuleNotFoundError: No module named 'sklearn.model_selection'; 'sklearn' is not a package ModuleNotFoundError:没有名为“sklearn”的模块 python - ModuleNotFoundError: No module named 'sklearn' python ModuleNotFoundError:没有名为“模型”的模块 - ModuleNotFoundError: No module named 'model'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM