[Numpy-discussion] How to Keep An Array Two Dimensional

2012-11-25 Thread Tom Bennett
Hi,

I am trying to extract n columns from an 2D array and then operate on the
extracted columns. Below is the code:

A is an MxN 2D array.

u = A[:,:n] #extract the first n columns from A

B = np.dot(u, u.T) #take outer product.

This code works when n1. However, when n=1, u becomes an 1D array instead
of an Mx1 2D array and the code breaks down.

I wonder if there is any way to keep u=A[:,:n] an Mxn array no matter what
value n takes. I do not want to use matrix because array is more convenient
in other places.

Thanks,
Tom
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] How to Keep An Array Two Dimensional

2012-11-25 Thread Warren Weckesser
On Sun, Nov 25, 2012 at 8:24 PM, Tom Bennett tom.benn...@mail.zyzhu.netwrote:

 Hi,

 I am trying to extract n columns from an 2D array and then operate on the
 extracted columns. Below is the code:

 A is an MxN 2D array.

 u = A[:,:n] #extract the first n columns from A

 B = np.dot(u, u.T) #take outer product.

 This code works when n1. However, when n=1, u becomes an 1D array instead
 of an Mx1 2D array and the code breaks down.

 I wonder if there is any way to keep u=A[:,:n] an Mxn array no matter what
 value n takes. I do not want to use matrix because array is more convenient
 in other places.


Tom,

Your example works for me:

In [1]: np.__version__
Out[1]: '1.6.2'

In [2]: A = arange(15).reshape(3,5)

In [3]: A
Out[3]:
array([[ 0,  1,  2,  3,  4],
   [ 5,  6,  7,  8,  9],
   [10, 11, 12, 13, 14]])

In [4]: u = A[:,:1]

In [5]: u
Out[5]:
array([[ 0],
   [ 5],
   [10]])

In [6]: B = np.dot(u, u.T)

In [7]: B
Out[7]:
array([[  0,   0,   0],
   [  0,  25,  50],
   [  0,  50, 100]])



Warren



 Thanks,
 Tom



 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] How to Keep An Array Two Dimensional

2012-11-25 Thread Tom Bennett
Thanks for the quick response.

Ah, I see. There is a difference between A[:,:1] and A[:,0]. The former
returns an Mx1 2D array whereas the latter returns an M element 1D array. I
was using A[:,0] in the code but A[:,:1] in the example.


On Sun, Nov 25, 2012 at 8:35 PM, Warren Weckesser 
warren.weckes...@gmail.com wrote:



 On Sun, Nov 25, 2012 at 8:24 PM, Tom Bennett 
 tom.benn...@mail.zyzhu.netwrote:

 Hi,

 I am trying to extract n columns from an 2D array and then operate on the
 extracted columns. Below is the code:

 A is an MxN 2D array.

 u = A[:,:n] #extract the first n columns from A

 B = np.dot(u, u.T) #take outer product.

 This code works when n1. However, when n=1, u becomes an 1D array
 instead of an Mx1 2D array and the code breaks down.

 I wonder if there is any way to keep u=A[:,:n] an Mxn array no matter
 what value n takes. I do not want to use matrix because array is more
 convenient in other places.


 Tom,

 Your example works for me:

 In [1]: np.__version__
 Out[1]: '1.6.2'

 In [2]: A = arange(15).reshape(3,5)

 In [3]: A
 Out[3]:
 array([[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14]])

 In [4]: u = A[:,:1]

 In [5]: u
 Out[5]:
 array([[ 0],
[ 5],
[10]])

 In [6]: B = np.dot(u, u.T)

 In [7]: B
 Out[7]:
 array([[  0,   0,   0],
[  0,  25,  50],
[  0,  50, 100]])



 Warren



 Thanks,
 Tom



 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion



 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] How to Keep An Array Two Dimensional

2012-11-25 Thread David Warde-Farley
On Sun, Nov 25, 2012 at 9:47 PM, Tom Bennett tom.benn...@mail.zyzhu.net wrote:
 Thanks for the quick response.

 Ah, I see. There is a difference between A[:,:1] and A[:,0]. The former
 returns an Mx1 2D array whereas the latter returns an M element 1D array. I
 was using A[:,0] in the code but A[:,:1] in the example.

You'll notice that Python lists and tuples work the same way: foo[0]
on a list or tuple gives you the first element whereas foo[:1] gives
you a list or tuple containing only the first element.

To clarify what's going on in the case of NumPy: when you use the [:,
0] syntax, the interpreter is calling A.__getitem__ with the tuple
(slice(None), 0) as the argument. When you use [:, :1], the argument
is (slice(None), slice(None, 1)). You can try this out with
A[(slice(None), slice(None, 1))] -- it does the same thing (creating
index tuples explicitly like this can be very handy in certain cases).

The rule that NumPy follows for index tuples is (approximately) that
scalar indices always squash the corresponding dimension, whereas
slices or iterables (in the case of fancy indexing) preserve the
dimension with an appropriate size. Notably, A[:, [0]] will also
return an (A.shape[0], 1) array. But the semantics here are different:
because using a sequence as an advanced indexing operation, a copy
is made, whereas A[:, :1] will return a view.

Hope that makes things less mysterious,

David
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion