【问题标题】:statsmodels.formula.api: plotting a statistical model causes AttributeErrorstatsmodels.formula.api:绘制统计模型会导致 AttributeError
【发布时间】:2020-03-05 00:29:22
【问题描述】:

statsmodels 是一个 Python 模块,它提供用于估计许多不同统计模型以及进行统计测试和统计数据探索的类和函数。每个估算器都有一个广泛的结果统计列表。对照现有的统计包对结果进行测试,以确保它们是正确的。

我正在尝试在我自己的笔记本电脑上重现此 example

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.formula.api as sm
from matplotlib import cm

csv = pd.read_csv('/afs/afs.sxl/python/3d/Advertising.csv', index_col=0)
model = sm.ols(formula='Sales ~ TV + Radio', data = csv)
fit = model.fit()

fit.summary()

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x_surf = np.arange(0, 350, 20)                # generate a mesh
y_surf = np.arange(0, 60, 4)
x_surf, y_surf = np.meshgrid(x_surf, y_surf)

exog = pd.core.frame.DataFrame({'TV': x_surf.ravel(), 'Radio': y_surf.ravel()})
out = fit.predict(exog = exog)
ax.plot_surface(x_surf, y_surf,
                out.reshape(x_surf.shape),
                rstride=1,
                cstride=1,
                color='None',
                alpha = 0.4)

ax.scatter(csv['TV'], csv['Radio'], csv['Sales'],
           c='blue',
           marker='o',
           alpha=1)

ax.set_xlabel('TV')
ax.set_ylabel('Radio')
ax.set_zlabel('Sales')

plt.show()

我收到了这个错误

Traceback (most recent call last):
  File "/3d/data_in_cube.py", line 24, in <module>
    out.reshape(x_surf.shape),
  File "/anaconda3/envs/lib/python3.6/site-packages/pandas/core/generic.py", line 5067, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'reshape'

我错过了什么?

【问题讨论】:

    标签: python statsmodels


    【解决方案1】:

    您正在使用pandas.Series.reshape。

    它的documentation 表示它已被弃用,使用它会引发错误。

    您应该将out.reshape(x_surf.shape) 替换为out.values.reshape(x_surf.shape)。它应该可以解决您的错误。

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 1970-01-01
      • 2021-01-04
      • 2020-03-16
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多