[SciPy-user] Avoiding inner for loops

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-user] Avoiding inner for loops

mdekauwe

Hi,

I need to avoid (at least) two inner for loops in what I am trying to do
otherwise my processing takes forever. What is the best way to transfer what
I am doing into a more "numpy way"? Essentially I am trying to call a model
again for various different parameter combinations. The example is
fictional, by the grid_size would ideally grow > 500 and by doing so the
processing speed becomes very slow the way I have set things up..

thanks.

example.


import numpy as np

def fake_model(data1, data2, p1, p2, p3):
    """ complete nonsense """
    return data1 + data2 * p1 * p2 * p3

data1 = np.random.rand(10) # the size of this arrays varies might be 10
might be 15 etc
data2 = np.random.rand(10) # the size of this arrays varies might be 10
might be 15 etc
obs = np.random.rand(10) # the size of this arrays varies might be 10 might
be 15 etc

grid_size = 10 # Ideally this would be a large number
param1 = np.linspace(5.0, 350, grid_size)
param2 = np.linspace(5.0, 550, grid_size)
param3 = np.linspace(1E-8, 10.5, grid_size)
ss = np.zeros(0)

for p1 in param1:
    for p2 in param2:
        for p3 in param3:
            ans = fake_model(data1, data2, p1, p2, p3)
           
            ss = np.append(ss, np.sum(obs - ans)**2)
            print np.sum(obs - ans)**2
--
View this message in context: http://old.nabble.com/Avoiding-inner-for-loops-tp34319609p34319609.html
Sent from the Scipy-User mailing list archive at Nabble.com.

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: [SciPy-user] Avoiding inner for loops

Issa Karambal-2
Hi,
This will help. You do not need to have  'fake_model' function

ss = []
params = param1*param2*param3
ans = data1+data2*params[:, None]
for i in range(0,len(ans)):
     ss.append( np.sum( obs-ans[i] )**2 )

On 19 August 2012 08:56, mdekauwe <[hidden email]> wrote:

Hi,

I need to avoid (at least) two inner for loops in what I am trying to do
otherwise my processing takes forever. What is the best way to transfer what
I am doing into a more "numpy way"? Essentially I am trying to call a model
again for various different parameter combinations. The example is
fictional, by the grid_size would ideally grow > 500 and by doing so the
processing speed becomes very slow the way I have set things up..

thanks.

example.


import numpy as np

def fake_model(data1, data2, p1, p2, p3):
    """ complete nonsense """
    return data1 + data2 * p1 * p2 * p3

data1 = np.random.rand(10) # the size of this arrays varies might be 10
might be 15 etc
data2 = np.random.rand(10) # the size of this arrays varies might be 10
might be 15 etc
obs = np.random.rand(10) # the size of this arrays varies might be 10 might
be 15 etc

grid_size = 10 # Ideally this would be a large number
param1 = np.linspace(5.0, 350, grid_size)
param2 = np.linspace(5.0, 550, grid_size)
param3 = np.linspace(1E-8, 10.5, grid_size)
ss = np.zeros(0)

for p1 in param1:
    for p2 in param2:
        for p3 in param3:
            ans = fake_model(data1, data2, p1, p2, p3)

            ss = np.append(ss, np.sum(obs - ans)**2)
            print np.sum(obs - ans)**2
--
View this message in context: http://old.nabble.com/Avoiding-inner-for-loops-tp34319609p34319609.html
Sent from the Scipy-User mailing list archive at Nabble.com.

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: [SciPy-user] Avoiding inner for loops

Issa Karambal-2
If you do not want to use any loop, you might try this


params = param1*param2*param3
ans = data1+data2*params[:, None]
ss = np.sum(obs - ans, axis=1)**2

On 19 August 2012 20:36, Issa Karambal <[hidden email]> wrote:
Hi,
This will help. You do not need to have  'fake_model' function

ss = []
params = param1*param2*param3
ans = data1+data2*params[:, None]
for i in range(0,len(ans)):
     ss.append( np.sum( obs-ans[i] )**2 )


On 19 August 2012 08:56, mdekauwe <[hidden email]> wrote:

Hi,

I need to avoid (at least) two inner for loops in what I am trying to do
otherwise my processing takes forever. What is the best way to transfer what
I am doing into a more "numpy way"? Essentially I am trying to call a model
again for various different parameter combinations. The example is
fictional, by the grid_size would ideally grow > 500 and by doing so the
processing speed becomes very slow the way I have set things up..

thanks.

example.


import numpy as np

def fake_model(data1, data2, p1, p2, p3):
    """ complete nonsense """
    return data1 + data2 * p1 * p2 * p3

data1 = np.random.rand(10) # the size of this arrays varies might be 10
might be 15 etc
data2 = np.random.rand(10) # the size of this arrays varies might be 10
might be 15 etc
obs = np.random.rand(10) # the size of this arrays varies might be 10 might
be 15 etc

grid_size = 10 # Ideally this would be a large number
param1 = np.linspace(5.0, 350, grid_size)
param2 = np.linspace(5.0, 550, grid_size)
param3 = np.linspace(1E-8, 10.5, grid_size)
ss = np.zeros(0)

for p1 in param1:
    for p2 in param2:
        for p3 in param3:
            ans = fake_model(data1, data2, p1, p2, p3)

            ss = np.append(ss, np.sum(obs - ans)**2)
            print np.sum(obs - ans)**2
--
View this message in context: http://old.nabble.com/Avoiding-inner-for-loops-tp34319609p34319609.html
Sent from the Scipy-User mailing list archive at Nabble.com.

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user