8000 ENH: np.nanmean with weights · Issue #21375 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: np.nanmean with weights #21375

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 stateme 8000 nt. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
marctorsoc opened this issue Apr 21, 2022 · 4 comments
Open

ENH: np.nanmean with weights #21375

marctorsoc opened this issue Apr 21, 2022 · 4 comments

Comments

@marctorsoc
Copy link

Proposed new feature or change:

My motivation comes from this PR in sklearn https://github.com/scikit-learn/scikit-learn/pull/23183/files#diff-b62de604fe871c9d2dcbfc799daf93b5bcebef1bf08341a0a7fc7562ba20b337R148, where I wrote:

Wrapper for np.average, with np.nan values being ignored from the average
    This is similar to np.nanmean, but allowing to pass weights as in np.average

The ideas would be to add this functionality to one of the two functions, i.e:

a) add weights to np.nanmean: https://numpy.org/doc/stable/reference/generated/numpy.nanmean.html
b) add option to ignore nan's for np.average : https://numpy.org/doc/stable/reference/generated/numpy.average.html

I'm aware of https://numpy.org/doc/stable/reference/generated/numpy.ma.average.html#numpy.ma.average but to be honest, I think it'd still be useful to implicitly indicate the mask with np.nan in the value, rather than having to create a masked array.

I'm happy to (at least try) do the PR myself, but would like some feedback on the idea / scope / api, and whether I missed some already existing method.

@itang1
Copy link
itang1 commented Oct 2, 2023

Has this feature been implemented yet? I'm trying to take a weighted nanmean of a list

@marctorsoc
Copy link
Author

@itang1 I don't think so, but you can use my wrapper from sklearn, released recently:

from sklearn.utils.extmath import _nanaverage
_nanaverage(np.array([3, np.nan, 4]), weights=[1,2,3])

@awestm
Copy link
awestm commented Dec 23, 2023

Hey, have a draft PR for this change (#25474). Decided to go with adding an ignore_nan parameter to np.average as it seemed like the path of least resistance. Would love some feedback on this if anyone has the time.

@awestm
Copy link
awestm commented Jan 31, 2024

Looked into option a) since option b) was struck down. Problem is that weights need to also be added to np.mean as all the nan functions are required to have identical signatures to their non nan counterparts. Adding weights to np.mean is pretty hands on and seems above my ability.

If anyone else wants to take this on, feel free.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
2AAA
0