Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Supervised Learning | Linear Regression | Cost function

linear-regression/cost-function.livemd

Supervised Learning | Linear Regression | Cost function

Section

# (size in 1000 square feet)
x_train = [1.0, 2.0]
# (price in 1000s of dollars)
y_train = [300.0, 500.0]
defmodule LinearRegression do
  def compute_cost(x, y, w, b) do
    m = Enum.count(x)

    cost =
      x
      |> Enum.with_index()
      |> Enum.map(fn {i, index} -> (w * i + b - Enum.at(y, index)) ** 2 end)
      |> Enum.sum()

    total_cost = 1 / (2 * m) * cost
    total_cost
  end
end
LinearRegression.compute_cost(x_train, y_train, 200, 50)