[SciPy-User] [SciPy-user] Avoiding inner for loops??

mdekauwe mdekauwe at gmail.com
Tue Aug 21 12:20:11 EDT 2012


Hi 

Apologies I didn't see this response as I was viewing on google groups and
on there it looks like no one has responded!! Odd.

Sort of. I already have a minimisation script set up using the lmfit
package. I was trying to sample the parameter space to find a decent
starting point for my initial parameter guess. 


J. David Lee wrote:
> 
> Hi Martin,
> 
> It looks like you are trying to do a brute force minimization of your 
> model with three parameters, is that right? For that you could look at 
> scipy.optimize.brute, which will probably be faster. scipy.optimize also 
> has several more sophisticated minimizers that you might  consider:
> 
> http://docs.scipy.org/doc/scipy/reference/optimize.html
> 
> If your function works with numpy arrays, you could try generating all 
> of your parameters at once with mgrid and calling your function on the 
> returned arrays,
> 
> p1, p2, p3 = np.mgrid[:10,:10,:10]
> 
> David
> 
> On 08/19/2012 04:07 AM, Martin De Kauwe 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
> 
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
> 
> 

-- 
View this message in context: http://old.nabble.com/Avoiding-inner-for-loops---tp34319763p34330080.html
Sent from the Scipy-User mailing list archive at Nabble.com.




More information about the SciPy-User mailing list