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').

问题

#就是因为输入模型的数据中有NaN值,所以引发了错误;

# Create lasso regression with three alpha values
regr_cv = LassoCV(alphas=alphas, cv = 5, max_iter = 1e4,selection = 'random',random_state=42)
#regr_cv = LassoCV(alphas=alphas, cv = 5, max_iter = 1e6)

# Fit the linear regression
regr_cv.fit(X_train, target_train)

#解决

#数据对齐的时候因为索引的缘故,发生了包含NaN的行,去除NaN问题就解决了;

# pd.DataFrame(features_train, columns = num_features)
# df.loc[num_features_train.index][cat_features]

# X_train = pd.concat([pd.DataFrame(features_train, columns = num_features),\\
#                      df.loc[num_features_train.index][cat_features].reset_index(drop = True)],axis = 1,ignore_index=True)
# combine dataframe so different info merged together
# X_train = pd.concat([pd.DataFrame(features_train, columns = num_features),\\
#                      df.loc[num_features_train.index][cat_features].reset_index(drop = True)],axis = 1,ignore_index=True)

完整错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-125-9843089b7e08> in <module>
      4 
      5 # Fit the linear regression
----> 6 regr_cv.fit(X_train, target_train)

D:\\anaconda\\lib\\site-packages\\sklearn\\linear_model\\_coordinate_descent.py in fit(self, X, y)
   1236             X, y = self._validate_data(X, y,
   1237                                        validate_separately=(check_X_params,
-> 1238                                                             check_y_params))
   1239             copy_X = False
   1240 

D:\\anaconda\\lib\\site-packages\\sklearn\\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
    428                 # :(
    429                 check_X_params, check_y_params = validate_separately
--> 430                 X = check_array(X, **check_X_params)
    431                 y = check_array(y, **check_y_params)
    432             else:

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').

参考:python

以上是关于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