-
Notifications
You must be signed in to change notification settings - Fork 195
Weighted mean with function #886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
d6a36af
2acfbfa
b8be0a5
7653f2c
448e6ee
6a6997a
f9984f8
c1768a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -682,6 +682,31 @@ function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) | |||||||||
return mean(A, dims=dims) | ||||||||||
end | ||||||||||
|
||||||||||
""" | ||||||||||
mean(f, A::AbstractArray, w::AbstractWeights[, dims::Int]) | ||||||||||
|
||||||||||
Compute the weighted mean of array `A`, after transforming it'S | ||||||||||
contents with the function `f`, with weight vector `w` (of type | ||||||||||
`AbstractWeights`). If `dim` is provided, compute the | ||||||||||
weighted mean along dimension `dims`. | ||||||||||
|
||||||||||
# Examples | ||||||||||
```julia | ||||||||||
n = 20 | ||||||||||
x = rand(n) | ||||||||||
w = rand(n) | ||||||||||
mean(√, x, weights(w)) | ||||||||||
``` | ||||||||||
""" | ||||||||||
mean(f, A::AbstractArray, w::AbstractWeights; dims::Union{Colon,Int}=:) = | ||||||||||
_mean(f.(A), w, dims) | ||||||||||
|
_mean(f.(A), w, dims) | |
_mean(f, A; dims) |
I'd also suggest making it slightly more generic, as
_mean(f.(A), w, dims) | |
_mean(f, A; kwargs...) |
(See below for more details.)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a slightly more generic version of the same thing, which is less likely to require maintenance in the future (if we add additional keyword arguments to mean
). I suggest using this pattern when you can.
function mean(f, A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) | |
function mean(f, A::AbstractArray, w::UnitWeights; kwargs...) |
ParadaCarleton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid memory allocation by using mean(f, A)
instead of mean(f.(A))
. Remember that f.(A)
creates an extra array, which is slow. Memory access is usually the biggest bottleneck on modern CPUs. mean(f.(A))
is 2 separate operations: The first one creates a new array, f.(A)
, and the second calculates its mean. mean(f, A)
calculates the mean of (f(x) for x in A)
directly, as one operation, without creating a new array.
return mean(f.(A), dims=dims) | |
return mean(f, A; dims) |
I'd also suggest making it slightly more generic, as
return mean(f.(A), dims=dims) | |
return mean(f, A; kwargs...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dims
shouldn't be required to be an integer.