在3d中用Matplotlib绘制线性模型

iva*_*nmp 4 python statistics matplotlib linear-regression

我正在尝试创建适合数据集的线性模型的3d图.我能够在R中相对容易地做到这一点,但我真的很难在Python中做同样的事情.这是我在R中所做的:

3d图

这是我在Python中所做的:

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

csv = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/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')

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

xx, yy = np.meshgrid(csv['TV'], csv['Radio'])

# Not what I expected :(
# ax.plot_surface(xx, yy, fit.fittedvalues)

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

plt.show()
Run Code Online (Sandbox Code Playgroud)

我做错了什么,我该怎么做?

谢谢.

iva*_*nmp 7

得到它了!

我在对mdurant的答案的评论中谈到的问题是表面没有被绘制为像这些将散点图与表面图合并的漂亮方形图案.

我意识到问题是我的meshgrid,所以我纠正了两个范围(xy)并使用了比例步骤np.arange.

这允许我使用mdurant的答案提供的代码,它完美地工作!

这是结果:

与OLS平面的3d散点图

这是代码:

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('http://www-bcf.usc.edu/~gareth/ISL/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()
Run Code Online (Sandbox Code Playgroud)


mdu*_*ant 4

您假设plot_surface需要一个坐标网格来工作是正确的,但predict需要一个像您安装的数据结构(“exog”)一样的数据结构。

exog = pd.core.frame.DataFrame({'TV':xx.ravel(),'Radio':yy.ravel()})
out = fit.predict(exog=exog)
ax.plot_surface(xx, yy, out.reshape(xx.shape), color='None')
Run Code Online (Sandbox Code Playgroud)