[SciPy-User] 3D array problem in Python
Thøger Rivera-Thorsen
thoger.emil@gmail....
Sun Dec 30 11:03:09 CST 2012
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@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@scipy.org <sentmsg?mailto=mailto%3aSciPy%2dUser@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/a04be7a7/attachment-0001.html
More information about the SciPy-User
mailing list