Collecting results from a loop returning NumPy arrays

I am admittedly a fairly simple Python programmer trying to learn when I encounter problems related to various research issues. And I hit one of these problems - in particular, how to handle loops, where I return a bunch of data, rather than the usual "exit from one number" examples, where you simply add the loop result to the previous one.

There is a Gist unlooped script here I'm trying to run: https://gist.github.com/1390355

In fact, the important point is the end of the model_solve function:

def model_solve(t): # lots of variables set params = np.zeroes((n_steps,n_params) params[:,0] = beta params[:,1] = gamma timer = np.arange(n_steps).reshape(n_steps,1) SIR = spi.odeint(eq_system, startPop, t_interval) output = np.hstack((timer,SIR,params)) return output 

This returns the results of the ODE integration bit ( spi.odeint ) along with the simple β€œWhat step are we now?” a timer and essentially two columns, the values ​​of two random variables are repeated many, many times in the form of an array of 4950 rows and 7 NumPy columns.

However, the goal is to run a Monte Carlo analysis of two parameters (beta and gamma) that have random values. Essentially, I want to create a function that will alternate like this:
 def loop_function(runs): for i in range(runs): model_solve(100) # output of those model_solves collected here # return collected output 

Then the collected output will be written to a file. Normally, I would simply use each model_solve function for its own results for the file, but this code will be run on PiCloud or another platform where I do not have the ability to write the file until the results are returned to the local computer. Instead, I am trying to get a huge NumPy array from the columns runs * 7 and 4950 rows, which can then be written to a file on my local machine.

Any tips on how to approach this?

+6
source share
2 answers

use list to save all results:

 results = [] for i in range(runs): results.append(model_solve(100)) 

then we get the output array:

 np.hstack(results) 
+10
source

In fact, if your code has a much larger loop, you should always try to vectorize your problem. If speed is important, you should know that β€œfor loops” is the neck of the bottle. In addition, the add operation is very slow and requires more memory because it creates copies. So, the best solution should be:

 results = [0]*runs # if you want to use lists... [model_solve(100) for x in results] # do see list comprehension in python 

Besides using a list, you can also use an array to store the results:

 resutls=np.zeros([numberOfRuns,ShapeOfModelResults]) for i in range(numberOfRuns): results[numberOfRuns,modelSolve(100)] # this will put the result directly in the matrix 

I hope my answer helps you write faster and more understandable code.

+8
source

Source: https://habr.com/ru/post/902268/


All Articles