Skip to content

Instantly share code, notes, and snippets.

@oem
Created April 15, 2018 11:00
Show Gist options
  • Save oem/a201b3651057981e069bb5db43b7251b to your computer and use it in GitHub Desktop.
Save oem/a201b3651057981e069bb5db43b7251b to your computer and use it in GitHub Desktop.
cost function for linear regression
# features is a Matrix, labels and theta are Vectors
def cost_function(features, labels, theta)
m = features.row_count
predictions = predict(features, theta)
squared_errors = (predictions - labels).map { |error| error**2 }
(1.0 / (2.0 * m)) * squared_errors.reduce { |a, b| a + b }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment