Jupyter NB 无法导入在 Ubuntu cmd 行上运行良好的软件包

Jupyter NB 无法导入在 Ubuntu cmd 行上运行良好的软件包

[我最近开始使用 Linux(Ubuntu),以便能够顺利运行 SageMath - 即不知道!]

Ubuntu 中的 Jupyter 笔记本允许同时运行 SageMath、Rstats 和 Python,并且运行完美。目前我在 WSL 上使用 Ubuntu,并运行 python3。我曾尝试实施此网站和其他网站上多个类似问题中的建议,但没有成功。

当我使用命令行时,我可以毫无问题地导入 pandas 和 statsmodels 并使用它们(见下面的示例)。

但是,如果我尝试从 Jupyter NB 运行相同的代码块,我会收到以下消息:

import statsmodels.api as sm              

import statsmodels.stats.stattools as stools

import statsmodels.stats as stats

from   statsmodels.graphics.regressionplots import *

from   statsmodels.sandbox.regression.predstd import wls_prediction_std

from   statsmodels.formula.api import ols

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/tmp/ipykernel_1044/2072660261.py in <module>
----> 1 import statsmodels.api as sm
      2 import statsmodels.stats.stattools as stools
      3 import statsmodels.stats as stats
      4 from   statsmodels.graphics.regressionplots import *
      5 from   statsmodels.sandbox.regression.predstd import wls_prediction_std

ModuleNotFoundError: No module named 'statsmodels'

我尝试重新启动电脑。为什么 Jupyter NB 无法识别 statsmodels,我该如何解决这个问题?


示例(来自 Ubuntu 的命令行):

user@antoni:~$ python3
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
t pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
import statsmodels.stats.stattools as stools
import statsmodels.stats as stats
from   statsmodels.graphics.regressionplots import *
from   statsmodels.sandbox.regression.predstd import wls_prediction_std
from   statsmodels.formula.api import ols
import io
import requests

url = "https://raw.githubusercontent.com/RInterested/datasets/gh-pages/mtcars.csv"
contents = requests.get(url).content
mtcars = pd.read_csv(io.StringIO(contents.decode('utf-8')))
mtcars['wt_square']=mtcars['wt']**2
model = ols('mpg ~ wt + wt_square', data=mtcars).fit()
print(model.summary())>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> import statsmodels.api as sm
>>> import statsmodels.stats.stattools as stools
>>> import statsmodels.stats as stats
>>> from   statsmodels.graphics.regressionplots import *
>>> from   statsmodels.sandbox.regression.predstd import wls_prediction_std
>>> from   statsmodels.formula.api import ols
>>> import io
>>> import requests
>>>
>>> url = "https://raw.githubusercontent.com/RInterested/datasets/gh-pages/mtcars.csv"
>>> contents = requests.get(url).content
>>> mtcars = pd.read_csv(io.StringIO(contents.decode('utf-8')))
>>> mtcars['wt_square']=mtcars['wt']**2
>>> model = ols('mpg ~ wt + wt_square', data=mtcars).fit()
>>> print(model.summary())
                            OLS Regression Results
==============================================================================
Dep. Variable:                    mpg   R-squared:                       0.819
Model:                            OLS   Adj. R-squared:                  0.807
Method:                 Least Squares   F-statistic:                     65.64
Date:                Sat, 20 May 2023   Prob (F-statistic):           1.71e-11
Time:                        13:46:59   Log-Likelihood:                -75.024
No. Observations:                  32   AIC:                             156.0
Df Residuals:                      29   BIC:                             160.4
Df Model:                           2
Covariance Type:            nonrobust
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     49.9308      4.211     11.856      0.000      41.318      58.544
wt           -13.3803      2.514     -5.322      0.000     -18.522      -8.239
wt_square      1.1711      0.359      3.258      0.003       0.436       1.906
==============================================================================
Omnibus:                        4.261   Durbin-Watson:                   1.732
Prob(Omnibus):                  0.119   Jarque-Bera (JB):                3.788
Skew:                           0.832   Prob(JB):                        0.150
Kurtosis:                       2.731   Cond. No.                         142.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

相关内容