gpt4 book ai didi

julia - 如何根据Julia中列中的值查找数据框行的平均值?

转载 作者:行者123 更新时间:2023-12-01 00:43:42 26 4
gpt4 key购买 nike

我在 Julia 中有以下数据框。

using DataFrames 
data = DataFrame(Value = [23, 56, 10, 48, 51], Type = ["A", "B", "A", "B", "B"])

5×2 DataFrame
│ Row │ Value │ Type │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 23 │ A │
│ 2 │ 56 │ B │
│ 3 │ 10 │ A │
│ 4 │ 48 │ B │
│ 5 │ 51 │ B │

如何获得列的平均值 基于列 类型 ?

最佳答案

如果您想要性能,请考虑以下选项

julia> using DataFrames

julia> using Statistics

julia> using BenchmarkTools

julia> data = DataFrame(Value = rand(1:10, 10^6),
Type = categorical(rand(["A", "B"], 10^6)));

请注意,我生成了 :Type列作为分类,因为这将在以后聚合得更快。

首先是上面答案的时间:
julia> @benchmark by($data, [:Type], df -> mean(df[:, :Value]))
BenchmarkTools.Trial:
memory estimate: 30.53 MiB
allocs estimate: 212
--------------
minimum time: 12.173 ms (0.00% GC)
median time: 13.305 ms (3.63% GC)
mean time: 14.229 ms (4.30% GC)
maximum time: 20.491 ms (2.98% GC)
--------------
samples: 352
evals/sample: 1

这是我更改的时间 df[:, :Value]df.Value .不同的是 df.Value不会不必要地复制数据。您可以看到,您已经节省了 10% 以上的运行时间:
julia> @benchmark by($data, :Type, df -> mean(df.Value))
BenchmarkTools.Trial:
memory estimate: 22.90 MiB
allocs estimate: 203
--------------
minimum time: 10.926 ms (0.00% GC)
median time: 13.151 ms (1.92% GC)
mean time: 13.093 ms (3.53% GC)
maximum time: 16.933 ms (3.25% GC)
--------------
samples: 382
evals/sample: 1

这是一种有效的编写方法。此语句表示我们通过列 :Value到函数 mean :
julia> @benchmark by($data, :Type, :Value => mean)
BenchmarkTools.Trial:
memory estimate: 15.27 MiB
allocs estimate: 190
--------------
minimum time: 8.326 ms (0.00% GC)
median time: 8.667 ms (0.00% GC)
mean time: 9.599 ms (2.74% GC)
maximum time: 17.364 ms (3.57% GC)
--------------
samples: 521
evals/sample: 1

最后,让我们检查一下差异是否 :ValueVector{String} (在另一个答案中给出的方法):
julia> data.Type = String.(data.Type);

julia> @benchmark by($data, [:Type], df -> mean(df[:, :Value]))
BenchmarkTools.Trial:
memory estimate: 46.16 MiB
allocs estimate: 197
--------------
minimum time: 26.664 ms (2.08% GC)
median time: 27.197 ms (2.11% GC)
mean time: 27.486 ms (2.11% GC)
maximum time: 35.740 ms (1.64% GC)
--------------
samples: 182
evals/sample: 1

您可以看到它比推荐的答案慢了大约三倍。另请注意:
julia> by(data, :Type, :Value => mean)
2×2 DataFrame
│ Row │ Type │ Value_mean │
│ │ String │ Float64 │
├─────┼────────┼────────────┤
│ 1 │ B │ 5.50175 │
│ 2 │ A │ 5.49524 │

为生成的列生成更好的默认名称(因为它知道源列名称和转换函数名称)。

关于julia - 如何根据Julia中列中的值查找数据框行的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60821416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com