beginner question: rank-1 arrays
Hi, I am trying to implement a project in scipy. I think I am getting somewhere finally. However in my code (I am converting from MATLAB) it is important to maintain 2d arrays, and keep the difference between row and column vectors. After working through some initial problems I think I am getting more of a picture of how things work in numpy. However I am finding my code littered with things like: np.array(np.r_[0:nterms],ndmin=2) (for a row vector) or np.array(np.r_[0:nterms],ndmin=2).T (for a column vector) Coming from matlab and being use to 0:10 for row or (0:10)' for column this seems a bit messy. Is there a better way of constructing row/column 2d arrays from a slice type range? Thanks, Robin
On Mon, Oct 08, 2007 at 11:00:39PM +0100, Robin wrote:
Coming from matlab and being use to 0:10 for row or (0:10)' for column this seems a bit messy. Is there a better way of constructing row/column 2d arrays from a slice type range?
r_[0:10] and c_[0:10]. Does that suit you ? The first one is indeed only 1D, but I don't see the problem with that. If you really want 2D you can use c_[0:10].T . Gaël
On 10/8/07, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
r_[0:10] and c_[0:10].
Does that suit you ? The first one is indeed only 1D, but I don't see the problem with that. If you really want 2D you can use c_[0:10].T .
Thanks, but not really :) Firstly - for me I don' see any difference between the two, they both give a numpy rank-1 array which doesn't have a row/column characteristic. x.shape for both of the above is (10,) I need (10,1) or (1,10) for my code. Robin
On Mon, Oct 08, 2007 at 11:12:07PM +0100, Robin wrote:
On 10/8/07, Gael Varoquaux <[1]gael.varoquaux@normalesup.org> wrote:
r_[0:10] and c_[0:10].
Does that suit you ? The first one is indeed only 1D, but I don't see the problem with that. If you really want 2D you can use c_[0:10].T .
Thanks, but not really :)
Firstly - for me I don' see any difference between the two, they both give a numpy rank-1 array which doesn't have a row/column characteristic. x.shape for both of the above is (10,) I need (10,1) or (1,10) for my code.
Damn it. Shame on me. I meant c_[0:10,]. If you really need a shape of (1,10) (I have never had such a need) you can use c_[0:10,].T. HTH, Gaël
On 10/8/07, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
Damn it. Shame on me. I meant c_[0:10,]. If you really need a shape of (1,10) (I have never had such a need) you can use c_[0:10,].T.
Thanks! - the trick with the , is just the sort of thing I was looking for - I knew there must be an easy way... Cheers Robin
On Mon, Oct 08, 2007 at 11:00:39PM +0100, Robin wrote:
Hi,
I am trying to implement a project in scipy. I think I am getting somewhere finally.
However in my code (I am converting from MATLAB) it is important to maintain 2d arrays, and keep the difference between row and column vectors. After working through some initial problems I think I am getting more of a picture of how things work in numpy.
However I am finding my code littered with things like: np.array(np.r_[0:nterms],ndmin=2) (for a row vector) or np.array(np.r_[0:nterms],ndmin=2).T (for a column vector)
You can use N.arange(10).reshape(-1,1) or N.c_[:10,] But if you use this sort of thing often, just write your own factory method: def col(n): return N.arange(n).reshape(-1,1) Cheers Stéfan
Alan G Isaac schrieb:
On Mon, 8 Oct 2007, Robin apparently wrote:
However in my code (I am converting from MATLAB) it is important to maintain 2d arrays, and keep the difference between row and column vectors.
How about using matrices? help(numpy.mat)
hth, Alan Isaac
Robin, Alan is right, you want numpy matrices which are always 2d. Check out numpy.matlib; if you replace from numpy import [whatever] by from numpy.matlib import [whatever] you get everything there is in numpy, and things like ones() zeros() empty() etc. will always be 2d matrices. -sven
Sorry for coming very late to the thread, but you mean something like: for i in range(len(cols): On Oct 9, 2007 4:36 AM, Sven Schreiber <svetosch@gmx.net> wrote:
Alan G Isaac schrieb:
On Mon, 8 Oct 2007, Robin apparently wrote:
However in my code (I am converting from MATLAB) it is important to maintain 2d arrays, and keep the difference between row and column vectors.
How about using matrices? help(numpy.mat)
hth, Alan Isaac
Robin, Alan is right, you want numpy matrices which are always 2d. Check out numpy.matlib; if you replace from numpy import [whatever] by from numpy.matlib import [whatever] you get everything there is in numpy, and things like ones() zeros() empty() etc. will always be 2d matrices.
-sven
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On Nov 16, 2007 5:55 PM, Emanuel Woiski <woiski@gmail.com> wrote:
Sorry for coming very late to the thread, but you mean something like: for i in range(len(cols):
[sending too soon...]
On Oct 9, 2007 4:36 AM, Sven Schreiber <svetosch@gmx.net > wrote:
Alan G Isaac schrieb:
On Mon, 8 Oct 2007, Robin apparently wrote:
However in my code (I am converting from MATLAB) it is important to maintain 2d arrays, and keep the difference between row and column vectors.
Well, I have noticed that numpy doesn't care very much about rows and
cols. Mind you, if you slice along a col, you end up with a row - just try it and see. But how can you evaluate an expression such as a[i] - b[j], for all (i,j), with i for rows and j for cols? The trick here is 'newaxis' . With 'newaxis' you have a temporary dimension for a or b, without actually changing a or b shapes. Following the usual meaning, the expression becomes: c = a[:,newaxis] - b See:
a = arange(3.) a array([ 0., 1., 2.]) b = a # just an example... Now I want: c1 = zeros((3,3)) for i in range(3): for j in range (3): c1[i,j] = a[i] - b[j]
c1 array([[ 0., -1., -2.], [ 1., 0., -1.], [ 2., 1., 0.]])
That's exactly the same as the one-liner:
c2 = a[:,newaxis] - b c2 array([[ 0., -1., -2.], [ 1., 0., -1.], [ 2., 1., 0.]])
Nice isn't it? Hope that help you somehow....:) cheers woiski
participants (6)
-
Alan G Isaac
-
Emanuel Woiski
-
Gael Varoquaux
-
Robin
-
Stefan van der Walt
-
Sven Schreiber