scikit-learn 没有在 jupyter notebook 中导入

Posted

技术标签:

【中文标题】scikit-learn 没有在 jupyter notebook 中导入【英文标题】:scikit-learn not importing in jupyter notebook 【发布时间】:2020-03-15 17:19:32 【问题描述】:

我使用 pip 和 conda 命令安装了 scikit-learn。每当我检查包时,它显示它已经安装,但每当我尝试导入它时,它就会显示错误。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
%matplotlib inline

错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-33-82d3fc6531ea> in <module>
      3 import matplotlib.pyplot as plt
      4 import seaborn as sns
----> 5 from sklearn.model_selection import train_test_split
      6 get_ipython().run_line_magic('matplotlib', 'inline')

ModuleNotFoundError: No module named 'sklearn'

我已经尝试过多次重新安装它,但它显示相同的错误

【问题讨论】:

pip3 install --upgrade scikit-learn 发布您的 scikit-learn 版本。 你在使用 virtualenv 吗? 我刚刚发现scikit没有安装pip3。每当我尝试安装它时,安装程​​序都会显示错误并以退出状态 1 退出 ModuleNotFoundError: No module named 'sklearn'的可能重复 这里有一些提示和建议:***.com/questions/46113732/… 【参考方案1】:

我在安装这些时遇到了同样的错误。 你可以做的是使用命令安装所有的包

conda install pandas 

和所有其他包类似或全部在一次

conda install pandas numpy matplotlib scikit-learn

在您的项目目录中。

现在你可以使用命令打开你的 jupyter 笔记本了

jupyter notebook

在同一个环境或者我应该说同一个项目目录。

【讨论】:

【参考方案2】:

我认为您没有最新版本的 sklearn,因为在旧版本的 sklearn 下面的代码不起作用。

from sklearn.model_selection import train_test_split

请通过以下命令更新您的 sklearn

conda update scikit-learn

【讨论】:

【参考方案3】:

确保如果您在您正在使用的 python 版本中安装 sklearn 包。假设如果您的系统中安装了两个版本的 python,比如 python 2 和 python 3,那么您已经在 python 2 中安装了 sklearn 包并在 python 3 中执行您的代码,反之亦然。

【讨论】:

以上是关于scikit-learn 没有在 jupyter notebook 中导入的主要内容,如果未能解决你的问题,请参考以下文章

为啥jupyter notebook代码运行一直没有反应?

Ipywidgets (Vbox) 没有出现在 Jupyter 笔记本上

Jupyter 错误:“没有名为 jupyter_core.paths 的模块”

为啥jupyter notebook代码运行一直没有反应

为啥jupyter notebook代码运行一直没有反应

没有Jupyter笔记本的Python ggplot(ggpy)?