[Numpy-discussion] force column vector

Keith Goodman kwgoodman at gmail.com
Wed Feb 7 17:41:21 EST 2007


On 2/7/07, Sven Schreiber <svetosch at gmx.net> wrote:
> Christopher Barker schrieb:
> > Christian wrote:
> >> when creating an ndarray from a list, how can I force the result to be
> >> 2d *and* a column vector? So in case I pass a nested list, there will be no
> >> modification of the shape and when I pass a simple list, it will be
> >> converted to a 2d column vector.
> >
> > I'm not sure I understand the specification of the problem. I would
> > think that the definition of a column vector is that it's shape is:
> >
> > (-1,1)
> >
>
> So I think what's needed is:
>
> b = array(yourlist)
> b.reshape(b.shape[0], -1)
>
> Now it seems I finally understood this business with the -1 in the
> shapes... (well it's trivial if you have the book :-)


I'd like to know what the -1 means. But first I'm trying to figure out
why there are two reshapes? Do they behave identically? The doc
strings make it look like they might not.

>> x = M.rand(3,3)
>> x.reshape?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in method reshape of matrix object at 0xb4b41df4>
Namespace:      Interactive
Docstring:
    a.reshape(d1, d2, ..., dn, order='c')

    Return a new array from this one.  The new array must have the same number
    of elements as self.  Also always returns a view or raises a ValueError if
    that is impossible.;

>> M.reshape?
Type:           function
Base Class:     <type 'function'>
String Form:    <function reshape at 0xb776541c>
Namespace:      Interactive
File:           /usr/local/lib/python2.4/site-packages/numpy/core/fromnumeric.py
Definition:     M.reshape(a, newshape, order='C')
Docstring:
    Return an array that uses the data of the given array, but with a new
    shape.

    :Parameters:
      - `a` : array
      - `newshape` : shape tuple or int
        The new shape should be compatible with the original shape. If an
        integer, then the result will be a 1D array of that length.
      - `order` : 'C' or 'FORTRAN', optional (default='C')
        Whether the array data should be viewed as in C (row-major) order or
        FORTRAN (column-major) order.

    :Returns:
      - `reshaped_array` : array
        This will be a new view object if possible; otherwise, it will return
        a copy.

    :See also:
      numpy.ndarray.reshape() is the equivalent method.



More information about the NumPy-Discussion mailing list