我正在尝试使用稀疏矩阵完成分类任务。当我将训练集传递给逻辑回归函数时,在scikit中,它会返回以下错误:
ValueError: Array contains NaN or infinity.
我作为逻辑回归的参数传递的矩阵类型是scipy.sparse.csr.csr_matrix
。
这是我用NaNs的numpy数组创建稀疏矩阵的方法:
X=sparse.csr_matrix(X_hat)
我做错了什么?我得到了与train_test_split
相同的错误,根据文档,这些函数应该接受稀疏矩阵作为参数。
我正在使用0.14版本的scikit-learn。
更新: 这是完整的TraceBack。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-65-acdaac419166> in <module>()
1
----> 2 model2.fit(X_train, y_train)
C:\Anaconda\lib\site-packages\sklearn\svm\base.pyc in fit(self, X, y)
666 " one.")
667
--> 668 X = atleast2d_or_csr(X, dtype=np.float64, order="C")
669
670 self.class_weight_ = compute_class_weight(self.class_weight,
C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in atleast2d_or_csr(X, dtype, order, copy, force_all_finite)
132 """
133 return _atleast2d_or_sparse(X, dtype, order, copy, sparse.csr_matrix,
--> 134 "tocsr", force_all_finite)
135
136
C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in _atleast2d_or_sparse(X, dtype, order, copy, sparse_class, convmethod, force_all_finite)
105 X = sparse_class(X, dtype=dtype)
106 if force_all_finite:
--> 107 _assert_all_finite(X.data)
108 X.data = np.array(X.data, copy=False, order=order)
109 else:
C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in _assert_all_finite(X)
25 if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum())
26 and not np.isfinite(X).all()):
---> 27 raise ValueError("Array contains NaN or infinity.")
28
29
ValueError: Array contains NaN or infinity.