statsmodels.formula.api:绘制统计模型会导致AttributeError

问题描述 投票:0回答:1

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个回答
0
投票

您正在使用pandas.Series.reshape

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

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

© www.soinside.com 2019 - 2024. All rights reserved.