Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collecting results from a loop that returns NumPy Arrays

Tags:

python

numpy

I'm an admittedly pretty basic Python programmer, trying to learn as I encounter problems implementing various research problems. And I've hit one of those problems - particularly, how to handle loops where I'm returning a bunch of data, rather than the usual "out comes a single number" examples where you just add the result of the loop to everything previous.

Here's a Gist of the unlooped script I'm trying to run: https://gist.github.com/1390355

The really salient 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

That returns the results of the ODE integration bit (spi.odeint) along with a simple "What time step are we on?" timer and essentially two columns of the value of two random variables repeated many, many times in the form of 4950 row and 7 column NumPy array.

The goal however is to run a Monte Carlo analysis of the two parameters (beta and gamma) which have random values. Essentially, I want to make a function that loops somewhat like so:

def loop_function(runs):
  for i in range(runs):
    model_solve(100)
    # output of those model_solves collected here
  # return collected output

That collected output would then be written to a file. Normally, I'd just have each model_solve function write its results to a file, but this code is going to be run on PiCloud or another platform where I don't necessarily have the ability to write a file until the results are returned to the local machine. Instead, I'm trying to get a return of a huge NumPy array of runs*7 columns and 4950 rows - which can then be written to a file on my local machine.

Any clues as to how to approach this?

like image 732
Fomite Avatar asked Nov 24 '11 01:11

Fomite


People also ask

Can you iterate through NumPy array?

Iterating means going through elements one by one. As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python. If we iterate on a 1-D array it will go through each element one by one.

What is __ Array_interface __?

__array_interface__ A dictionary of items (3 required and 5 optional). The optional keys in the dictionary have implied defaults if they are not provided. The keys are: shape (required) Tuple whose elements are the array size in each dimension.

What is Vstack and Hstack in Python?

hstack combines NumPy arrays horizontally and np. vstack combines arrays vertically. Other than that though, the syntax and behavior is very similar.


1 Answers

use a list to save all the results:

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

then get the output array by:

np.hstack(results)
like image 154
HYRY Avatar answered Sep 19 '22 18:09

HYRY