F# - 根据百分位数比较用标签替换浮点数
F# - Replace Float With Label Based On Percentile Comparison
我需要根据与 prcntls[ 中的百分位数的比较,将 tmp 中的值映射到 tmpRplcd 中的标签=19=],但是 Array.map2 行失败,因为数组的长度不同。
module SOQN =
open System
open MathNet.Numerics.Statistics
let tmp = [| 13.0; 17.0; 23.0; 11.0; 11.0; 13.0; 31.0;
19.0; 47.0; 29.0; 29.0; 19.0; 43.0; 37.0 |]
let tmpPrcntls =
tmp
|> Array.sort
let lbls = [| "p0"; "p1"; "p2"; "p3"; "p4";"p5" |]
let prcntls = [| Statistics.Percentile(tmpPrcntls,0) // 11.0
Statistics.Percentile(tmpPrcntls,20) // 13.0
Statistics.Percentile(tmpPrcntls,40) // 19.0
Statistics.Percentile(tmpPrcntls,60) // 28.6
Statistics.Percentile(tmpPrcntls,80) // 35.8
Statistics.Percentile(tmpPrcntls,100) // 47.0
|]
let lkpTbl = Map(Array.zip prcntls lbls)
let tmpRplcd:string[] =
tmp
|> Array.map2 (fun x y -> if x <= y then lkpTbl.[y] else "") prcntls
let main =
printfn ""
printfn "Percentile Test"
printfn ""
printfn "tmpPrcntls: %A" tmpPrcntls
printfn "prcntls:%A" prcntls
printfn "tmpRplcd:%A" tmpRplcd
0
[<EntryPoint>]
main
|> ignore
// Expected Result:
// tmpRplcd = [| "p1"; "p2"; "p3"; "p0"; "p0"; "p1"; "p4";
// "p2"; "p5"; "p4"; "p4"; "p2"; "p5"; "p5" |]
我哪里错了?
我认为您对 map2
的使用是错误的 - map2
函数压缩了两个数组,然后将给定函数应用于压缩后的数组。
根据你的问题,我的猜测是你实际上想做其他事情。对于每个输入,您想要遍历所有百分位数并找到第一个百分位数,使得该值大于(或小于?)百分位数。为此,您需要将 map2
替换为如下内容:
let tmpRplcd:string[] =
tmp
|> Array.map (fun y ->
prcntls |> Array.tryPick (fun x ->
if x <= y then Some(lkpTbl.[x]) else None))
|> Array.map (fun v -> defaultArg v "")
我没有合适的版本来尝试这个,但我认为这应该可以满足你的需要(我只是不确定你是否需要 x <= y
或相反!)
下面是您打算对 F# 程序执行的操作的方法。
我想出了 http://www.dummies.com/education/math/statistics/how-to-calculate-percentiles-in-statistics/ 的百分位计算的实现,如下面的统计模块所示。
namespace FSharpBasics
module Statistics =
let percentile p (array: float[]) =
let threshold = (float p / 100.0) * float (array |> Array.length)
let thresholdCeiling = int (System.Math.Ceiling threshold)
let thresholdInteger = int (threshold)
array
|> Array.sort
|> Array.skip (thresholdCeiling - 1)
|> Array.truncate (if thresholdInteger = thresholdCeiling then 2 else 1)
|> Array.average
module PercentileTest =
open System
let tmp = [| 13.0; 17.0; 23.0; 11.0; 11.0; 13.0; 31.0;
19.0; 47.0; 29.0; 29.0; 19.0; 43.0; 37.0 |]
let lbls =
[| for n in 0..20..100 -> "p" + string (n / 20) |]
let prcntls =
[| for n in 0..20..100 -> Statistics.percentile n tmp |]
let tmpPrcntls =
tmp |> Array.sort
let lkpTbl =
Array.zip prcntls lbls
let tmpRplcd : string[] =
tmp
|> Array.map (fun x ->
lkpTbl
|> Array.filter (fun (prcntl, lbl) -> prcntl <= x)
|> Array.last
|> snd)
[<EntryPoint>]
let main argv =
printfn ""
printfn "Percentile Test"
printfn ""
printfn "tmp: %A" tmp
printfn "tmpPrcntls: %A" tmpPrcntls
printfn "prcntls: %A" prcntls
printfn "tmpRplcd: %A" tmpRplcd
System.Console.ReadKey() |> ignore
0 // return an integer exit code
(*---- output ----
Percentile Test
tmp: [|13.0; 17.0; 23.0; 11.0; 11.0; 13.0; 31.0; 19.0; 47.0; 29.0; 29.0; 19.0; 43.0;
37.0|]
tmpPrcntls: [|11.0; 11.0; 13.0; 13.0; 17.0; 19.0; 19.0; 23.0; 29.0; 29.0; 31.0; 37.0; 43.0;
47.0|]
prcntls: [|11.0; 13.0; 19.0; 29.0; 37.0; 47.0|]
tmpRplcd: [|"p1"; "p1"; "p2"; "p0"; "p0"; "p1"; "p3"; "p2"; "p5"; "p3"; "p3"; "p2"; "p4";
"p4"|]
---- ----*)
我需要根据与 prcntls[ 中的百分位数的比较,将 tmp 中的值映射到 tmpRplcd 中的标签=19=],但是 Array.map2 行失败,因为数组的长度不同。
module SOQN =
open System
open MathNet.Numerics.Statistics
let tmp = [| 13.0; 17.0; 23.0; 11.0; 11.0; 13.0; 31.0;
19.0; 47.0; 29.0; 29.0; 19.0; 43.0; 37.0 |]
let tmpPrcntls =
tmp
|> Array.sort
let lbls = [| "p0"; "p1"; "p2"; "p3"; "p4";"p5" |]
let prcntls = [| Statistics.Percentile(tmpPrcntls,0) // 11.0
Statistics.Percentile(tmpPrcntls,20) // 13.0
Statistics.Percentile(tmpPrcntls,40) // 19.0
Statistics.Percentile(tmpPrcntls,60) // 28.6
Statistics.Percentile(tmpPrcntls,80) // 35.8
Statistics.Percentile(tmpPrcntls,100) // 47.0
|]
let lkpTbl = Map(Array.zip prcntls lbls)
let tmpRplcd:string[] =
tmp
|> Array.map2 (fun x y -> if x <= y then lkpTbl.[y] else "") prcntls
let main =
printfn ""
printfn "Percentile Test"
printfn ""
printfn "tmpPrcntls: %A" tmpPrcntls
printfn "prcntls:%A" prcntls
printfn "tmpRplcd:%A" tmpRplcd
0
[<EntryPoint>]
main
|> ignore
// Expected Result:
// tmpRplcd = [| "p1"; "p2"; "p3"; "p0"; "p0"; "p1"; "p4";
// "p2"; "p5"; "p4"; "p4"; "p2"; "p5"; "p5" |]
我哪里错了?
我认为您对 map2
的使用是错误的 - map2
函数压缩了两个数组,然后将给定函数应用于压缩后的数组。
根据你的问题,我的猜测是你实际上想做其他事情。对于每个输入,您想要遍历所有百分位数并找到第一个百分位数,使得该值大于(或小于?)百分位数。为此,您需要将 map2
替换为如下内容:
let tmpRplcd:string[] =
tmp
|> Array.map (fun y ->
prcntls |> Array.tryPick (fun x ->
if x <= y then Some(lkpTbl.[x]) else None))
|> Array.map (fun v -> defaultArg v "")
我没有合适的版本来尝试这个,但我认为这应该可以满足你的需要(我只是不确定你是否需要 x <= y
或相反!)
下面是您打算对 F# 程序执行的操作的方法。
我想出了 http://www.dummies.com/education/math/statistics/how-to-calculate-percentiles-in-statistics/ 的百分位计算的实现,如下面的统计模块所示。
namespace FSharpBasics
module Statistics =
let percentile p (array: float[]) =
let threshold = (float p / 100.0) * float (array |> Array.length)
let thresholdCeiling = int (System.Math.Ceiling threshold)
let thresholdInteger = int (threshold)
array
|> Array.sort
|> Array.skip (thresholdCeiling - 1)
|> Array.truncate (if thresholdInteger = thresholdCeiling then 2 else 1)
|> Array.average
module PercentileTest =
open System
let tmp = [| 13.0; 17.0; 23.0; 11.0; 11.0; 13.0; 31.0;
19.0; 47.0; 29.0; 29.0; 19.0; 43.0; 37.0 |]
let lbls =
[| for n in 0..20..100 -> "p" + string (n / 20) |]
let prcntls =
[| for n in 0..20..100 -> Statistics.percentile n tmp |]
let tmpPrcntls =
tmp |> Array.sort
let lkpTbl =
Array.zip prcntls lbls
let tmpRplcd : string[] =
tmp
|> Array.map (fun x ->
lkpTbl
|> Array.filter (fun (prcntl, lbl) -> prcntl <= x)
|> Array.last
|> snd)
[<EntryPoint>]
let main argv =
printfn ""
printfn "Percentile Test"
printfn ""
printfn "tmp: %A" tmp
printfn "tmpPrcntls: %A" tmpPrcntls
printfn "prcntls: %A" prcntls
printfn "tmpRplcd: %A" tmpRplcd
System.Console.ReadKey() |> ignore
0 // return an integer exit code
(*---- output ----
Percentile Test
tmp: [|13.0; 17.0; 23.0; 11.0; 11.0; 13.0; 31.0; 19.0; 47.0; 29.0; 29.0; 19.0; 43.0;
37.0|]
tmpPrcntls: [|11.0; 11.0; 13.0; 13.0; 17.0; 19.0; 19.0; 23.0; 29.0; 29.0; 31.0; 37.0; 43.0;
47.0|]
prcntls: [|11.0; 13.0; 19.0; 29.0; 37.0; 47.0|]
tmpRplcd: [|"p1"; "p1"; "p2"; "p0"; "p0"; "p1"; "p3"; "p2"; "p5"; "p3"; "p3"; "p2"; "p4";
"p4"|]
---- ----*)