mercredi 10 juin 2015

Fast Random Permutation of Binary Array

For my project, I wish to quickly generate random permutations of a binary array of fixed length and a given number of 1s and 0s. Given these random permutations, I wish to add them elementwise.

I am currently using numpy's ndarray object, which is convenient for adding elementwise. My current code is as follows:

    # n is the length of the array. I want to run this across a range of 
    # n=100 to n=1000.
    row = np.zeros(n)
    # m_list is a given list of integers. I am iterating over many possible
    # combinations of possible values for m in m_list. For example, m_list
    # could equal [5, 100, 201], for n = 500.
    for m in m_list: 
        row += np.random.permutation(np.concatenate([np.ones(m), np.zeros(n - m)]))

My question is, is there any faster way to do this? According to timeit, 1000000 calls of "np.random.permutation(np.concatenate([np.ones(m), np.zeros(n - m)]))" takes 49.6 seconds. For my program's purposes, I'd like to decrease this by an order of magnitude. Can anyone suggest a faster way to do this?

Thank you!




Aucun commentaire:

Enregistrer un commentaire