Я пытаюсь напечатать с помощью matplotlib параболоид, который является функцией стоимости простой линейной регрессии. Проблема в том, что функция не выглядит параболоидной ... здесь линейная регрессия фиктивный параболоид здесь идеальная прямая линия - вес 2, смещение 0
def main():
#create database
n_samples = 40
x = np.linspace(0, 20, n_samples)
y = 2*x + 4*np.random.randn(n_samples)
#show
plt.scatter(x, y)
print_cost_func(x, y)
def cost_func(x: np.ndarray, y: np.ndarray, weight: int, bias: int) ->
float:
return np.sum((y - (weight*x + bias))**2) / (2*len(x))
def print_cost_func(x: np.ndarray, y: np.ndarray):
fig = plt.figure()
ax = fig.gca(projection='3d')
weight = np.arange(-50, 50, 0.25)
bias = np.arange(-50, 50, 0.25)
weight, bias = np.meshgrid(weight, bias)
Z = np.zeros((400, 400))
#i think the problem is here
for i in range(400):
for j in range(400):
Z[i][j] = cost_func(x, y, weight[i][j], bias[i][j])
# Plot the surface.
surf = ax.plot_surface(weight, bias, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.title('Cost function')
plt.xlabel('Weight')
plt.ylabel('Bias')
plt.show()