def corrsort(features, use_tsp=False):
"""
Given a features array, one feature per axis=0 entry, return axis=0 indices
such that adjacent indices correspond to features that are correlated.
cf. Traveling Salesman Problem. Not an optimal solution.
use_tsp:
Use tsp solver. See tsp_solver.greedy module that is used for this.
Slows run-time considerably: O(N^4) computation, O(N^2) memory.
Without use_tsp, both computation and memory are O(N^2).
"""
correlations = np.ma.corrcoef(arrays.plane(features))
if use_tsp: return tsp.solve_tsp(-correlations)
size = features.shape[0]
correlations.mask[np.diag_indices(size)] = True
# initialize results with the pair with the highest correlations.
largest = np.argmax(correlations)
results = [int(largest / size), largest % size]
correlations.mask[:, results[0]] = True
while len(results) < size:
correlations.mask[:, results[-1]] = True
results.append(np.argmax(correlations[results[-1]]))
return results
评论列表
文章目录