Closed DavisVaughan closed 5 years ago
:+1: on adding a keepdims
argument to the reducers.
Simply confirming that keepdims
does do what I want. This would be great, I'd probably make it the default in my package. 👍
import numpy as np
x = [[1, 1, 2], [1, 5, 2]]
x
[[1, 1, 2], [1, 5, 2]]
x / np.sum(x, axis=0)
array([[0.5 , 0.16666667, 0.5 ],
[0.5 , 0.83333333, 0.5 ]])
x / np.sum(x, axis=0, keepdims = True)
array([[0.5 , 0.16666667, 0.5 ],
[0.5 , 0.83333333, 0.5 ]])
x / np.sum(x, axis=1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-4f2eb8eb8ed6> in <module>
----> 1 x / np.sum(x, axis=1)
ValueError: operands could not be broadcast together with shapes (2,3) (2,)
x / np.sum(x, axis=1, keepdims = True)
array([[0.25 , 0.25 , 0.5 ],
[0.125, 0.625, 0.25 ]])
Hi Davis,
thanks for your deep investigations here :)
I have a working prototype of keepdims, will integrate it asap. What complicates keepdims (for us C++ people) is that it needs to be a compile-time option since we need to compute the number of dimensions statically.
I'll also investigate your other issues. Not sure how we should correctly handle the scalar problem.
Thanks for your fast work. Continually impressed with how quickly you guys are integrating things.
I can imagine that compile-time limitation is frustrating. I can only wish you the best of luck with my limited knowledge! Thank you!
I've begun implementing my ideas for keep_dims here: https://github.com/wolfv/xtensor/tree/reducer_keep_dims
Closed by QuantStack/xtensor#1474
While working through some reducer examples, I noticed that they always drop the dimension that you are reducing over. Would it be possible to provide an option to keep that dimension and leave it as size 1? I think this can have some neat implications and use cases. For example,
rray_sum()
has been implemented to always keep that dimension as size1
.rarray_matrix_sum_cpp()
is just the standard version that drops the dimension.Notice how I can easily do a reduction then a broadcasting operation when the dimensions are kept.
numpy.sum
actually haskeepdims
as an argument. I played around with it, but im not sure it does exactly what im asking for (but im actually not sure). https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.htmlCreated on 2018-12-08 by the reprex package (v0.2.1.9000)