statsmodels.formula.api: 绘制统计模型导致 AttributeError
statsmodels.formula.api: plotting a statistical model causes AttributeError
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'
我错过了什么?
您正在使用 pandas.Series.reshape
。
它的 documentation 说它已被弃用,使用它会引发错误。
您应该将 out.reshape(x_surf.shape)
替换为 out.values.reshape(x_surf.shape)
。它应该可以解决您的错误。
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'
我错过了什么?
您正在使用 pandas.Series.reshape
。
它的 documentation 说它已被弃用,使用它会引发错误。
您应该将 out.reshape(x_surf.shape)
替换为 out.values.reshape(x_surf.shape)
。它应该可以解决您的错误。