【数学建模之Python】9.AttributeError: module ‘pandas‘ has no attribute ‘Panel‘

你们的每个赞都能让我开心好几天✿✿ヽ(°▽°)ノ✿

其实这类报错仔细看看就能够明白为什么,如果自己写的程序是没问题的话,那报错就是库的问题简而言之,库的版本太旧了,有的函数、名词已经更新了,在库的互相使用的过程中就无法兼容,解决方法是升级statsmodels

版本声明:

python 3.7.1

numpy 1.21+mkl

statsmodels 0.12.2

解决方法:

在Anaconda Prompt里pip uninstall statsmodels再pip install statsmodels即可,会安装到匹配你的python版本

以下是我的使用的报错、代码

报错

D:\Anaconda\python.exe "C:/Users/86189/Desktop/数学建模课件/Python数学实验与建模-程序及数据/程序及数据/12第12章  回归分析/Pex12_2_1.py"
Traceback (most recent call last):
  File "C:/Users/86189/Desktop/数学建模课件/Python数学实验与建模-程序及数据/程序及数据/12第12章  回归分析/Pex12_2_1.py", line 2, in <module>
    import numpy as np; import statsmodels.api as sm
  File "D:\Anaconda\lib\site-packages\statsmodels\api.py", line 7, in <module>
    from .regression.recursive_ls import RecursiveLS
  File "D:\Anaconda\lib\site-packages\statsmodels\regression\recursive_ls.py", line 16, in <module>
    from statsmodels.tsa.statespace.mlemodel import (
  File "D:\Anaconda\lib\site-packages\statsmodels\tsa\statespace\mlemodel.py", line 21, in <module>
    import statsmodels.tsa.base.tsa_model as tsbase
  File "D:\Anaconda\lib\site-packages\statsmodels\tsa\base\tsa_model.py", line 2, in <module>
    from statsmodels.compat.pandas import is_numeric_dtype, Float64Index
  File "D:\Anaconda\lib\site-packages\statsmodels\compat\pandas.py", line 49, in <module>
    data_klasses = (pandas.Series, pandas.DataFrame, pandas.Panel)
  File "D:\Anaconda\lib\site-packages\pandas\__init__.py", line 244, in __getattr__
    raise AttributeError(f"module 'pandas' has no attribute '{name}'")
AttributeError: module 'pandas' has no attribute 'Panel'

Process finished with exit code 1

代码

import numpy as np
import statsmodels.api as sm

a=np.loadtxt('水泥热量与两种化学成分的观测值.txt')
d={'x1':a[:,0],'x2':a[:,1],'y':a[:,2]}

#1.基于公式求解
md=sm.formula.ols('y~x1+x2',d).fit()
print(md.summary(),'\n')
y_predict=md.predict({'x1':a[:,0],'x2':a[:,1]})
print(y_predict)#输出预测值

#2.基于数组求解
X=sm.add_constant(a[:,:2])
md=sm.OLS(a[:,2],X).fit()
print(md.params,'\n')
y=md.predict(X)
print(md.summary2())

修改之后的结果

D:\Anaconda\python.exe "C:/Users/86189/Desktop/technology/python执行文件/数学建模/回归分析/水泥 利用statsmodels库求解 例12.1.2.py"
D:\Anaconda\lib\site-packages\scipy\stats\stats.py:1542: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=13
  "anyway, n=%i" % int(n))
D:\Anaconda\lib\site-packages\scipy\stats\stats.py:1542: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=13
  "anyway, n=%i" % int(n))
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.979
Model:                            OLS   Adj. R-squared:                  0.974
Method:                 Least Squares   F-statistic:                     229.5
Date:                Sun, 01 Aug 2021   Prob (F-statistic):           4.41e-09
Time:                        21:01:23   Log-Likelihood:                -28.156
No. Observations:                  13   AIC:                             62.31
Df Residuals:                      10   BIC:                             64.01
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     52.5773      2.286     22.998      0.000      47.483      57.671
x1             1.4683      0.121     12.105      0.000       1.198       1.739
x2             0.6623      0.046     14.442      0.000       0.560       0.764
==============================================================================
Omnibus:                        1.509   Durbin-Watson:                   1.922
Prob(Omnibus):                  0.470   Jarque-Bera (JB):                1.104
Skew:                           0.503   Prob(JB):                        0.576
Kurtosis:                       1.987   Cond. No.                         175.
==============================================================================

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

0      80.074002
1      73.250919
2     105.814740
3      89.258477
4      97.292515
5     105.152489
6     104.002051
7      74.575420
8      91.275487
9     114.537543
10     80.535674
11    112.437244
12    112.293440
dtype: float64
[52.57734888  1.46830574  0.66225049] 

                 Results: Ordinary least squares
=================================================================
Model:              OLS              Adj. R-squared:     0.974   
Dependent Variable: y                AIC:                62.3124 
Date:               2021-08-01 21:01 BIC:                64.0072 
No. Observations:   13               Log-Likelihood:     -28.156 
Df Model:           2                F-statistic:        229.5   
Df Residuals:       10               Prob (F-statistic): 4.41e-09
R-squared:          0.979            Scale:              5.7904  
-------------------------------------------------------------------
           Coef.    Std.Err.      t      P>|t|     [0.025    0.975]
-------------------------------------------------------------------
const     52.5773     2.2862   22.9980   0.0000   47.4834   57.6713
x1         1.4683     0.1213   12.1047   0.0000    1.1980    1.7386
x2         0.6623     0.0459   14.4424   0.0000    0.5601    0.7644
-----------------------------------------------------------------
Omnibus:              1.509        Durbin-Watson:           1.922
Prob(Omnibus):        0.470        Jarque-Bera (JB):        1.104
Skew:                 0.503        Prob(JB):                0.576
Kurtosis:             1.987        Condition No.:           175  
=================================================================


Process finished with exit code 0

(那个usewarning无关紧要,不影响程序)

你们的每个赞都能让我开心好几天✿✿ヽ(°▽°)ノ✿

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若oo尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值