Skip to content

Instantly share code, notes, and snippets.

@endenis
Last active August 29, 2015 14:23
Show Gist options
  • Save endenis/24f569879957be0b3b4f to your computer and use it in GitHub Desktop.
Save endenis/24f569879957be0b3b4f to your computer and use it in GitHub Desktop.
Polynomial regression in ruby
require 'matrix'
def polynomial_regression x, y, degree
rows = x.map do |i|
(0..degree).map { |power| (i ** power).to_f }
end
mx, my = Matrix.rows(rows), Matrix.columns([y])
((mx.transpose * mx).inv * mx.transpose * my).transpose.row(0).to_a.reverse
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment