Ruby的statiscts (Statsample)似乎给出了错误的答案。模式函数不应该返回数组中最常见的iten?例:4
irb(main):185:0> [1,2,3,2,4,4,4,4].to_vector
=>
#<Daru::Vector:34330120 @name = nil @size = 8 >
nil
0 1
1 2
2 3
3 2
4 4
5 4
6 4
7 4
irb(main):186:0> [1,2,3,2,4,4,4,4].to_vector.mode
=> 2为什么要返回2?
ruby 2.1.6p336 (2015-04-13 revision 50298) [x64-mingw32]
statsample (2.0.1)发布于 2016-03-01 00:54:01
是的你是对的维克多!向量库中有一个bug:
[1,2,3,2,4,4,4,4].to_vector
=>
#<Daru::Vector:70255750869440 @name = nil @size = 8 >
nil
0 1
1 2
2 3
3 2
4 4
5 4
6 4
7 4
[1,2,3,2,4,4,4,4].to_vector.frequencies
=> {1=>1, 2=>2, 3=>1, 4=>4}
[1,2,3,2,4,4,4,4].to_vector.frequencies.values
=> [1, 2, 1, 4] 然后从具有给定索引的基数组中获取最大值的索引并返回值(在本例中为第4位置->值2 )。它是由这个方法完成的
def mode
freqs = frequencies.values
@data[freqs.index(freqs.max)]
end解决办法
您可以使用以下方法来代替mode方法:
[1,2,3,2,4,4,4,4].to_vector.frequencies.max{|a,b| a[1]<=>b[1]}.first
=> 4 https://stackoverflow.com/questions/35710144
复制相似问题