我使用sklearn和有一个问题的亲和传播。我已经建立了一个输入矩阵,我一直得到以下错误。
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
我已经跑了
np.isnan(mat.any()) #and gets False
np.isfinite(mat.all()) #and gets True
我试着用
mat[np.isfinite(mat) == True] = 0
去除掉无限值,但这也没用。
我要怎么做才能去掉矩阵中的无穷大值,这样我就可以使用亲和传播算法了?
我使用anaconda和python 2.7.9。
这是它失败的检查:
https://github.com/scikit-learn/scikit-learn/blob/0.17.X/sklearn/utils/validation.py#L51
说
def _assert_all_finite(X):
"""Like assert_all_finite, but only for ndarray."""
X = np.asanyarray(X)
# First try an O(n) time, O(1) space solution for the common case that
# everything is finite; fall back to O(n) space np.isfinite to prevent
# false positives from overflow in sum method.
if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum())
and not np.isfinite(X).all()):
raise ValueError("Input contains NaN, infinity"
" or a value too large for %r." % X.dtype)
所以确保你的输入中有非NaN值。所有这些值实际上都是浮点值。这些值也不应该是Inf。
这是它失败的检查:
https://github.com/scikit-learn/scikit-learn/blob/0.17.X/sklearn/utils/validation.py#L51
说
def _assert_all_finite(X):
"""Like assert_all_finite, but only for ndarray."""
X = np.asanyarray(X)
# First try an O(n) time, O(1) space solution for the common case that
# everything is finite; fall back to O(n) space np.isfinite to prevent
# false positives from overflow in sum method.
if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum())
and not np.isfinite(X).all()):
raise ValueError("Input contains NaN, infinity"
" or a value too large for %r." % X.dtype)
所以确保你的输入中有非NaN值。所有这些值实际上都是浮点值。这些值也不应该是Inf。