[SciPy-User] 3D array problem in Python

Thøger Rivera-Thorsen thoger.emil at gmail.com
Sun Dec 30 14:29:42 EST 2012


On 12/30/2012 06:46 PM, Happyman wrote:
> Great thanks Emil
>
> because of being in a new step in python may be I am not realizing the 
> "advantages" of it!!!
>
>
> I am going to try to do so according to your advice hopefully.
>
> What I wanted to explain by showing the function which you did not get 
> is I have two separately independent args (arg1, arg2) which contains 
> arrays, one and 3 dimensional respectively..
>
> arg1=A -> 1-Dimen.
> arg2=B -> 3 Dimen.
>
> def F(arg1, arg2):
>         # this is just example we can put any function or process here!!
>       return array(value)  # as an example
>
> I wanted to get faster result by avoiding loops inside of function 
> here...it seems really (of course for me) confusing...to me. I tried 
> but could not do it!
>

I think the answer depends on the specific operations you want the 
function to do. As an example, you could imagine that A is a (25,) 1D 
array, and B is a (33, 25) 2D array. Remember,m the latter means 33 
rows, 25 columns. Let's say we want to add A to each of the rows of B. 
We could of course loop, but we don't want to do that. Instead we can 
create a (33, 25) array where each row is A, and then add this array to B:

import numpy as np

A = np.random.random(25)
B = np.ones((33, 25))

def F(Arg1, Arg2):

     # We want it to be a 2D array so we can define if it's a row or a column.
     #  Default is that it's a row:
     C = np.array(Arg1, ndmin=2)
     
     print C.shape            # Just to see it
     C =C.repeat(33, axis=0)  # Stack 33 rows on top of each other
     out = B + C              # Now, element-wise addition.
     return out

my_sum = F(A, B)
print my_sum

Hope this helps...
A shortcut to avoid some of these steps is to define A as a row or 
column explicitly:

A = np.random.random((1, 25))  # Or ((25, 1)) if you want it to be a column.


Then you can just A.transpose() or A.T  if you need that.

Try googling "Numpy for matlab users" and "NumPy for IDL users", even if 
you don't use matlab or IDL. They have some pretty good list of basic 
array handling functionality (and NumPy is designed to take you a very 
long way with basic functionality).

/Emil



>
> Воскресенье, 30 декабря 2012, 18:03 +01:00 от Thøger 
> Rivera-Thorsen<thoger.emil at gmail.com>:
>
>     Oops, we should probably reply to the list again;
>
>     On 12/30/2012 05:16 PM, Happyman wrote:
>>     Hi
>>
>>     Thanks for your answer... I also tried to do so unfortunately I
>>     could not manage that because of no very good experience in Python...
>>     The way you propose is really good indeed but how can i do it???
>>
>>     Two huge problems I always encounter in Python are:
>>     1) Array with scalars...always!!!!!
>>     2) for loop      I can understand Python is script language but
>>     how to deal with it???
>>
>
>     The idea behind NumPy  is exactly that it performs element-wise
>     operations on arrays of N dimensions. Loops should be reserved
>     only to the cases where an operation on an element depends on the
>     state of other elements.
>     To perform an element-wise operation on two arrays, they need to
>     be of the same dimensions (and the same orientation - that is, a
>     size (3, 6, 3) is not compatible with a size (3, 3, 6) until
>     you've transposed it)
>
>     But suppose you have two arrays:
>
>     import numpy as np
>
>     A = np.array([1., 2., 3., 4., 5., 6., 7., 8., 9., 19., 11., 12.]).reshape((3, 4))
>
>     B = np.random.random(A.shape)
>
>     C = A + B
>
>     D = A**B
>
>     But if you change the orientation of one, it goes wrong:
>     C*A.transpose() will not work.
>
>     And you can perform operations on only elements in an array that
>     meet some logical condition, for example:
>
>     E = np.zeros_like(A)  # Just to create the array
>
>     E[B<0.5] = A[B<0.5]
>
>     E[B>0.5] = C[B>0.5]
>
>     Or simply:
>
>     B[B>5] += 1.
>
>     Note, again, tyhat there is *no* looping going on here.
>
>>     Especially, when I create some function in which arguments can
>>     take one value are messed up with negotiation wit outer coming
>>     variables into my function!!
>>     for example,
>>     arg1, arg2 are arrays!
>>     def f( arg1, arg2 ):
>>            some process, loops everthing
>>        return val
>>
>     I am not sure I understand the last question; what exactly is it
>     that goes wrong?
>
>     Cheers,
>     Emil
>
>
>>
>>
>>
>>
>>     Воскресенье, 30 декабря 2012, 16:47 +01:00 от Thøger
>>     Rivera-Thorsen <thoger.emil at gmail.com>
>>     <sentmsg?mailto=mailto%3athoger.emil at gmail.com>:
>>
>>         Use np.where() or logical indexing (same thing, really) to
>>         mask your array, then perform the operations. Let's say your
>>         array is called A:
>>
>>         A[float(A) == 0.0] = 0.0
>>
>>         A[float(A) != 0.0] = [...etc.]
>>
>>
>>         This, of course, only works if the operation for an entry
>>         doesn't depend on other entries in the array; but it should
>>         give you a great speed gain.
>>
>>         Cheers;
>>
>>         Emil
>>
>>
>>
>>         On 12/30/2012 04:32 AM, Happyman wrote:
>>>         Hello
>>>
>>>         I have 3 dimensional array  which I want  to calculate in a
>>>         huge process. Everything is working well if I use ordinary
>>>         way which is unsuitable in Python like the following:
>>>
>>>         nums=32
>>>         rows=120
>>>         cols=150
>>>
>>>         for k in range(0,nums):
>>>         for i in range(0,rows):
>>>                    for j in range(0,cols):
>>>                                     if float ( R[ k ] [ i ] [ j ] )
>>>         == 0.0:
>>>          val11 [ i ] =0.0
>>>                                     else:
>>>          val11[ i ] [ j ], val22[ i ][ j ] = integrate.quad( lambda
>>>         x :  F1(x)*F2(x) , 0 , pi)
>>>
>>>         But, this calculation takes so long time, let's say about  1
>>>         hour (theoretically)... Is there any better way to easily
>>>         and fast calculate the process such as [ F( i ) for i in
>>>         xlist ] or something like that rather than using for loop?
>>>
>>>
>>>
>>>
>>>         _______________________________________________
>>>         SciPy-User mailing list
>>>         SciPy-User at scipy.org
>>>         http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20121230/769763f5/attachment.html>


More information about the SciPy-User mailing list