-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie_test.rb
53 lines (43 loc) · 1.32 KB
/
movie_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# movie_test.rb
# Written by: Noranda Brown
# Version: 2014.1.26
class MovieTest
def initialize
@test_results = []
end
##
# returns the average predication error of the test results or 0.0 if < 2 tuples in test results
def mean
return 0.0 if @test_results.size < 2
@test_results.inject(0) { |sum_errors, tuple| sum_errors + (tuple[2] - tuple[3]).abs }.to_f / @test_results.size.to_f
end
##
# returns the standard deviation of the error or 0.0 if errors array size < 2
def stddev
return 0.0 if errors.size < 2
errors.stdev.to_f.round(1)
end
##
# returns the root mean square error of the prediction or 0.0 if errors array size < 2
def rms
return 0.0 if errors.size < 2
Math.sqrt(errors.inject(0) { |sum, error| sum + error * error }.to_f / errors.size.to_f ).round(1)
end
##
# returns an array of the predictions in the form [u,m,r,p]
def to_a
@test_results
end
##
# adds the given result tuple to the test results
def add_result(result_tuple)
@test_results << result_tuple
end
alias_method :<<, :add_result
private #######################################################################
##
# returns an array of error differences between predicted ratings and actual ratings
def errors
@test_results.map { |tuple| (tuple[2] - tuple[3]).abs }
end
end