[Matrix-SIG] Re: NumPy questions

Travis Oliphant Oliphant.Travis@mayo.edu
Fri, 26 Mar 1999 13:04:22 -0600 (EST)


> 
> I've been working thru the newly released Numerical Python document
> and subscribed to the matrix SIG. But, I'm having many problems.
> Where would be the appropriate place to direct these questions
> in the future so I can stop bugging you?

The matrix-sig is a good place to ask questions.  Then you can get
answers from many people besides me (multiple insights).  I don't mind
answering questions when I can help, though.

Email matrix-sig@python.org to have your mail go to the list.  I'm posting
this repsonse to that list, too.

Archives of the list can be viewed at
http://www.python.org/pipermail/matrix-sig/

If your question is more general (not NumPy specific) you can
get a wider audience by posting to comp.lang.python as you did before.


> 
> Also, just so you know, I'm considering using NumPy to rewrite
> a bunch of stuff for cortical surface extraction & flattening. 
> ....  I think NumPy/Python could save me a 
> lot of time, but only if I can figure it out!
> 
> I have an array defined in python:
> 
> >>> b
> array([[ 7,  8,  9,  9,  9,  9,  9, 11],
>        [14, 15, 16, 15, 15, 17, 19, 13],
>        [ 7,  9, 12, 13, 14, 13, 12, 10],
>        [ 9, 12, 15, 13, 11,  9,  7,  8],
>        [10, 10, 10,  9,  9,  7,  6,  6],
>        [ 7,  9, 11, 11, 12, 12, 13, 13],
>        [13, 11, 10,  9,  9,  9,  9, 11],
>        [14, 16, 18, 16, 14, 11,  9, 10],
>        [12, 11, 11, 10,  9, 10, 11, 15],
>        [20, 16, 13, 13, 13, 18, 23, 26],
>        [30, 21, 12,  9,  7,  8, 10, 11],
>        [13, 13, 14, 12, 10,  9,  8,  8],
>        [ 9, 11, 13, 13, 14, 16, 18, 17],
>        [16, 13, 11, 11, 11, 11, 12, 13],
>        [15, 15, 16, 12,  8,  7,  7,  8]],'b')
> 
> I try to perform a searchsorted operation on it:
> 
> >>> searchsorted (b, arange (0.0,255,1.0))
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> ValueError: Object too deap for desired array
>  

You've noticed that sometimes the error messages have typos.  These should
be fixed.

> What does this error message mean? I searched all the comp.lang.python
> postings and the www.python.org website. Why is my array too "deap"??

Searching for an explanation of error messages is often not fruitful in
Numerical.  The only real explanation of all of the messages is in the
source code.  The error messages themselves are supposed to be
somewhat helpful in determining the problem.

In this case the error message type is ValueError.  This means that an
incorrect value was passed to the function.  The explanation is that the
object is too "deap" (deep).  Depth of an object refers to how many
dimensions it has.  Here the searchsorted function works only with rank-1
arrays (i.e. 1 dimensional arrays)  so it can't be used with a matrix like
this.

> It's an 15x8 matrix which brings up another question: normally,
> the first number is the number of columns. In python, it's the
> number of rows...?? Are matrices sorted in row-major order? column-major? 
>

I don't know what you mean by normally.  NumPy defines a generic array
object that can have any number of dimensions (actually it's restricted to
no more than 40 dimensions at the moment).  Row-major and column-major
order are specific for matrices only.

With that said, look at b.shape:
>>> print b.shape
(15, 8)

So b is an array of rank-2 that has 15 elements along the first dimension
(number of rows) and 8 elements along the second dimension
(number of columns). This fits well with interpretation that b is a 15x8
matrix.  I don't now what you mean by "sorted in row-major order...".  If
you are talking about how the data is stored in memory, then if the array
is contiguous (not the result of a slicing operation) it is stored with
the last dimension varying the fastest.  If it is not contiguous then
ravel will give you the elements as a rank-1 (1-D) array with the last
dimension varying the fastest and the first dimension varying the slowest: 

>>> ravel(b)
array([ 7,  8,  9,  9,  9,  9,  9, 11, 14, 15, 16, 15, 15, 17, 19, 13,  7,
9, 12,
            13, 14, 13, 12, 10,  9, 12, 15, 13, 11,  9,  7,  8, 10, 10,
10,  9,  9,
             7,  6,  6,  7,  9, 11, 11, 12, 12, 13, 13, 13, 11, 10,  9,
9,  9,  9,
            11, 14, 16, 18, 16, 14, 11,  9, 10, 12, 11, 11, 10,  9, 10,
11, 15, 20,
            16, 13, 13, 13, 18, 23, 26, 30, 21, 12,  9,  7,  8, 10, 11,
13, 13, 14,
            12, 10,  9,  8,  8,  9, 11, 13, 13, 14, 16, 18, 17, 16, 13,
11, 11, 11,
            11, 12, 13, 15, 15, 16, 12,  8,  7,  7,  8],'b')

 
> I try a few of the other functions:
> 
> >>> sort (b,0)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
>   File "/usr/local/lib/python1.5/NumPy/Numeric.py", line 73, in sort
>     if axis != -1: a = swapaxes(a, axis, -1)
>   File "/usr/local/lib/python1.5/NumPy/Numeric.py", line 49, in swapaxes
>     n = len(shape(a))
>   File "/usr/local/lib/python1.5/NumPy/Numeric.py", line 278, in shape
>     return asarray(a).shape
>   File "/usr/local/lib/python1.5/NumPy/Numeric.py", line 274, in asarray
>     return array(a, typecode, copy=0)
> TypeError: argument 2: expected string, None found
> 

I'm not sure what is happening here as when I enter sort(b,0) I get the
array b sorted along the first (0th) axis.  Using matrix terminology it is
sorted down each column.  To sort along each row then use sort(b,1) { =
sort(b,-1) = sort(b) }.  Again, this does not happen to me, has b changed
somehow?

>>> b
array([[ 7,  8,  9,  9,  9,  9,  9, 11],
       [14, 15, 16, 15, 15, 17, 19, 13],
       [ 7,  9, 12, 13, 14, 13, 12, 10],
       [ 9, 12, 15, 13, 11,  9,  7,  8],
       [10, 10, 10,  9,  9,  7,  6,  6],
       [ 7,  9, 11, 11, 12, 12, 13, 13],
       [13, 11, 10,  9,  9,  9,  9, 11],
       [14, 16, 18, 16, 14, 11,  9, 10],
       [12, 11, 11, 10,  9, 10, 11, 15],
       [20, 16, 13, 13, 13, 18, 23, 26],
       [30, 21, 12,  9,  7,  8, 10, 11],
       [13, 13, 14, 12, 10,  9,  8,  8],
       [ 9, 11, 13, 13, 14, 16, 18, 17],
       [16, 13, 11, 11, 11, 11, 12, 13],
       [15, 15, 16, 12,  8,  7,  7,  8]],'b')
>>> sort(b,0)
array([[ 7,  8,  9,  9,  7,  7,  6,  6],
       [ 7,  9, 10,  9,  8,  7,  7,  8],
       [ 7,  9, 10,  9,  9,  8,  7,  8],
       [ 9, 10, 11,  9,  9,  9,  8,  8],
       [ 9, 11, 11, 10,  9,  9,  9, 10],
       [10, 11, 11, 11,  9,  9,  9, 10],
       [12, 11, 12, 11, 10,  9,  9, 11],
       [13, 12, 12, 12, 11, 10, 10, 11],
       [13, 13, 13, 12, 11, 11, 11, 11],
       [14, 13, 13, 13, 12, 11, 12, 13],
       [14, 15, 14, 13, 13, 12, 12, 13],
       [15, 15, 15, 13, 14, 13, 13, 13],
       [16, 16, 16, 13, 14, 16, 18, 15],
       [20, 16, 16, 15, 14, 17, 19, 17],
       [30, 21, 18, 16, 15, 18, 23, 26]],'b')


> Well, I don't know what's wrong with this, as b is a 2-D matrix
> and I'm sorting using the 0th axis...

This should work just fine.

Try

>>> shape(b)

>>> asarray(b)


> Does it need to be square??

No 

> 
> These are a few of the examples of the problems I'm having.
> 

I hope we can help you resolve them.


- Travis