[Numpy-discussion] Newbee question - looking to use Numpy to solve permutation problem

Robert Kern robert.kern at gmail.com
Sat Jan 27 22:12:24 EST 2007

Chris Toczycki wrote:
> I am looking to find a solution to a problem and believe that Numpy may
> help.  I'd appreciate some advice and/or direction and I'll pursue from
> there.
> The problem is that I am looking for a most efficient solution where I
> have to select 25 solutions where the highest pair-covariance value is
> the lowest of all possible 25 solution combinations.  There are possible
> 95 solutions to choose from and I have a 95 x 95 matrix of
> pair-covariance values.

I'll give you a brief sketch:

  * Write a function that will extract the upper-triangle (including the
diagonal) into a flattened array. a[triu(ones((len(a),len(a)))).astype(bool)] is
the one-liner.

  * Write a function that takes an index from this flattened array to the pair
of (i,j) indices that correspond to the same element in the original matrix.

  * Flatten your covariance matrix with the first function, use argsort() to
find the indices into the flattened array in ascending order, extract the first
25, and then use the second function to get the (i,j) pair that you are looking for.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list