[Numpy-discussion] Slicing, sum, etc. reduces rank of array?
Fri Sep 24 21:23:26 CDT 2010
On Fri, Sep 24, 2010 at 8:56 PM, George <firstname.lastname@example.org> wrote:
> I couldn't find an answer to my newbie question, so I'm posting it here.
> I have:
> Via broadcasting, I know that
> Being a recent convert from MATLAB, I expected the same result from
> assuming b[:,0] would be the column vector [,].
> Unfortunately, I was wrong. b[:,0] is apparently a 1-rank array of shape
> This causes a*b[:,0] to evaluate as
> a*numpy.array([[5,7]])=numpy.array([[5,14],[15,28]]) instead of
> To get the result I desire, the only way I've been able to come up with is
> to "coerce" b[:,0] into a column vector. Is there an easier way to do this,
> without having to do the reshape?
> I find similar things happen when I use other operations (e.g. "sum") that
> seem to reduce the array rank.
> For example, I would expect numpy.sum(b,1) to also be a "column vector,"
> but it
> also evaluates to a 1-rank array [11, 15] with shape (2,)
> Any thoughts, suggestions?
This has bitten me several times in the past. While there are some neat
tricks around this issue, the one sure-fire, blunt-object solution to the
problem is the np.atleast_2d() function. There is also a 1d and 3d variant
(although the 3d variant messes around a bit with the order of the axes...).
I will leave the more elegant solutions to others to give.
> NumPy-Discussion mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion