python错误:数组的索引过多 [英] python error : too many indices for array

查看:173
本文介绍了python错误:数组的索引过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入是一个导入到postgresqldb的csv文件.后来我正在使用keras构建cnn.我的以下代码给出以下错误"IndexError:数组的索引过多".我是机器学习的新手,所以我对如何解决这个问题一无所知.有什么建议吗?

My input was a csv file which was imported to postgresqldb .Later i am building a cnn using keras.My code below gives the following error "IndexError: too many indices for array". I am quite new to machine learning so I do not have any idea about how to solve this. Any suggestions?

X = dataframe1[['Feature1','Feature2','Feature3','Feature4','Feature5','Feature6','Feature7','Feature8','Feature9','Feature10','Feature11\1','Feature12','Feature13','Feature14']]
Y=result[['label']]



# evaluate model with standardized dataset
results = cross_val_score(estimator, X, Y, cv=kfold)
print("Results: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))

错误

    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    <ipython-input-50-0e5d0345015f> in <module>()
          2 estimator = KerasClassifier(build_fn=create_baseline, nb_epoch=100, batch_size=5, verbose=0)
          3 kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
    ----> 4 results = cross_val_score(estimator, X, Y, cv=kfold)
          5 print("Results: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))

    C:\Anacondav3\lib\site-packages\sklearn\model_selection\_validation.py in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch)
        129 
        130     cv = check_cv(cv, y, classifier=is_classifier(estimator))
    --> 131     cv_iter = list(cv.split(X, y, groups))
        132     scorer = check_scoring(estimator, scoring=scoring)
        133     # We clone the estimator to make sure that all the folds are

    C:\Anacondav3\lib\site-packages\sklearn\model_selection\_split.py in split(self, X, y, groups)
        320                                                              n_samples))
        321 
    --> 322         for train, test in super(_BaseKFold, self).split(X, y, groups):
        323             yield train, test
        324 

    C:\Anacondav3\lib\site-packages\sklearn\model_selection\_split.py in split(self, X, y, groups)
         89         X, y, groups = indexable(X, y, groups)
         90         indices = np.arange(_num_samples(X))
    ---> 91         for test_index in self._iter_test_masks(X, y, groups):
         92             train_index = indices[np.logical_not(test_index)]
         93             test_index = indices[test_index]

    C:\Anacondav3\lib\site-packages\sklearn\model_selection\_split.py in _iter_test_masks(self, X, y, groups)
        608 
        609     def _iter_test_masks(self, X, y=None, groups=None):
    --> 610         test_folds = self._make_test_folds(X, y)
        611         for i in range(self.n_splits):
        612             yield test_folds == i

    C:\Anacondav3\lib\site-packages\sklearn\model_selection\_split.py in _make_test_folds(self, X, y, groups)
        595         for test_fold_indices, per_cls_splits in enumerate(zip(*per_cls_cvs)):
        596             for cls, (_, test_split) in zip(unique_y, per_cls_splits):
    --> 597                 cls_test_folds = test_folds[y == cls]
        598                 # the test split can be too big because we used
        599                 # KFold(...).split(X[:max(c, n_splits)]) when data is not 100%

IndexError: too many indices for array

我应该以其他方式声明数组或数据框吗?

Is there a different way that I should be declaring the array or dataframe?

推荐答案

请注意,用户指南显示X是2维的,而y是1维的:

Notice that the example in the User Guide shows that X is 2-dimensional while y is 1-dimensional:

>>> X_train.shape, y_train.shape
((90, 4), (90,))

某些程序员将大写变量用于2维数组,并将小写字母用于1维数组.

Some programmers use capitalized variables for 2-dimensional arrays and lower-case for 1-dimensional arrays.

因此使用

Y = result['label']

代替

Y = result[['label']]


我假设result是熊猫DataFrame.当您为具有诸如['label']之类的列的列表的Dataframe编制索引时,将返回2维的子DataFrame.如果使用单个字符串为DataFrame编制索引,则将返回一维Series.


I am assuming that result is a pandas DataFrame. When you index a Dataframe with a list of columns such as ['label'], a sub-DataFrame -- which is 2-dimensional -- is returned. If you index the DataFrame with a single string, a 1-dimensional Series is returned.

最后,请注意IndexError

Finally, note that the IndexError

IndexError: too many indices for array

在此行上引发

cls_test_folds = test_folds[y == cls]

因为y是二维的,所以y == cls是二维的 boolean 数组,而test_folds是一维的.情况类似于以下内容:

because y is 2-dimensional so y == cls is a 2-dimensional boolean array and test_folds is 1-dimensional. The situation is similar to the following:

In [72]: test_folds = np.zeros(5, dtype=np.int)
In [73]: y_eq_cls = np.array([(True, ), (False,)])
In [74]: test_folds[y_eq_cls]
IndexError: too many indices for array

这篇关于python错误:数组的索引过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆