use*_*418 12 python scikit-learn
错误是这样的:
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
Run Code Online (Sandbox Code Playgroud)
代码是这样的:
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())
Run Code Online (Sandbox Code Playgroud)
我使用的scikit-learn包,X-train,y_train在LIBSVM格式,X是特征:值对,y_train是目标/标签,X_train在CSR基质格式时,shrink_threshold不支持CSR稀疏矩阵,所以我想补充.todense()到X_train,然后我得到这个错误,可能有人帮我修好了吗?非常感谢!
FaX*_*esh 30
我使用Pystruct时遇到了类似的问题pystruct.learners.OneSlackSSVM.
它发生的原因是我的训练标签是漂浮物,而不是整数.就我而言,这是因为我用np.ones初始化了标签,没有指定dtype = np.int8.希望能帮助到你.
经常发生索引数组应按integer其创建方式明确键入类型的情况,但是在传递空列表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
Run Code Online (Sandbox Code Playgroud)
因此,应始终dtype在数组构造函数中明确定义。