Re: A question about numpy

2015-04-14 Thread Paulo da Silva
On 14-04-2015 23:49, Rob Gaddi wrote:
 On Tue, 14 Apr 2015 23:41:56 +0100, Paulo da Silva wrote:
 
 Supposing I have 2 vectors v1 and v2 and a value (constant) k.
 I want to build a vector r with all values of v1 greater than k and the
 others from v2.

 
 You're looking for numpy.where() .
 
That's it! Thank you.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A question about numpy

2015-04-14 Thread Rob Gaddi
On Tue, 14 Apr 2015 23:41:56 +0100, Paulo da Silva wrote:

 Supposing I have 2 vectors v1 and v2 and a value (constant) k.
 I want to build a vector r with all values of v1 greater than k and the
 others from v2.
 

You're looking for numpy.where() .

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: a question about numpy

2009-09-10 Thread Robert Kern

On 2009-09-09 20:46 PM, rechard wrote:

Robert Kern wrote:

On 2009-09-08 20:45 PM, hi_roger wrote:

hello, i want to ask a question about numpy.

i know how to select a submatrix using the slice object in numpy. But
how can i select a submatrix
A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
j1,j2,j3 , and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
The submatrix must share data memory with original matrix.

Any one help? thank you


Sturla is almost correct. What you really want is this:

i = [[i1], [i2], [i3]]
j = [[j1, j2, j3]]
B = A[i, j]

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer

If you have more numpy questions, please ask on the numpy mailing list.

http://www.scipy.org/Mailing_Lists


wow..., thank you all :)
but it seems it is impossible to make the submatrix share data with the
original matrix in pure numpy code ?


That is correct.

--
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list


Re: a question about numpy

2009-09-09 Thread Robert Kern

On 2009-09-08 20:45 PM, hi_roger wrote:

hello, i want to ask a question about numpy.

i know how to select a submatrix using the slice object in numpy. But
how can i select a submatrix
A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
j1,j2,j3 ,  and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
The submatrix must share data memory with original matrix.

Any one help? thank you


Sturla is almost correct. What you really want is this:

  i = [[i1], [i2], [i3]]
  j = [[j1, j2, j3]]
  B = A[i, j]

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer

If you have more numpy questions, please ask on the numpy mailing list.

  http://www.scipy.org/Mailing_Lists

--
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list


Re: a question about numpy

2009-09-09 Thread Robert Kern

On 2009-09-08 22:03 PM, sturlamolden wrote:

On 9 Sep, 03:45, hi_rogerrechardc...@gmail.com  wrote:


i know how to select a submatrix using the slice object in numpy. But
how can i select a submatrix
A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
j1,j2,j3 ,  and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )


You just pass an array of ints for each dimension. If you want a 3x3
submatrix, you must pass in two 3x3 arrays of indices.


Oops! I just responded to the OP saying that you were almost correct; but I 
was just thinking of your latter 1D example rather than this correct statement. 
My apologies.


--
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list


Re: a question about numpy

2009-09-09 Thread rechard

Robert Kern wrote:

On 2009-09-08 20:45 PM, hi_roger wrote:

hello, i want to ask a question about numpy.

i know how to select a submatrix using the slice object in numpy. But
how can i select a submatrix
A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
j1,j2,j3 ,  and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
The submatrix must share data memory with original matrix.

Any one help? thank you


Sturla is almost correct. What you really want is this:

  i = [[i1], [i2], [i3]]
  j = [[j1, j2, j3]]
  B = A[i, j]

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer

If you have more numpy questions, please ask on the numpy mailing list.

  http://www.scipy.org/Mailing_Lists


wow...,  thank you all :)
but it seems it is impossible to make the submatrix share data with the 
original matrix in pure numpy code ?

--
http://mail.python.org/mailman/listinfo/python-list


Re: a question about numpy

2009-09-08 Thread sturlamolden
On 9 Sep, 03:45, hi_roger rechardc...@gmail.com wrote:
 hello, i want to ask a question about numpy.

 i know how to select a submatrix using the slice object in numpy. But
 how can i select a submatrix
 A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
 j1,j2,j3 ,  and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
 The submatrix must share data memory with original matrix.

So the only way to do this is to make an ndarray subclass that
overloads __getitem__, __setitem__, and __iter__, and takes care of
the mapping into A. Thus you get a double indirection.






-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a question about numpy

2009-09-08 Thread sturlamolden
On 9 Sep, 03:45, hi_roger rechardc...@gmail.com wrote:

 i know how to select a submatrix using the slice object in numpy. But
 how can i select a submatrix
 A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
 j1,j2,j3 ,  and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )

You just pass an array of ints for each dimension. If you want a 3x3
submatrix, you must pass in two 3x3 arrays of indices.





 The submatrix must share data memory with original matrix.

That is the tricky part. An ndarray must be indexable using an array
of strides. That is in C:

void *get_element_ptr( PyArrayObject *a, int indices[])
{
   char *out = a-data;
   int d;
   for (d=0; d  a-nd; d++)
  out += indices[d] * a-strides[d];
   return (void *)out;
}

If you slice with an array or list of ints in Python, the resulting
array cannot be indexed like this. Therefore NumPy is forced to make a
copy. So if I do

 import numpy as np
 a = np.zeros((10,10))
 b = a[[1,3,5],[6,7,8]]

I get this:

 b.flags['OWNDATA']
True


But:

 c = a[::2,::2]
 c.flags['OWNDATA']
False

This is because C can still be indexed with strides as shown above,
and no copy is made.

















-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question about numpy

2006-08-25 Thread Paul Johnston
On Thu, 24 Aug 2006 17:23:49 GMT, Dennis Lee Bieber
[EMAIL PROTECTED] wrote:

On Thu, 24 Aug 2006 16:38:45 +0100, Paul Johnston
[EMAIL PROTECTED] declaimed the following in
comp.lang.python:

 I know its a long time since my degree but that's not matrix
 multiplication is it ?

   Define matrix multiplication...

   What you see appears to be multiplication of corresponding elements.

   Were you expecting a dot product, or a cross product, or something
else?


That as explained in
http://people.hofstra.edu/faculty/stefan_Waner/RealWorld/tutorialsf1/frames3_2.html

As I say its been a long time :-)

Thanks to everyone for the help.
Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question about numpy

2006-08-24 Thread Gabriel Genellina

At Thursday 24/8/2006 12:38, Paul Johnston wrote:


from numpy  import *

a = array([[1,2,3],[4,5,6],[1,2,3]])
b = array([[1,3,6],[2,5,1],[1,1,1]])
print a * b is \n, a * b

I know its a long time since my degree but that's not matrix
multiplication is it ?


No, it's plain element-by-element multiplication.
You want matrixmultiply: 
http://numpy.scipy.org/numpydoc/numpy-9.html#pgfId-36542




Gabriel Genellina
Softlab SRL 






__
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Newbie question about numpy

2006-08-24 Thread Robert Kern
Paul Johnston wrote:
 Hi I'm new to python and have just been taking a look at what it has
 to offer.
 I noted the lack of matrices so installed numpy

You will want to ask numpy questions on the numpy list.

   http://www.scipy.org/Mailing_Lists

numpy arrays are not matrices; they are arrays. All of the arithmetic 
operations 
on them are done element-wise. The dot() function will do matrix multiplication.

There is a matrix class (with the constructor numpy.mat(some_array)) that 
derives from arrays and overrides the * operator to do matrix multiplication if 
that is what you want. I prefer using dot() on regular arrays, myself.

-- 
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth.
   -- Umberto Eco

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question about numpy

2006-08-24 Thread Avell Diroll
Paul Johnston wrote:
(snip)
 I noted the lack of matrices so installed numpy
(snip)
 _
 from numpy  import *
 
 a = array([[1,2,3],[4,5,6],[1,2,3]])
 b = array([[1,3,6],[2,5,1],[1,1,1]])
(snip)
 print a * b is \n, a * b
 _
(snip)
 a * b is
 [[ 1  6 18]
  [ 8 25  6]
  [ 1  2  3]]
 _
 
 
 I know its a long time since my degree but that's not matrix
 multiplication is it ?

You consider that a and b are matrices, but for the python interpreter
they are arrays so a*b returns the multiplication of 2 arrays.

For matrices multiplication, you could get a hint by typing the
following in the interpreter :

 import numpy
 dir(numpy)
 help(numpy.matrixmultiply)#type q to exit

which could make you want to try the following code :

 from numpy  import *
 a = array([[1,2,3],[4,5,6],[1,2,3]])
 b = array([[1,3,6],[2,5,1],[1,1,1]])
 print matrixmultiply(a,b)

...
output :
...

array([[ 8, 16, 11],
   [20, 43, 35],
   [ 8, 16, 11]])
...

HIH,
avell

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question about numpy

2006-08-24 Thread Robert Kern
Avell Diroll wrote:

 For matrices multiplication, you could get a hint by typing the
 following in the interpreter :
 
 import numpy
 dir(numpy)
 help(numpy.matrixmultiply)#type q to exit

Note that the name matrixmultiply() has been deprecated in favor of dot() for 
many, many years now even in Numeric, numpy's predecessor. It has finally been 
removed in recent versions of numpy.

-- 
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth.
   -- Umberto Eco

-- 
http://mail.python.org/mailman/listinfo/python-list