2013-02-01 5 views

Répondre

1

En fait, ce lambda est très efficace:

In [1]: unsquareform = lambda a: a[numpy.nonzero(numpy.triu(a))] 

Par exemple:

In [2]: scipy.spatial.distance.pdist(numpy.arange(12).reshape((4,3))) 
Out[2]: 
array([ 5.19615242, 10.39230485, 15.58845727, 5.19615242, 
     10.39230485, 5.19615242]) 

et

In [3]: unsquareform(scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(numpy.arange(12).reshape((4,3))))) 
Out[3]: 
array([ 5.19615242, 10.39230485, 15.58845727, 5.19615242, 
     10.39230485, 5.19615242])