-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Missing something like numpy.bincount
#1786
Comments
Transfer this issue to mlx -- I don't see one in the underlying C++ core. Per the docs it looks like it produces a histogram. I think the problem with the API (as written) is that the output size is variable based on the contents of the input, so it doesn't match the mlx model. |
Oh, I can't transfer it. Well @awni what do you think? |
I'll transfer it to MLX. I think we could have a version where the maximum value is provided as an argument.. but without that it would be an op with data-dependent output shape and we don't have support for those in MLX at the moment. |
Well given a fixed output size mx.zeros(N, dtype=mx.int32).at[k_neareset_labels].add(1).argmax() |
Thanks! Can this be done in Swift? I can't find the equivalent to this let a = MLXArray([0, 0, 0])
let idx = MLXArray([0, 1, 0, 1, 1, 2, 1, 2, 2, 2, 2])
for i in idx {
a[i] = a[i] + 1
}
print(a.argMax()) |
Added an issue to add the missing |
@angeloskath the one liner you wrote for this is the entire implementation so I’m debating if it’s worth adding this op or closing this. It might be handy to have the op since it’s not entirely obvious.. I’ll mark it as an enhancement for now but I’m also ok to close it. |
Personally I don't think it is worth an op but I tend to be overly conservative wrt these things ie I don't like ops like relu etc. I think they hide more than they are worth. Otoh |
Fixes ml-explore#1786 Add `bincount` method to MLX to count occurrences of each value in an array. * **mlx/array.cpp**: Implement `bincount` method using `std::unordered_map` to store counts and return an array with the counts of each value in the input array. * **mlx/array.h**: Declare `bincount` method in the `array` class. * **python/src/array.cpp**: Add `bincount` method to the Python API for the `array` class and implement it using the `bincount` method from `mlx/array.cpp`. * **python/tests/test_array.py**: Add test cases for the `bincount` method in the `array` class to verify its correctness with various input arrays. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ml-explore/mlx/issues/1786?shareId=XXXX-XXXX-XXXX-XXXX).
MLX doesn't include a let prediction = Dictionary(grouping: kNearestLabels.asArray(Int.self), by: { $0 })
.mapValues { $0.count }
.max(by: { $0.value < $1.value })!
.key Not as concise as NumPy's one-liner, but at least it's clear! |
While converting a KNN implementation from NumPy to MLX Swift I stumbled upon the usage of a
bincount
to count the number of occurrences of each value in array. I couldn't find a MLX equivalent so I did the following:The NumPy code did this in just one line:
Is there a more "MLX way" of doing this? If not, maybe MLX should have this
bincount
method, seems quite handy.The text was updated successfully, but these errors were encountered: