因此,模式是資料集中出現頻率最高的數字。
我已經設法解決它,但它只能獲得 1 種模式。這是我的代碼:
Dim mode = inputX.GroupBy(Function(n) n).Select(Function(g) New With {.Number = g.Key, .Quantity = g.Count}).OrderByDescending(Function(o) o.Quantity).FirstOrDefault
If mode.Quantity > 1 Then
result = mode.Number.ToString() " Quantity: " mode.Quantity.ToString()
Else
result = "None."
End If
現在,即使我輸入29 29 35 30 30了具有 2 種模式的 ,它也只顯示29,這是它獲得的第一個模式。我想獲得兩種或更多模式。
我一直在絞盡腦汁,不斷尋找答案,但我無法讓它發揮作用。
我兩天前才開始學習這種語言。
uj5u.com熱心網友回復:
從 LINQ 中的評論中參考我自己的話:Mean, Median, and Mode:
Dim inputX = {29, 29, 35, 30, 30}
Dim modes = From a In
(From n In inputX
Group n By n Into g = Count()
Select g, n)
Where a.g =
(From n In inputX
Group n By n Into g = Count() Select g).Max
Select a.n
Console.WriteLine(String.Join(", ", modes))
輸出:
29, 30
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/459315.html
標籤:VB.net
下一篇:更改不反映運行時間(代碼和設計)
