-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: add initial and out parameters to bincount #22907
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -889,16 +889,17 @@ def vdot(a, b): | |
|
||
|
||
@array_function_from_c_func_and_dispatcher(_multiarray_umath.bincount) | ||
def bincount(x, weights=None, minlength=None): | ||
def bincount(x, weights=None, minlength=None, initial=None, out=None): | ||
""" | ||
bincount(x, /, weights=None, minlength=0) | ||
bincount(x, /, weights=None, minlength=0, initial=None, out=None) | ||
|
||
Count number of occurrences of each value in array of non-negative ints. | ||
|
||
The number of bins (of size 1) is one larger than the largest value in | ||
`x`. If `minlength` is specified, there will be at least this number | ||
of bins in the output array (though it will be longer if necessary, | ||
depending on the contents of `x`). | ||
depending on the contents of `x`). If `out` is specified, its size must | ||
be large enough to contain all of the bins. | ||
Each bin gives the number of occurrences of its index value in `x`. | ||
If `weights` is specified the input array is weighted by it, i.e. if a | ||
value ``n`` is found at position ``i``, ``out[n] += weight[i]`` instead | ||
|
@@ -912,22 +913,32 @@ def bincount(x, weights=None, minlength=None): | |
Weights, array of the same shape as `x`. | ||
minlength : int, optional | ||
A minimum number of bins for the output array. | ||
initial: ndarray, 1 dimension, optional | ||
Array of initial values for each bin. It must have the same shape and | ||
buffer length as the expected output | ||
out: ndarray, 1 dimenion, optional | ||
Alternative output array in which to place the result. It must have the | ||
same shape and buffer length as the expected output but the type will | ||
be cast when safe. | ||
|
||
.. versionadded:: 1.6.0 | ||
|
||
Returns | ||
------- | ||
out : ndarray of ints | ||
The result of binning the input array. | ||
The length of `out` is equal to ``np.amax(x)+1``. | ||
The length of `out` is equal to ``max(minlength, np.amax(x)+1)``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strictly, it can be even larger if the input |
||
|
||
Raises | ||
------ | ||
ValueError | ||
If the input is not 1-dimensional, or contains elements with negative | ||
values, or if `minlength` is negative. | ||
values, or if `minlength` is negative, or initial or out do not have | ||
sufficient sizes, or initial and out have different sizes. | ||
TypeError | ||
If the type of the input is float or complex. | ||
If the type of the input is float or complex, or `minlength` cannot be | ||
converted to int, or `initial` or `out` cannot be safely converted to | ||
the expected type. | ||
|
||
See Also | ||
-------- | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -417,6 +417,8 @@ def bincount( | |
/, | ||
weights: None | ArrayLike = ..., | ||
minlength: SupportsIndex = ..., | ||
initial: None | NDArray[Any] = ..., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, I think scalar would be OK in principle. |
||
out: None | NDArray[Any] = ..., | ||
) -> NDArray[intp]: ... | ||
|
||
def copyto( | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think ideally it would just broadcast to the output (i.e., effectively the default is
initial=0
).