<h4 dir="auto">Description</h4>

<p dir="auto">Setting the train_size to 50 does not result in the correct size of the train indices.</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sklearn.datasets import fetch_mldata
from sklearn.cross_validation import StratifiedShuffleSplit

def get_dataset():
    from sklearn.datasets import fetch_mldata
    mnist = fetch_mldata('MNIST original')
    # rescale the data, use the traditional train/test split
    X, y = mnist.data / 255., mnist.target
    test_size = 60000
    X_train, X_test = X[:test_size], X[60000:]
    y_train, y_test = y[:test_size], y[60000:]
    return (X_train, y_train, X_test, y_test)

X_train, y_train, X_test, y_test = get_dataset()
seed = 1337
n_iter = 1
train_size = 100

sss = StratifiedShuffleSplit(y_train.reshape(-1), n_iter,
                             train_size=train_size, random_state=seed)

for train_indices, test_indices in sss:
    print &quot;Train indices:&quot;, len(train_indices)
    print &quot;Test indices:&quot;, len(test_indices)
    assert(train_size == len(train_indices))"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">datasets</span> <span class="pl-k">import</span> <span class="pl-s1">fetch_mldata</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">cross_validation</span> <span class="pl-k">import</span> <span class="pl-v">StratifiedShuffleSplit</span>

<span class="pl-k">def</span> <span class="pl-en">get_dataset</span>():
    <span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">datasets</span> <span class="pl-k">import</span> <span class="pl-s1">fetch_mldata</span>
    <span class="pl-s1">mnist</span> <span class="pl-c1">=</span> <span class="pl-en">fetch_mldata</span>(<span class="pl-s">'MNIST original'</span>)
    <span class="pl-c"># rescale the data, use the traditional train/test split</span>
    <span class="pl-v">X</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">mnist</span>.<span class="pl-s1">data</span> <span class="pl-c1">/</span> <span class="pl-c1">255.</span>, <span class="pl-s1">mnist</span>.<span class="pl-s1">target</span>
    <span class="pl-s1">test_size</span> <span class="pl-c1">=</span> <span class="pl-c1">60000</span>
    <span class="pl-v">X_train</span>, <span class="pl-v">X_test</span> <span class="pl-c1">=</span> <span class="pl-v">X</span>[:<span class="pl-s1">test_size</span>], <span class="pl-v">X</span>[<span class="pl-c1">60000</span>:]
    <span class="pl-s1">y_train</span>, <span class="pl-s1">y_test</span> <span class="pl-c1">=</span> <span class="pl-s1">y</span>[:<span class="pl-s1">test_size</span>], <span class="pl-s1">y</span>[<span class="pl-c1">60000</span>:]
    <span class="pl-k">return</span> (<span class="pl-v">X_train</span>, <span class="pl-s1">y_train</span>, <span class="pl-v">X_test</span>, <span class="pl-s1">y_test</span>)

<span class="pl-v">X_train</span>, <span class="pl-s1">y_train</span>, <span class="pl-v">X_test</span>, <span class="pl-s1">y_test</span> <span class="pl-c1">=</span> <span class="pl-en">get_dataset</span>()
<span class="pl-s1">seed</span> <span class="pl-c1">=</span> <span class="pl-c1">1337</span>
<span class="pl-s1">n_iter</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>
<span class="pl-s1">train_size</span> <span class="pl-c1">=</span> <span class="pl-c1">100</span>

<span class="pl-s1">sss</span> <span class="pl-c1">=</span> <span class="pl-v">StratifiedShuffleSplit</span>(<span class="pl-s1">y_train</span>.<span class="pl-en">reshape</span>(<span class="pl-c1">-</span><span class="pl-c1">1</span>), <span class="pl-s1">n_iter</span>,
                             <span class="pl-s1">train_size</span><span class="pl-c1">=</span><span class="pl-s1">train_size</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-s1">seed</span>)

<span class="pl-k">for</span> <span class="pl-s1">train_indices</span>, <span class="pl-s1">test_indices</span> <span class="pl-c1">in</span> <span class="pl-s1">sss</span>:
    <span class="pl-k">print</span> <span class="pl-s">"Train indices:"</span>, <span class="pl-en">len</span>(<span class="pl-s1">train_indices</span>)
    <span class="pl-k">print</span> <span class="pl-s">"Test indices:"</span>, <span class="pl-en">len</span>(<span class="pl-s1">test_indices</span>)
    <span class="pl-k">assert</span>(<span class="pl-s1">train_size</span> <span class="pl-c1">==</span> <span class="pl-en">len</span>(<span class="pl-s1">train_indices</span>))</pre></div>
<h4 dir="auto">Expected Results</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Train indices: 50
Test indices: 6000"><pre class="notranslate"><code class="notranslate">Train indices: 50
Test indices: 6000
</code></pre></div>
<h4 dir="auto">Actual Results</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Train indices: 54000
Test indices: 6000
Traceback (most recent call last):
  File &quot;stratisfied_shuffle_split_bug.py&quot;, line 23, in &lt;module&gt;
    assert(50 == len(train_indices))
AssertionError"><pre class="notranslate"><code class="notranslate">Train indices: 54000
Test indices: 6000
Traceback (most recent call last):
  File "stratisfied_shuffle_split_bug.py", line 23, in &lt;module&gt;
    assert(50 == len(train_indices))
AssertionError
</code></pre></div>
<h4 dir="auto">Additional Information</h4>
<p dir="auto"><code class="notranslate">train_size = 50</code> seems to be a magic line. All other options I tested, like <code class="notranslate">train_size = 25</code> and <code class="notranslate">train_size = 100</code> result in the expected output.</p>
<h5 dir="auto">Setting <code class="notranslate">test_size</code> results in a 1 off error</h5>
<p dir="auto">Setting <code class="notranslate">test_size = 50</code> instead of the train_size results in:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Train indices: 59949
Test indices: 51"><pre class="notranslate"><code class="notranslate">Train indices: 59949
Test indices: 51
</code></pre></div>
<h4 dir="auto">Versions</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import platform; print(platform.platform())
Darwin-15.6.0-x86_64-i386-64bit
&gt;&gt;&gt; import sys; print(&quot;Python&quot;, sys.version)
('Python', '2.7.12 (default, Jun 29 2016, 14:05:02) \n[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]')
&gt;&gt;&gt; import numpy; print(&quot;NumPy&quot;, numpy.__version__)
('NumPy', '1.11.1')
&gt;&gt;&gt; import scipy; print(&quot;SciPy&quot;, scipy.__version__)
('SciPy', '0.18.0')
&gt;&gt;&gt; import sklearn; print(&quot;Scikit-Learn&quot;, sklearn.__version__)
('Scikit-Learn', '0.17.1')"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; import platform; print(platform.platform())
Darwin-15.6.0-x86_64-i386-64bit
&gt;&gt;&gt; import sys; print("Python", sys.version)
('Python', '2.7.12 (default, Jun 29 2016, 14:05:02) \n[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]')
&gt;&gt;&gt; import numpy; print("NumPy", numpy.__version__)
('NumPy', '1.11.1')
&gt;&gt;&gt; import scipy; print("SciPy", scipy.__version__)
('SciPy', '0.18.0')
&gt;&gt;&gt; import sklearn; print("Scikit-Learn", sklearn.__version__)
('Scikit-Learn', '0.17.1')
</code></pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="y = [0, 1, 2, 3] * 3 + [4, 5] * 5                                                                  
X = np.ones_like(y)                                                                                

splits = StratifiedShuffleSplit(n_iter=1, train_size=11, test_size=11, random_state=0)                                     

train, test = next(iter(splits.split(X=X, y=y)))                                                   

assert_array_equal(np.intersect1d(train, test), [])   
assert_equal(len(train), 11)
assert_equal(len(test), 11)"><pre class="notranslate"><span class="pl-s1">y</span> <span class="pl-c1">=</span> [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>] <span class="pl-c1">*</span> <span class="pl-c1">3</span> <span class="pl-c1">+</span> [<span class="pl-c1">4</span>, <span class="pl-c1">5</span>] <span class="pl-c1">*</span> <span class="pl-c1">5</span>                                                                  
<span class="pl-v">X</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">ones_like</span>(<span class="pl-s1">y</span>)                                                                                

<span class="pl-s1">splits</span> <span class="pl-c1">=</span> <span class="pl-v">StratifiedShuffleSplit</span>(<span class="pl-s1">n_iter</span><span class="pl-c1">=</span><span class="pl-c1">1</span>, <span class="pl-s1">train_size</span><span class="pl-c1">=</span><span class="pl-c1">11</span>, <span class="pl-s1">test_size</span><span class="pl-c1">=</span><span class="pl-c1">11</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">0</span>)                                     

<span class="pl-s1">train</span>, <span class="pl-s1">test</span> <span class="pl-c1">=</span> <span class="pl-en">next</span>(<span class="pl-en">iter</span>(<span class="pl-s1">splits</span>.<span class="pl-en">split</span>(<span class="pl-v">X</span><span class="pl-c1">=</span><span class="pl-v">X</span>, <span class="pl-s1">y</span><span class="pl-c1">=</span><span class="pl-s1">y</span>)))                                                   

<span class="pl-en">assert_array_equal</span>(<span class="pl-s1">np</span>.<span class="pl-en">intersect1d</span>(<span class="pl-s1">train</span>, <span class="pl-s1">test</span>), [])   
<span class="pl-en">assert_equal</span>(<span class="pl-en">len</span>(<span class="pl-s1">train</span>), <span class="pl-c1">11</span>)
<span class="pl-en">assert_equal</span>(<span class="pl-en">len</span>(<span class="pl-s1">test</span>), <span class="pl-c1">11</span>)</pre></div>
<blockquote>
<p dir="auto">raise self.failureException('12 != 11')</p>
</blockquote>
<p dir="auto">this is a follow-up on <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="134250379" data-permission-text="Title is private" data-url="https://github.com/scikit-learn/scikit-learn/issues/6379" data-hovercard-type="pull_request" data-hovercard-url="/scikit-learn/scikit-learn/pull/6379/hovercard" href="https://github.com/scikit-learn/scikit-learn/pull/6379">#6379</a> and a sign that the logic is too complex. I'm doing a rewrite to fix this bug but it is not as clean as I'd like.</p>