44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/leave-one-out-validation.rb', line 44
def statistics
accept_values = Feature.find(model.prediction_feature_id).accept_values
confusion_matrix = Array.new(accept_values.size,0){Array.new(accept_values.size,0)}
weighted_confusion_matrix = Array.new(accept_values.size,0){Array.new(accept_values.size,0)}
predictions.each do |pred|
pred[:database_activities].each do |db_act|
if pred[:value]
if pred[:value] == db_act
if pred[:value] == accept_values[0]
confusion_matrix[0][0] += 1
weighted_confusion_matrix[0][0] += pred[:confidence]
elsif pred[:value] == accept_values[1]
confusion_matrix[1][1] += 1
weighted_confusion_matrix[1][1] += pred[:confidence]
end
else
if pred[:value] == accept_values[0]
confusion_matrix[0][1] += 1
weighted_confusion_matrix[0][1] += pred[:confidence]
elsif pred[:value] == accept_values[1]
confusion_matrix[1][0] += 1
weighted_confusion_matrix[1][0] += pred[:confidence]
end
end
end
end
end
accept_values.each_with_index do |v,i|
true_rate[v] = confusion_matrix[i][i]/confusion_matrix[i].reduce(:+).to_f
predictivity[v] = confusion_matrix[i][i]/confusion_matrix.collect{|n| n[i]}.reduce(:+).to_f
end
confidence_sum = 0
weighted_confusion_matrix.each do |r|
r.each do |c|
confidence_sum += c
end
end
update_attributes(
accept_values: accept_values,
confusion_matrix: confusion_matrix,
weighted_confusion_matrix: weighted_confusion_matrix,
accuracy: (confusion_matrix[0][0]+confusion_matrix[1][1])/(nr_instances-nr_unpredicted).to_f,
weighted_accuracy: (weighted_confusion_matrix[0][0]+weighted_confusion_matrix[1][1])/confidence_sum.to_f,
true_rate: true_rate,
predictivity: predictivity,
finished_at: Time.now
)
$logger.debug "Accuracy #{accuracy}"
end
|