[Numpy-discussion] Need some explanations on assigning/incrementing values in Numpy

Robert Kern robert.kern at gmail.com
Sat Nov 22 18:12:43 EST 2008


On Sat, Nov 22, 2008 at 13:19, Loïc BERTHE <berthe.loic at gmail.com> wrote:
> I've encoutered an error during an ipython's session that I fail to understand :
>
> In [12]: n = 4
> In [13]: K = mat(diag(arange(2*n)))
> In [14]: print K
> [[0 0 0 0 0 0 0 0]
>  [0 1 0 0 0 0 0 0]
>  [0 0 2 0 0 0 0 0]
>  [0 0 0 3 0 0 0 0]
>  [0 0 0 0 4 0 0 0]
>  [0 0 0 0 0 5 0 0]
>  [0 0 0 0 0 0 6 0]
>  [0 0 0 0 0 0 0 7]]
>
> In [15]: o = 2*arange(n)
> In [16]: kc = 10 + arange(n)
> In [17]: K[ r_[o-1, o], r_[o, o-1] ] = r_[kc, kc]
> In [18]: print K
> [[ 0  0  0  0  0  0  0 10]
>  [ 0  1 11  0  0  0  0  0]
>  [ 0 11  2  0  0  0  0  0]
>  [ 0  0  0  3 12  0  0  0]
>  [ 0  0  0 12  4  0  0  0]
>  [ 0  0  0  0  0  5 13  0]
>  [ 0  0  0  0  0 13  6  0]
>  [10  0  0  0  0  0  0  7]]
>
>
> In [19]: K[ r_[o-1, o], r_[o, o-1]]  += 10*r_[kc, kc]
> ---------------------------------------------------------------------------
> <type 'exceptions.ValueError'>            Traceback (most recent call last)
> /home/loic/Python/numpy/<ipython console> in <module>()
> <type 'exceptions.ValueError'>: array is not broadcastable to correct shape
>
>
> In [20]: print K[ r_[o-1, o], r_[o, o-1]].shape, r_[kc, kc].shape
> (1, 8) (8,)
>
> In [21]: print K[ r_[o-1, o], r_[o, o-1]].shape, r_[kc, kc][newaxis, :].shape
> (1, 8) (1, 8)
>
> In [22]: K[ r_[o-1, o], r_[o, o-1]]  += 10*r_[kc, kc][newaxis, :]
> ---------------------------------------------------------------------------
> <type 'exceptions.ValueError'>            Traceback (most recent call last)
> /home/loic/Python/numpy/<ipython console> in <module>()
> <type 'exceptions.ValueError'>: array is not broadcastable to correct shape
>
> Could you explain me :
>  - Why do an assignment at line 17 works where an increment raises an
> error (line 19) ?

matrix objects are a bit weird. Most operations on them always return
a 2D matrix, even if the same operation on a regular ndarray would
return a 1D array. In the assignment, no object is actually created by
indexing K, so this coercion never happens, and the effect is the same
as if you were using ndarrays. With +=, on the other hand, indexing K
does return something that gets coerced to a 2D matrix. I'm not
entirely sure where the ValueError is getting raised, but I suspect it
happens when the result is getting assigned back into K.

Your code works fine if you just use regular ndarray objects. I highly
recommend just using ndarrays, especially if you are going to be doing
advanced indexing like this. The "always return a 2D matrix" semantics
really get in the way for such things.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list