[Numpy-discussion] design issues - octave 'incompatibilities'
Soeren Sonnenburg
python-ml at nn7.de
Mon Jul 25 22:23:11 EDT 2005
On Mon, 2005-07-25 at 08:59 -0700, Chris Barker wrote:
> Soeren Sonnenburg wrote:
> > -- why do vectors have no 'orientation', i.e. there are only row but no
> > column vectors (or why do you treat matrices/vectors differently, i.e.
> > have vectors at all as a separate type)
>
> I think the key to understanding this is that NumPy uses a
> fundamentally different data type that MATLAB and it's derivatives.
> MATLAB was originally just what it is called: a "Matrix" laboratory. The
> basic data type of Matlab is a 2-d matrix. Even a scalar is really a 1X!
> matrix. Matlab has a few tricks that can make these look like row and
> column vectors, etc, but they are really always matrices.
Ok, I am realizing that R also distinguishes between vectors and
matrices.
> On the other hand, NumPy arrays are N-dimensional, where N is
> theoretically unlimited. In practice, I think the max N is defined and
> compiled in, but you could change it and re-compile if you wanted. In
> any case, many of us frequently use 3-d and higher arrays, and they can
> be very useful. When thought of this way, you can see why there is no
Well at least this is the same for octave/matlab.
> such thing as a column vs. a row vector. A vector is a one-dimensional
> array: it has no orientation.
This makes life more difficult if one wants to convert
from octave/matlab -> numarray and automated systems close to
impossible. If vectors had the same properties/functions as matrices one
would not have such problems, i.e. v^{transpose} * u == dot(v,u) and v*u
-> error
> However, NumPy does support NX1 and 1XN 2-d arrays which can be very handy:
> >>> import numarray as N
> >>> a = N.arange(5)
> >>> a.shape = (1,-1)
> >>> a
> array([[0, 1, 2, 3, 4]])
> >>> b = N.arange(5)
> >>> b.shape = (-1,1)
> >>> a
> array([0, 1, 2, 3, 4])
> >>> b
> array([[0],
> [1],
> [2],
> [3],
> [4]])
>
> So a is a row vector and b is a column vector. If you multiply them, you
> get "array broadcasting":
> >>> a * b
> array([[ 0, 0, 0, 0, 0],
> [ 0, 1, 2, 3, 4],
> [ 0, 2, 4, 6, 8],
> [ 0, 3, 6, 9, 12],
> [ 0, 4, 8, 12, 16]])
>
> This eliminates a LOT of extra duplicate arrays that you have to make in
> Matlab with meshgrid.
In my eyes 'array broadcasting' is confusing and should rather be in a
function like meshgrid and instead a*b should return
matrixmultiply(a,b) ...
> When you index into an array, you reduce its rank (number of dimensions)
> by 1:
> >>> a = N.arange(27)
> >>> a.shape = (3,3,3)
> >>> a
> array([[[ 0, 1, 2],
> [ 3, 4, 5],
> [ 6, 7, 8]],
>
> [[ 9, 10, 11],
> [12, 13, 14],
> [15, 16, 17]],
>
> [[18, 19, 20],
> [21, 22, 23],
> [24, 25, 26]]])
> >>> a.shape
> (3, 3, 3)
> >>> a[1].shape
> (3, 3)
> >>> a[1][1].shape
> (3,)
>
> When you slice, you keep the rank the same:
>
> >>> a[1:2].shape
> (1, 3, 3)
>
> This creates a way to make row and column "vectors" from your 2-d array
> (matrix)
> >>> a = N.arange(25)
> >>> a.shape = (5,5)
> >>> a
> array([[ 0, 1, 2, 3, 4],
> [ 5, 6, 7, 8, 9],
> [10, 11, 12, 13, 14],
> [15, 16, 17, 18, 19],
> [20, 21, 22, 23, 24]])
>
> To make a "row vector" (really a 1XN matrix)
> >>> a[0:1,:]
> array([[0, 1, 2, 3, 4]])
>
>
> To make a "column vector" (really a NX1 matrix)
> >>> a[:,0:1]
> array([[ 0],
> [ 5],
> [10],
> [15],
> [20]])
>
>
> I hope that helps:
Indeed it does - Thanks!! Unfortunately I am not at all happy now that
'*' != matrixmultiply (but outerproduct) for vectors/matrices...
I realize that with lists it is ok to grow them via slicing.
x=[]
x[0]=1
IndexError: list assignment index out of range
x[0:0]=[1]
x
[1]
that seems not to work with numarray ... or ?
y=array()
y[0]=1
TypeError: object does not support item assignment
y[0:0]=array([1])
TypeError: object does not support item assignment
Soeren.
More information about the NumPy-Discussion
mailing list