ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64‘).

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64‘).相关的知识,希望对你有一定的参考价值。

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

目录

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

问题:

解决:

完成错误:


问题:

数据里面有无穷大值;

selector = SelectKBest(f_classif, k=10)
X = selector.fit_transform(df, y)

解决:

删除包含无穷大值的数据行;

df = df[np.isfinite(df).all(1)]
 

print(df.shape)
df = df[np.isfinite(df).all(1)]
print(df.shape)

selector = SelectKBest(f_classif, k=10)
X = selector.fit_transform(df, y)

完成错误:

ValueError                                Traceback (most recent call last)
<ipython-input-5333-9f0c91a72327> in <module>
      1 selector = SelectKBest(f_classif, k=10)
----> 2 X = selector.fit_transform(X_original, y)

D:\\anaconda\\lib\\site-packages\\sklearn\\base.py in fit_transform(self, X, y, **fit_params)
    700         else:
    701             # fit method of arity 2 (supervised transformation)
--> 702             return self.fit(X, y, **fit_params).transform(X)
    703 
    704 

D:\\anaconda\\lib\\site-packages\\sklearn\\feature_selection\\_univariate_selection.py in fit(self, X, y)
    343         """
    344         X, y = self._validate_data(X, y, accept_sparse=['csr', 'csc'],
--> 345                                    multi_output=True)
    346 
    347         if not callable(self.score_func):

D:\\anaconda\\lib\\site-packages\\sklearn\\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
    431                 y = check_array(y, **check_y_params)
    432             else:
--> 433                 X, y = check_X_y(X, y, **check_params)
    434             out = X, y
    435 

D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
    876                     ensure_min_samples=ensure_min_samples,
    877                     ensure_min_features=ensure_min_features,
--> 878                     estimator=estimator)
    879     if multi_output:
    880         y = check_array(y, accept_sparse='csr', force_all_finite=True,

D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
    719         if force_all_finite:
    720             _assert_all_finite(array,
--> 721                                allow_nan=force_all_finite == 'allow-nan')
    722 
    723     if ensure_min_samples > 0:

D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in _assert_all_finite(X, allow_nan, msg_dtype)
    104                     msg_err.format
    105                     (type_err,
--> 106                      msg_dtype if msg_dtype is not None else X.dtype)
    107             )
    108     # for object dtype data, we only check for NaNs (GH-13254)

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

以上是关于ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64‘).的主要内容,如果未能解决你的问题,请参考以下文章

ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64‘).

Python Xgboost: ValueError('feature_names may not contain [, ] or <')

VARMAX 结果扩展导致 ValueError: array must not contain infs or NaNs

[Trouble Shooting - Python] ValueError: source code string cannot contain null bytes

Pycharm ValueError: source code string cannot contain null bytes

skopt 的 gp_minimize() 函数引发 ValueError: array must not contain infs or NaNs