Guest User

Untitled

a guest
Sep 19th, 2015
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.55 KB | None | 0 0
  1. data = File.read('data.csv').split("\n").map { |d| d.split(',').map(&:to_f) }
  2. iterations = 1000
  3. m = data.size
  4. alpha = 0.0001
  5. theta0 = 0
  6. theta1 = 0
  7.  
  8. (1..iterations).each do |iter|
  9.   d_theta0 = 0
  10.   d_theta1 = 0
  11.   (0..(m - 1)).each do |i|
  12.     x = data[i][0]
  13.     y = data[i][1]
  14.     d_theta0 += (y - (theta1 * x + theta0))
  15.     d_theta1 += (y - (theta1 * x + theta0)) * x
  16.   end
  17.   theta0 = theta0 + alpha * d_theta0
  18.   theta1 = theta1 + alpha * d_theta1
  19.   puts "#{iter}: f(x) = #{theta1} * x + #{theta0}"
  20. end
  21.  
  22. puts "f(x) = #{theta1} * x + #{theta0}"
Advertisement
Add Comment
Please, Sign In to add comment