1

現在、Ruby で ROC 曲線の計算を実装しようとしています。http://people.inf.elte.hu/kiss/13dwhdm/roc.pdf (6 サイト、第 5 章、アルゴリズム 1「ROC ポイントを効率的に生成する方法」を参照)の疑似コードを Ruby コードに変換してみました。

1.0簡単な例を作成しましたが、リコールのために常に値を取得しています。何かを誤解したか、プログラミングを間違えたと思います。これが私がこれまでに行ったことです:

# results from a classifier
# index 0: users voting
# index 1: estimate from the system
results = [[5.0,4.8],[4.6,4.2],[4.3,2.2],[3.1,4.9],[1.3,2.6],[3.9,4.3],[1.9,2.4],[2.6,2.3]]
# over a score of 2.5 an item is a positive one
threshold = 2.5
# sort by index 1, the estimate
l_sorted = results.sort { |a,b| b[1] <=> a[1] }

# count the real positives and negatives
positives, negatives = 0, 0
positives, negatives = 0, 0
l_sorted.each do |item|
  if item[0] >= threshold
    positives += 1
  else
    negatives += 1
  end
end

fp, tp = 0, 0
# the array that holds the points
r = []
f_prev = -Float::INFINITY

# iterate over all items
l_sorted.each do |item|
  # if the score of the former iteration is different,
  # add another point to r
  if item[1]!=f_prev
    r.push [fp/negatives.to_f,tp/positives.to_f]
    f_prev = item[1]
  end
  # if the current item is a real positive
  # (user likes the item indeed, and estimater was also correct)
  # add a true positive, otherwise, add a false positve
  if item[0] >= threshold && item[1] >= threshold
    tp += 1
  else
    fp += 1
  end
end

# push the last point (1,1) to the array
r.push [fp/negatives.to_f,tp/positives.to_f]

r.each do |point|
  puts "(#{point[0].round(3)},#{point[1].round(3)})"
end

results配列の配列に基づいて、コードはポイントを計算しようとします。私はそれが何であるか分かりませんf_prevf_prev格納されている分類器のスコアにあるか、またはそれがまたはtrueの場合のみ?false

誰かが私のコードをざっと見て、間違いを見つけるのを手伝ってくれたら最高です。どうも!

4

2 に答える 2