Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data = File.read('data.csv').split("\n").map { |d| d.split(',').map(&:to_f) }
- iterations = 1000
- m = data.size
- alpha = 0.0001
- theta0 = 0
- theta1 = 0
- (1..iterations).each do |iter|
- d_theta0 = 0
- d_theta1 = 0
- (0..(m - 1)).each do |i|
- x = data[i][0]
- y = data[i][1]
- d_theta0 += (y - (theta1 * x + theta0))
- d_theta1 += (y - (theta1 * x + theta0)) * x
- end
- theta0 = theta0 + alpha * d_theta0
- theta1 = theta1 + alpha * d_theta1
- puts "#{iter}: f(x) = #{theta1} * x + #{theta0}"
- end
- puts "f(x) = #{theta1} * x + #{theta0}"
Advertisement
Add Comment
Please, Sign In to add comment