<p>@Ken Syme已经有了一个很好的答案。我只是想补充一点。</p>
<ul>
<li><strong><code>StratifiedKFold</code></strong>是<code>KFold</code>的变体。首先,<code>StratifiedKFold</code>洗牌数据,然后将数据分成<code>n_splits</code>部分并完成。
现在,它将使用每个部分作为测试集。注意<strong>它只会在拆分前对数据进行一次洗牌。</li>
</ul>
<p>使用<code>shuffle = True</code>,数据将被<code>random_state</code>洗牌。否则,
数据被<code>np.random</code>(作为默认值)洗牌。
例如,使用<code>n_splits = 4</code>,数据有3个类(label)用于<code>y</code>(因变量)。4个测试集覆盖所有数据,没有任何重叠。</p>
<p><a href="https://i.stack.imgur.com/XJZve.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XJZve.png" alt="enter image description here"/></a></p>
<ul>
<li>另一方面,<strong><code>StratifiedShuffleSplit</code></strong>是<code>ShuffleSplit</code>的变体。
首先,<code>StratifiedShuffleSplit</code>洗牌您的数据,然后它还将数据分成<code>n_splits</code>部分。但是,还没有完成。在这一步之后,<code>StratifiedShuffleSplit</code>选择一个部分作为测试集。
然后它重复相同的进程<code>n_splits - 1</code>其他时间,以获得<code>n_splits - 1</code>其他测试集。看看下面的图片,数据是一样的,但是这次,4个测试集并没有覆盖所有的数据,即测试集之间有重叠。</li>
</ul>
<p><a href="https://i.stack.imgur.com/AGv9B.png" rel="noreferrer"><img src="https://i.stack.imgur.com/AGv9B.png" alt="enter image description here"/></a></p>
<p>因此,这里的区别是<code>StratifiedKFold</code><strong>只进行一次洗牌和拆分,因此测试集不会重叠</strong>,而<code>StratifiedShuffleSplit</code><strong>在每次拆分前都进行洗牌,并且它会进行<code>n_splits</code>次拆分,测试集可以重叠</strong>。</p>
<ul>
<li><strong>注意</strong>:这两种方法使用“分层折叠”(这就是“分层”出现在两个名称中的原因)。这意味着每个部分都保留与原始数据相同百分比的每个类(标签)的样本。你可以在<a href="http://scikit-learn.org/stable/modules/cross_validation.html#cross-validation" rel="noreferrer">cross_validation documents</a>上阅读更多内容</li>
</ul>