Python 用作索引的数组必须是整数(或布尔)类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17393989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:06:15  来源:igfitidea点击:

Arrays used as indices must be of integer (or boolean) type

pythonscikit-learn

提问by user1710418

Errors are like this:

错误是这样的:

Traceback (most recent call last):
  File "NearestCentroid.py", line 53, in <module>
    clf.fit(X_train.todense(),y_train)
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.13.1-py2.7-linux-i686.egg/sklearn/neighbors/nearest_centroid.py", line 115, in fit
    variance = np.array(np.power(X - self.centroids_[y], 2))
IndexError: arrays used as indices must be of integer (or boolean) type

Codes are like this:

代码是这样的:

distancemetric=['euclidean','l2']
for mtrc in distancemetric:
for shrkthrshld in [None]:
#shrkthrshld=0
#while (shrkthrshld <=1.0):
    clf = NearestCentroid(metric=mtrc,shrink_threshold=shrkthrshld)
    clf.fit(X_train.todense(),y_train)
    y_predicted = clf.predict(X_test.todense())

I am using scikit-learnpackage, X-train, y_trainare in LIBSVM format, Xis the feature:value pair, y_trainis the target/label, X_trainis in CSR matric format, the shrink_thresholddoes not support CSR sparse matrix, so I add .todense()to X_train, then I got this error, could anyone help me fix this? Thanks a lot!

我使用的scikit-learn包,X-trainy_train在LIBSVM格式,X是特征:值对,y_train是目标/标签,X_train在CSR基质格式时,shrink_threshold不支持CSR稀疏矩阵,所以我想补充.todense()X_train,然后我得到这个错误,可能有人帮我解决这个问题吗?非常感谢!

采纳答案by FaXilifresh

I had a similar problem using the Pystruct pystruct.learners.OneSlackSSVM.

我在使用 Pystruct 时遇到了类似的问题pystruct.learners.OneSlackSSVM

It occured because my training labels were floats, in stead of integers. In my case, it was because I initialized the labels with np.ones, without specifying dtype=np.int8. Hope it helps.

这是因为我的训练标签是浮点数,而不是整数。就我而言,这是因为我使用 np.ones 初始化了标签,而没有指定 dtype=np.int8。希望能帮助到你。

回答by Radio Controlled

It happens quite often that an indexing array should be clearly integertype by the way it is created, but in the case of empty list passed, becomes default float, a case which might not be considered by the programmer. For example:

经常发生的情况是,索引数组应该按照integer创建的方式明确键入,但在传递空列表的情况下,变为 default float,程序员可能不会考虑这种情况。例如:

>>> np.array(xrange(1))
>>> array([0])                #integer type as expected
>>> np.array(xrange(0))
>>> array([], dtype=float64)  #does not generalize to the empty list

Therefore, one should always explicitely define the dtypein the array constructor.

因此,应该始终dtype在数组构造函数中明确定义。

回答by yasi

Sometimes your data is in integer and every thing is right but it happened because one of your data series is an empty array, so you can use this condition:

有时你的数据是整数,每件事都是正确的,但它发生是因为你的数据系列之一是一个空数组,所以你可以使用这个条件:

if len(X_train.todense())> 0: