I am trying to create a surface plot of the error function in linear regression. I do it like this:
class LinearRegression:
def __init__(self):
self.data = pd.read_csv("data.csv")
def computeCost(self):
j = 0.5 * (
(self.data.hypothesis - self.data.y)**2).sum() / self.data.y.size
return j
def regress(self, theta0, theta1):
self.data["hypothesis"] = theta1 * self.data.x + theta0
def plotCostFunction3D(self):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
theta0_vals = np.linspace(-100, 100, 200)
theta1_vals = np.linspace(-100, 100, 200)
costs = []
for theta0 in theta0_vals:
for theta1 in theta1_vals:
self.regress(theta0, theta1)
costs.append(self.computeCost())
ax.plot_surface(
theta0_vals,
theta1_vals,
np.array(costs),
)
if __name__ == "__main__":
regression = LinearRegression()
regression.plotCostFunction3D()
plt.show()
I get the following error:
ValueError: Argument Z must be 2-dimensional.
I am aware that I need to use np.meshgrid for theta0_vals and theta1_vals, but I'm not sure how to compute the costs from those results. How would I go about it?