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


A question about numpy

2015-04-14 Thread Paulo da Silva
I am new to numpy ...

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.

I found 2 ways, but not sure if they are the best solution:

1.
r1=v1.copy()
r2=v2.copy()
r1[r1k]=0
r2[r2=k]=0
r=r1+r2

2.
r=v1*(v1=k)+v2*(v2k)

Are there any better sols.?

Thanks for help/comments.
-- 
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: question about numpy, subclassing, and a DeprecationWarning

2012-06-27 Thread Robert Kern

On 6/27/12 10:02 PM, Jason Swails wrote:

Hello,

I'm running into an unexpected issue in a program I'm writing, and I was hoping
someone could provide some clarification for me.  I'm trying to subclass
numpy.ndarray (basically create a class to handle a 3D grid).  When I
instantiate a numpy.ndarray, everything works as expected.  When I call
numpy.ndarray's constructor directly within my subclass, I get a deprecation
warning about object.__init__ not taking arguments.  Presumably this means that
ndarray's __init__ is somehow (for some reason?) calling object's __init__...

This is some sample code:

  import numpy as np
  class derived(np.ndarray):
... def __init__(self, stuff):
... np.ndarray.__init__(self, stuff)
...
  l = derived((2,3))
__main__:3: DeprecationWarning: object.__init__() takes no parameters
  l
derived([[  8.87744455e+159,   6.42896975e-109,   5.56218818e+180],
[  1.79996515e+219,   2.41625066e+198,   5.15855295e+307]])
 

Am I doing something blatantly stupid?  Is there a better way of going about
this?  I suppose I could create a normal class and just put the grid points in a
ndarray as an attribute to the class, but I would rather subclass ndarray
directly (not sure I have a good reason for it, though).  Suggestions on what I
should do?


numpy.ndarray does not have its own __init__(), just a __new__(). It's 
__init__() is the same as object.__init__(), which takes no arguments.


[~]
|3 np.ndarray.__init__ is object.__init__
True

There is no need to call np.ndarray.__init__() explicitly.


http://docs.scipy.org/doc/numpy/user/basics.subclassing.html#a-brief-python-primer-on-new-and-init

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

  http://www.scipy.org/Mailing_Lists

Personally, I recommend not subclassing ndarray at all. It rarely works out 
well.

--
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-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


a question about numpy

2009-09-08 Thread hi_roger
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
-- 
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


Newbie question about numpy

2006-08-24 Thread Paul Johnston
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
I know the documentation is chargable so wanted a quick play to see if
I should buy it 
However 

_
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 = \n',a,\n
print 'b = \n',b,\n

print 'a has shape ', a.shape
print 'b has shape ', b.shape, \n

print a * b is \n, a * b
_

Gives me 
_
a =
[[1 2 3]
 [4 5 6]
 [1 2 3]] 

b =
[[1 3 6]
 [2 5 1]
 [1 1 1]] 

a has shape  (3, 3)
b has shape  (3, 3) 

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 ?

TIA 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