python - 使用 matplotlib 将数据绘制为 3D 线性模型
问题描述
我想在下面的链接中绘制一些数据。
当我在 OLS 公式中有多个“电视和广播”并且我只想用“销售”来绘制这两个时,我该怎么办?因为如果我喜欢下面的代码(链接),它会向我显示其他未定义的错误(电视和广播除外)..
感谢帮助!
https://stackoverflow.com/a/26434204/14208684
这是链接的代码:
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()
解决方案
推荐阅读
- java - 使用 FTDI ft311 开发板进行编程
- node.js - 我需要在我的 NodeJS 服务器上运行 MATLAB 代码
- ubuntu - H2O 的 Driverless AI .deb 包安装失败
- spring - 如何在 Spring MongoDB 中将 2 个整数值连接成一个字符串
- php - Laravel - 覆盖模型 ID
- c++ - 用犰狳对向量进行归一化
- c# - 重新启动我的代码的一部分?SharpPCap 的问题
- reactjs - 在 Redux 中存储简单状态更改的位置
- asp.net-core-2.0 - .Net Reactor 和 ASP.NET Core
- iis - ASP.NET Core 2.0 - IIS 的 HTTP 错误 502.5 进程失败