mercredi 28 janvier 2015

numpy random numpers in specified shape of any complexity

def shaperand(s): r = [] for i in s: if type(i) in [list,tuple]: r.append(shaperand(i)) else: r.append(np.random.rand()) return r


So,


shaperand([[1,2],[3,4,5],[6,7]])


results in:


[[0.27611814857329864, 0.6271028191307862], [0.6245245446787084, 0.743259931401167, 0.9061663248784034], [0.7236900927531255, 0.540622773908648]]


I didn't see a function to do this. If there's one it'd be faster. Is there a better, more nifty way to write this?





Aucun commentaire:

Enregistrer un commentaire