簡體   English   中英

數組中的最大值和百分位數

[英]Maximum value and percentile in array

我有以下數組:

array = [{"student" => 1, "cost" => 2, "university" => 2, "room" => 2},
         {"student" => 1, "cost" => 5, "university" => 2, "room" => 3},
         {"student" => 1, "cost" => 1, "university" => 3, "room" => 1},
         {"student" => 2, "cost" => 1, "university" => 1, "room" => 3},
         {"student" => 2, "cost" => 2, "university" => 2, "room" => 2},
         {"student" => 2, "cost" => 4, "university" => 1, "room" => 1}]

我想要一個僅具有最大值和"cost"的百分位數95的數組,即

array = [{"student" => 1, "cost_max" => 5, "university" => 2, "room" => 3, "cost_per95" => 4.7},
         {"student" => 2, "cost_max" => 4, "university" => 1, "room" => 1, "cost_per95"=> 3.9}

我已經應用了這個:

groupedmax  = array.group_by {|h| h["student"]}
keysmax = groupedmax.keys
arrmax = keysmax.map {|k| [k, groupedmax[k].max_by {|h| h["cost"]}]}
table_max = arrmax.map { |ts| ts[1] }
# => [{"student"=>1, "cost"=>5, "university"=>2, "room"=>3},
#   {"student"=>2, "cost"=>4, "university"=>1, "room"=>1}]

但我不知道如何將百分位數95添加到該數組。 一種計算百分位數的方法是:

def percentile(values, percentile)
  values_sorted = values.sort
  k = (percentile*(values_sorted.length-1)+1).floor - 1
  f = (percentile*(values_sorted.length-1)+1).modulo(1)

  return values_sorted[k] + (f * (values_sorted[k+1] - values_sorted[k]))
end

注意您的評論。

def doit(arr)
  arr.group_by { |h| h["student"] }.   
      map do |_,a|
        costs = a.map { |h| h['cost'] }
        imax = costs.each_index.max_by { |i| costs[i] }
        a[imax].merge('cost_per95'=>percentile(costs, 0.95).round(1))
      end
end

doit array
  #=> [{"student"=>1, "cost"=>5, "university"=>2, "room"=>3, "cost_per95"=>4.7},
  #    {"student"=>2, "cost"=>4, "university"=>1, "room"=>1, "cost_per95"=>3.9}]

array = [{"student" => 1, "cost" => 2, "university" => 2, "room" => 2},
         {"student" => 1, "cost" => 5, "university" => 2, "room" => 3},
         {"student" => 1, "cost" => 1, "university" => 3, "room" => 1},
         {"student" => 2, "cost" => 1, "university" => 1, "room" => 3},
         {"student" => 2, "cost" => 3, "university" => 2, "room" => 2}, 
         {"student" => 2, "cost" => 4, "university" => 1, "room" => 1}]
  #=> [{"student"=>1, "cost"=>5, "university"=>2, "room"=>3, "cost_per95"=>4.7},
  #    {"student"=>2, "cost"=>4, "university"=>1, "room"=>1, "cost_per95"=>3.9}]

您可以使用merge在該哈希中添加鍵。
嘗試這個,

table_max = arrmax.map { |ts| ts[1].merge("cost_per95": ts[1]["cost"] * 0.95) }

或者您也可以嘗試在一行中完成所有計算

array.group_by{ |x| x["student"] }.values.map{ |gp| gp.max_by{ |st| st["cost"] }.merge({ "cost_per95": "Your calculations" })}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM