我正在尝试为多标签分类进行特征选择。我提取了模型将被训练到X中的特征。模型测试是在同一个X上完成的。我正在使用Pipeline并选择最好的100个特征-
#arrFinal contains all the features and the labels. Last 16 columns are labels and features are from 1 to 521. 17th column from the last is not taken
X=np.array(arrFinal[:,1:-17])
Xtest=np.array(X)
Y=np.array(arrFinal[:,522:]).astype(int)
clf = Pipeline([('chi2', SelectKBest(chi2, k=100)),('rbf',SVC())])
clf = OneVsRestClassifier(clf)
clf.fit(X, Y)
ans=clf.predict(X_test)
但我得到了以下错误-
Traceback (most recent call last):
File "C:Users50004182Documents\callee.py", line 10, in <module
>
combine.combine_main(dict_ids,inv_dict_ids,noOfIDs)
File "C:Users50004182Documentscombine.py", line 201, in combi
ne_main
clf.fit(X, Y)
File "C:Python34libsite-packagessklearnmulticlass.py", line 287, in fit
for i, column in enumerate(columns))
File "C:Python34libsite-packagessklearnexternalsjoblibparallel.py", lin
e 804, in __call__
while self.dispatch_one_batch(iterator):
File "C:Python34libsite-packagessklearnexternalsjoblibparallel.py", lin
e 662, in dispatch_one_batch
self._dispatch(tasks)
File "C:Python34libsite-packagessklearnexternalsjoblibparallel.py", lin
e 570, in _dispatch
job = ImmediateComputeBatch(batch)
File "C:Python34libsite-packagessklearnexternalsjoblibparallel.py", lin
e 183, in __init__
self.results = batch()
File "C:Python34libsite-packagessklearnexternalsjoblibparallel.py", lin
e 72, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:Python34libsite-packagessklearnexternalsjoblibparallel.py", lin
e 72, in <listcomp>
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:Python34libsite-packagessklearnmulticlass.py", line 74, in _fit_b
inary
estimator.fit(X, y)
File "C:Python34libsite-packagessklearnpipeline.py", line 164, in fit
Xt, fit_params = self._pre_transform(X, y, **fit_params)
File "C:Python34libsite-packagessklearnpipeline.py", line 145, in _pre_tr
ansform
Xt = transform.fit_transform(Xt, y, **fit_params_steps[name])
File "C:Python34libsite-packagessklearnbase.py", line 458, in fit_transfo
rm
return self.fit(X, y, **fit_params).transform(X)
File "C:Python34libsite-packagessklearnfeature_selectionunivariate_selec
tion.py", line 331, in fit
self.scores_, self.pvalues_ = self.score_func(X, y)
File "C:Python34libsite-packagessklearnfeature_selectionunivariate_selec
tion.py", line 213, in chi2
if np.any((X.data if issparse(X) else X) < 0):
TypeError: unorderable types: numpy.ndarray() < int()
因此,在与@JamieBull和@Joker进行了上述评论中的调试会话之后。我们想出的解决方案是:
确保类型正确(原始字符串)
X=np.array(arrFinal[:,1:-17]).astype(np.float64)
Xtest=np.array(X)
Y=np.array(arrFinal[:,522:]).astype(int)
首先使用VarianceThreshold
删除chi2
之前的常量(0)列。
clf = Pipeline([
('vt', VarianceThreshold()),
('chi2', SelectKBest(chi2, k=100)),
('rbf',SVC())
])
clf = OneVsRestClassifier(clf)
clf.fit(X, Y)
ans=clf.predict(X_test)