首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IndexError :索引越界

IndexError :索引越界
EN

Stack Overflow用户
提问于 2016-08-29 07:43:34
回答 1查看 615关注 0票数 1

我已经实现了MultinomialNB,但是我得到了这个消息。请帮我解决这个问题。下面是我的代码:

代码语言:javascript
复制
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

然后我得到这样的结果:

代码语言:javascript
复制
IndexError                                Traceback (most recent call last)
<ipython-input-3-df0b24edb687> in <module>()
      5 
      6 for k, (index_train, index_test) in enumerate(kf):
----> 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

EN

回答 1

Stack Overflow用户

发布于 2016-08-29 07:56:25

Python使用从零开始的索引,因此如果X.ix[index_train,:]y[index_train]的第零维是100,那么有效的index_train的最大值是99。index_test也是如此。

中的某些内容

代码语言:javascript
复制
kf = KFold(len(X), n_folds=2, shuffle=True, random_state=9999)

在枚举(Kf)时,会导致这些索引中的一个对于其中一个数组太大。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39196798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档