<p>不是对最初问题的回答,但考虑到讨论,这可能是有用的。在</p>
<p>目标是找到列的组合,使列和与<code>OGT</code>具有最大的相关性。这很容易,因为协方差是双线性的:</p>
<ul>
<li><code>cov(OGT, A+B) = cov(OGT, A) + cov(OGT, B)</code>。在</li>
</ul>
<p>我依靠两个简化的假设:</p>
<ol>
<li>因素A、B、C等是独立的。在</li>
<li>物种的权重相等。在</li>
<li>每个因子的方差是<code>1</code>。在</li>
</ol>
<p>想法是:</p>
<ol>
<li>规范化所有列,使其具有单位方差(即假设3)。在</li>
<li>计算每列OGT的协方差。在</li>
<li>按协方差递减的顺序对因子A、B、C进行排序。最佳组合将作为这种安排的前缀出现。在</li>
<li>我们应该选择哪个前缀?与标准差之和最大的那个。由于第1步的规范化,对于大小为n的前缀,每个前缀之和的标准偏差仅为sqrt(n),因此需要在序列中找到一个最大索引,这很容易。在</li>
</ol>
<p>这可能比检查所有可能的组合要快一点。在</p>
<hr/>
<pre><code>import pandas as pd
import numpy as np
# set up fake data
import string
df = pd.DataFrame(np.random.rand(3, 26), columns=list(string.ascii_uppercase))
df["species"] = ["dog", "cat", "human"]
df["OGT"] = np.random.randint(0, 100, 3)
df = df.set_index("species")
# actual work
alpha_cols = list(string.ascii_uppercase)
# normalize standard deviations of each column
df = df[alpha_cols + ["OGT"]].div(df.std(0), axis=1)
# compute correlations (= covariances) of OGT with each column
corrs = df.corrwith(df.OGT).sort_values(ascending=False)
del corrs["OGT"]
# sort covariances in order from the greatest to the smallest
# compute cumulative sums
# divide by standard deviation of a group (i.e. sqrt(n) at index n-1)
cutoff = (corrs.cumsum() / np.sqrt(np.arange(corrs.shape[0]) + 1)).idxmax()
answer = sorted(corrs.loc[:cutoff].index.values)
print(answer)
# e.g.
# ['B', 'I', 'K', 'O', 'Q', 'S', 'U', 'V', 'Y']
</code></pre>