Skip to content

Instantly share code, notes, and snippets.

@jinhucheung
Forked from Serabe/gist:990667
Created April 10, 2022 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinhucheung/f2e6de3bcb8347c03cdb1b01204a3e43 to your computer and use it in GitHub Desktop.
Save jinhucheung/f2e6de3bcb8347c03cdb1b01204a3e43 to your computer and use it in GitHub Desktop.
require 'matrix'
def regression x, y, degree
x_data = x.map {|xi| (0..degree).map{|pow| (xi**pow) }}
mx = Matrix[*x_data]
my = Matrix.column_vector y
((mx.t * mx).inv * mx.t * my).transpose.to_a[0].reverse
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment