Plot lineares Modell in 3d mit Matplotlib
Ich versuche, ein 3D-Diagramm eines linearen Modells zu erstellen, das für einen Datensatz geeignet ist. In R war dies relativ einfach, aber in Python habe ich große Mühe, dasselbe zu tun. Hier ist, was ich in R gemacht habe:
Hier ist was ich in Python gemacht habe:
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()
Was mache ich falsch und was soll ich stattdessen tun?
Vielen Dank