IndexError:索引超出范围

时间:2016-08-28 23:43:34

标签: python function

我已经实现了MultinomialNB,但是我收到了这条消息。请帮我解决一下。这是我的代码:

// pseudo code:
var all_text = File.ReadAllLines("doc.txt"); 
bool b = false;
for (var s in all_text)
{
   // add s to a list of strings

   if (b)
   {
      // add the new string to the list
      b = false;
   }
   if (/* s == match */ )
     b = true;
}

// write your file.

然后我得到这样的结果:

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

1 个答案:

答案 0 :(得分:0)

Python使用基于零的索引,因此如果X.ix[index_train,:]y[index_train]的第0维度为100,则有效的index_train的最大值为99.同样适用于{{1} }。

中的内容
index_test
在您枚举(kf)时,

导致其中一个索引对于其中一个数组太大。

相关问题