2014-10-17 87 views
2

我正在尝试创建适合数据集的线性模型的3D图。我可以在R中相对容易地做到这一点,但我真的很努力地在Python中做同样的事情。使用Matplotlib在3D中绘制线性模型

3d plot

这里就是我在Python进行:以下是我在R来

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() 

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

谢谢。

回答

3

假设plot_surface想要一个坐标meshgrid可以使用,但是预测需要一个数据结构(如适配的数据结构)(“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') 
+1

这类作品,但由此产生的平面非常糟糕绘制。我感到困惑的是,似乎没有更好更简单的方法来做到这一点。 – ivanmp 2014-10-17 21:14:53

+0

不好怎么样?有许多参数可以提供给plot_surface。如果你想要3D,但你可能想看看mayavi。 – mdurant 2014-10-17 21:25:31

+0

这是http://imgur.com/A5w55U6是我得到的(我删除了'color ='None')。我正在寻找这样的东西:http://stackoverflow.com/questions/15229896/matplotlib-3d-plots-combining-scatter-plot-with-surface-plot。顺便说一句,感谢您的帮助。 – ivanmp 2014-10-17 21:29:09

5

Got it!

我在mdurant回答的评论中谈到的问题是,表面不会被绘制为像这些Combining scatter plot with surface plot这样的漂亮方形图案。

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

这让我可以使用mdurant的答案所提供的代码,它完美地工作!

这里的结果:

3d scatter plot with OLS plane

而这里的代码:

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()