欧联穆 發表於 2025-5-14 12:32:00

集成学习中的多样性密码:量化学习器的多样性

<p>在集成学习中,<strong>多样性</strong>是一个关键概念,简单来说,多样性衡量的是各个学习器之间的差异程度。</p>
<p>如果学习器之间差异很大,那么它们的组合就更有可能覆盖更多的情况,从而提高集成模型的性能,</p>
<p>就像足球队需要不同位置的球员配合一样。</p>
<p>下面介绍四种常用的多样性度量方法及其实现。</p>
<h1 id="1-不合度量disagreement-measure">1. 不合度量(Disagreement Measure)</h1>
<p><strong>不合度量</strong>是一种非常直观的多样性度量方法。</p>
<p>它通过计算两个学习器在样本上的预测结果不一致的比例来衡量它们之间的差异。</p>
<p>不合度量的值介于<strong>0到1之间</strong>,值越接近 <code>1</code>,说明两个学习器的预测结果越不一致,多样性越高。</p>
<p>假设我们有两个学习器$ L_1 <span class="math inline">\(和\)</span> L_2 <span class="math inline">\(,对于一个数据集中的每个样本\)</span> x_i <span class="math inline">\(,如果\)</span> L_1 <span class="math inline">\(和\)</span> L_2 $的预测结果不同,那么就认为它们在这个样本上<strong>“不合”</strong>。</p>
<p>不合度量就是这种<strong>“不合”</strong>情况的比例。</p>
<p>为了演示如何度量多样性,我们先创建两个不同的决策树模型:</p>
<pre><code class="language-python">import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

# 生成数据
X, y = make_classification(
    n_samples=100, n_features=20, n_informative=10, n_redundant=10, random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# 训练两个不同的决策树
clf1 = DecisionTreeClassifier(random_state=42)
clf2 = DecisionTreeClassifier(random_state=43)
clf1.fit(X_train, y_train)
clf2.fit(X_train, y_train)

# 预测测试集
y_pred1 = clf1.predict(X_test)
y_pred2 = clf2.predict(X_test)
</code></pre>
<p>然后,我们看看如何计算两个模型的<strong>不合度量</strong>(<code>Disagreement Measure</code>)。</p>
<pre><code class="language-python"># 计算不合度量
disagreement = np.mean(y_pred1 != y_pred2)
print(f"不合度量: {disagreement}")

## 输出结果:
# 不合度量: 0.2
</code></pre>
<h1 id="2-相关系数correlation-coefficient">2. 相关系数(Correlation Coefficient)</h1>
<p><strong>相关系数</strong>衡量的是两个学习器预测结果之间的<strong>线性相关性</strong>。</p>
<p>如果两个学习器的预测结果完全一致,相关系数为<code>1</code>;</p>
<p>如果完全相反,相关系数为<code>-1</code>;</p>
<p>如果两者之间没有线性关系,相关系数接近 <code>0</code>。</p>
<p>在集成学习中,我们希望相关系数<strong>越低越好</strong>,因为这意味着学习器之间的差异更大。</p>
<p><strong>相关系数</strong>是通过计算两个学习器预测结果的协方差与它们各自的标准差的比值来得到的。</p>
<p>其公式为:$ \rho=\frac{\text{Cov}(L_1,L_2)}{\sigma{L_1}\sigma{L_2}} $</p>
<p>其中,$ Cov(L_1,L_2) <span class="math inline">\(是协方差,\)</span> \sigma{L_1} <span class="math inline">\(和\)</span> \sigma{L_2} <span class="math inline">\(分别是\)</span> L_1 <span class="math inline">\(和\)</span> L_2 $的标准差。</p>
<p>使用代码来计算<strong>相关系数</strong>也很简单,同样使用上面创建的两个决策树模型。</p>
<pre><code class="language-python">import numpy as np

# 计算相关系数
correlation = np.corrcoef(y_pred1, y_pred2)
print(f"相关系数: {correlation}")

## 输出结果:
# 相关系数: 0.6637465183030644
</code></pre>
<h1 id="3-q-统计量q-statistic">3. Q-统计量(Q-Statistic)</h1>
<p><strong>Q-统计量</strong>是另一种衡量学习器之间一致性和差异性的指标。</p>
<p>它通过比较两个学习器在样本上的预测结果来计算。</p>
<p><strong>Q-统计量</strong>的值介于<strong>-1 到 1 之间</strong>,值越接近 <code>1</code>,说明两个学习器的预测结果越一致;</p>
<p>值越接近<code>-1</code>,说明它们的预测结果越不一致。</p>
<p><strong>Q-统计量</strong>的计算公式为:$ Q=\frac{a\times d-b\times c}{(a+b)\times(c+d)} $</p>
<p>其中:</p>
<ul>
<li>$ a $是两个学习器都正确预测的样本数</li>
<li>$ b $是第一个学习器正确预测,第二个学习器错误预测的样本数</li>
<li>$ c $是第一个学习器错误预测,第二个学习器正确预测的样本数</li>
<li>$ d $是两个学习器都错误预测的样本数</li>
</ul>
<p>计算<strong>Q-统计量</strong>的示例代码如下:</p>
<pre><code class="language-python">from sklearn.metrics import confusion_matrix

# 计算混淆矩阵
cm = confusion_matrix(y_test, y_pred1, labels=)
a = np.sum((y_pred1 == y_test) &amp; (y_pred2 == y_test))
b = np.sum((y_pred1 == y_test) &amp; (y_pred2 != y_test))
c = np.sum((y_pred1 != y_test) &amp; (y_pred2 == y_test))
d = np.sum((y_pred1 != y_test) &amp; (y_pred2 != y_test))

# 计算 Q-统计量
q_statistic = (a * d - b * c) / ((a + b) * (c + d))
print(f"Q-统计量: {q_statistic}")

## 输出结果:
# Q-统计量: 0.5757575757575758
</code></pre>
<h1 id="4-k-统计量kappa-statistic">4. k-统计量(Kappa Statistic)</h1>
<p><strong>k-统计量</strong>是一种衡量分类器性能的指标,它考虑了分类器的正确率和随机猜测的正确率。</p>
<p>在集成学习中,<strong>k-统计量</strong>可以用来衡量两个学习器之间的多样性。</p>
<p><strong>k-统计量</strong>的值介于<strong>-1 到 1 之间</strong>,值越接近 <code>1</code>,说明两个学习器的预测结果越一致;</p>
<p>值越接近<code>-1</code>,说明它们的预测结果越不一致。</p>
<p><strong>k-统计量</strong>的计算公式为:$ \kappa=\frac{P_o-P_e}{1-P_e} $</p>
<p>其中:</p>
<ul>
<li>$ P_o $是两个学习器预测结果一致的比例</li>
<li>$ P_e $是随机猜测下两个学习器预测结果一致的比例</li>
</ul>
<p>计算<strong>k统计量</strong>的示例代码如下:</p>
<pre><code class="language-python">from sklearn.metrics import cohen_kappa_score

# 计算 k-统计量
kappa_statistic = cohen_kappa_score(y_pred1, y_pred2)
print(f"Kappa 统计量: {kappa_statistic}")

## 输出结果:
# Kappa 统计量: 0.6116504854368932
</code></pre>
<h1 id="5-指标使用建议">5. 指标使用建议</h1>
<p>在实际场景中,我们应该如何使用这些指标呢?下面是一些使用建议:</p>
<ol>
<li>在<code>Bagging</code>中优先关注不合度量</li>
<li>构建<code>Stacking</code>时参考相关系数</li>
<li>使用<code>Q-statistic</code>诊断高相关模型</li>
<li><code>Kappa</code>值过低时考虑增加模型类型差异</li>
</ol>
<p>各个指标的数值可参考:</p>
<table>
<thead>
<tr>
<th>指标</th>
<th>理想范围</th>
<th>应用场景</th>
</tr>
</thead>
<tbody>
<tr>
<td>不合度量</td>
<td>0.3-0.7</td>
<td>快速评估模型差异度</td>
</tr>
<tr>
<td>相关系数</td>
<td>[-0.5, 0.5]</td>
<td>分析模型互补性</td>
</tr>
<tr>
<td>Q-统计量</td>
<td>(-0.5, 0.5)</td>
<td>研究预测结果关联强度</td>
</tr>
<tr>
<td>Kappa统计量</td>
<td>&lt;0.4</td>
<td>综合评估一致性水平</td>
</tr>
</tbody>
</table>
<h1 id="6-总结">6. 总结</h1>
<p>在集成学习中,度量学习器的<strong>多样性</strong>是非常重要的。</p>
<p>通过计算<strong>不合度量</strong>、<strong>相关系数</strong>、<strong>Q-统计量</strong>和 <strong>k-统计量</strong>,我们可以更好地了解学习器之间的差异。</p>
<p>这些指标可以帮助我们选择合适的集成策略,从而提高集成模型的性能。</p>
<p>在实际应用中,可以根据具体问题选择合适的多样性度量指标,并结合<code>scikit-learn</code>等工具进行计算和分析。</p><br><br>
来源:https://www.cnblogs.com/wang_yb/p/18875672
頁: [1]
查看完整版本: 集成学习中的多样性密码:量化学习器的多样性