IndexError : index out of bounds
<p>I have implemented MultinomialNB but I get this message. Please help me to solve it. Here my code : </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>kf = KFold(len(X), n_folds=2, shuffle=True, random_state=9999)
model_train_index = []
model_test_index = []
model = 0

for k, (index_train, index_test) in enumerate(kf):
    X_train, X_test, y_train, y_test = X.ix[index_train,:], X.ix[index_test,:],y[index_train], y[index_test]
    clf = MultinomialNB(alpha=0.1).fit(X_train, y_train)
    score = clf.score(X_test, y_test)
    f1score = f1_score(y_test, clf.predict(X_test))
    precision = precision_score(y_test, clf.predict(X_test))
    recall = recall_score(y_test, clf.predict(X_test))
    print('Model %d has accuracy %f with | f1score: %f | precision: %f | recall : %f'%(k,score, f1score, precision, recall))
    model_train_index.append(index_train)
    model_test_index.append(index_test)
    model+=1</code></pre>
</div>
</div>
</p>

<p>and then I get result like this : </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>IndexError                                Traceback (most recent call last)
&lt;ipython-input-3-df0b24edb687&gt; in &lt;module&gt;()
      5 
      6 for k, (index_train, index_test) in enumerate(kf):
----&gt; 7     X_train, X_test, y_train, y_test = X.ix[index_train,:], X.ix[index_test,:],y[index_train], y[index_test]
      8     clf = MultinomialNB(alpha=0.1).fit(X_train, y_train)
      9     score = clf.score(X_test, y_test)

IndexError: index 100 is out of bounds for axis 0 with size 100</code></pre>
</div>
</div>
</p>

<p>Python uses zero based indexing so if the zeroth dimension of <code>X.ix[index_train,:]</code> or <code>y[index_train]</code> is 100, the maximum value of <code>index_train</code> that would be valid is 99. Likewise for <code>index_test</code>.</p>

<p>Something in</p>

<pre><code>kf = KFold(len(X), n_folds=2, shuffle=True, random_state=9999)
</code></pre>

<p>is causing one of those indices to be too large for one of those arrays at the time you enumerate(kf).</p>

python function