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

Chris Toczycki ctoczycki at yahoo.com
Sun Jan 28 07:39:41 CST 2007

```I believe that I didn't communicate my problem completely.  Your suggestion
to flatten the matrix should help develop a solution.  Now as I progress
down the sorted list I need to detect when I have 25 complete solution sets.

My goal is to find a set of 25 where the highest collective covariance
(within the 25 x 25 matrix) is the lowest.  My concern is that this problem
requires brut force permutation analysis.

Chris

-----Original Message-----
From: numpy-discussion-bounces at scipy.org
[mailto:numpy-discussion-bounces at scipy.org] On Behalf Of Robert Kern
Sent: Saturday, January 27, 2007 9:12 PM
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] Newbee question - looking to use Numpy to
solve permutation problem

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