用作索引的数组必须是整数(或布尔)类型 [英] Arrays used as indices must be of integer (or boolean) type

查看:86
本文介绍了用作索引的数组必须是整数(或布尔)类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是这样的:

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

代码是这样的:

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())

我使用的是scikit-learn包,X-trainy_train是LIBSVM格式,X是特征:值对,y_train是目标/标签,X_train是CSR矩阵格式,shr​​ink_threshold不支持CSR稀疏矩阵,所以我将 .todense() 添加到 X_train,然后我得到了这个错误,谁能帮我解决这个问题?非常感谢!

I am using scikit-learn package, X-train, y_train are in LIBSVM format, X is the feature:value pair, y_train is the target/label, X_train is in CSR matric format, the shrink_threshold does 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!

推荐答案

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

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

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

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.

这篇关于用作索引的数组必须是整数(或布尔)类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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