title: How to get the set of features with the least sum of correlation in python?
body: <p>I have a <code>correlation matrix</code> which is a <code>pandas</code> dataframe that looks like this:</p>
  <pre><code>import pandas as pd
  foo = pd.DataFrame({'vars': ['col_a', 'col_b', 'col_c', 'col_d'],
                     'col_a': [1, 0.9, 0.04, 0.03],
                     'col_b': [0.9,1,0.05,0.03],
                     'col_c': [0.04, 0.05, 1, -0.04],
                     'col_d': [0.03, 0.03, -0.04,1]})
  </code></pre>
  <p>I would like to get a set of 3 (out of the 4) variables, that the sum of their absolute correlation is the minimum.</p>
  <p>My solution is:</p>
  <pre><code>import numpy as np
  import itertools
  
  test_cols = ['col_a', 'col_b', 'col_c', 'col_d']
  sum_l = {}
  for l in list(itertools.combinations(test_cols, 3)):
  #     print(l)
  #     print(list(itertools.combinations(l, 2)))
      sum_l2 = 0
      for l2 in list(itertools.combinations(l, 2)):
  #         print(l2)
  #         print(l2[0])
  #         print(l2[1])
          sum_l2 += np.abs(foo.query('vars == @l2[0]')[l2[1]].values[0])
      sum_l[l] = sum_l2
  print(sum_l)
  print(min(sum_l, key=sum_l.get))
  </code></pre>
  <p>Is there a way to do it more efficiently ? To avoid the <code>for</code> loops</p>