簡體   English   中英

ValueError: Input contains NaN, infinity or a value too large for dtype('float64') While fit the model after Imputation

[英]ValueError: Input contains NaN, infinity or a value too large for dtype('float64') While fitting the model After Imputation

我從插補管道傳遞預測變量,檢查列的 NaN 和 inf 值

col_name = X.columns.to_series()[np.isinf(X).any()]

沒有缺少值或 inf 值的列。 分類器 = MLPClassifier

我這樣做: clf.fit(X,y)然后得到錯誤ValueError: Input contains NaN, infinity or a value too large for dtype('float64')我在這里缺少什么? 任何人都可以幫助或指導我該怎么做???? 此錯誤的其他可能解釋是什么? 我還應該檢查什么? 還有完整的追溯:

    Input In [137], in <cell line: 4>()
      1 #l = list(X.isin([np.inf, -np.inf]))
      2 #col_name = X.columns.to_series()[np.isnan(X).any()]
      3 #col_name
----> 4 clf.fit(X, y)

File ~\anaconda3\lib\site-packages\sklearn\pipeline.py:394, in Pipeline.fit(self, X, y, **fit_params)
    392     if self._final_estimator != "passthrough":
    393         fit_params_last_step = fit_params_steps[self.steps[-1][0]]
--> 394         self._final_estimator.fit(Xt, y, **fit_params_last_step)
    396 return self

File ~\anaconda3\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py:752, in BaseMultilayerPerceptron.fit(self, X, y)
    735 def fit(self, X, y):
    736     """Fit the model to data matrix X and target(s) y.
    737 
    738     Parameters
   (...)
    750         Returns a trained MLP model.
    751     """
--> 752     return self._fit(X, y, incremental=False)

File ~\anaconda3\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py:393, in BaseMultilayerPerceptron._fit(self, X, y, incremental)
    386     raise ValueError(
    387         "hidden_layer_sizes must be > 0, got %s." % hidden_layer_sizes
    388     )
    389 first_pass = not hasattr(self, "coefs_") or (
    390     not self.warm_start and not incremental
    391 )
--> 393 X, y = self._validate_input(X, y, incremental, reset=first_pass)
    395 n_samples, n_features = X.shape
    397 # Ensure y is 2D

File ~\anaconda3\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py:1100, in MLPClassifier._validate_input(self, X, y, incremental, reset)
   1099 def _validate_input(self, X, y, incremental, reset):
-> 1100     X, y = self._validate_data(
   1101         X,
   1102         y,
   1103         accept_sparse=["csr", "csc"],
   1104         multi_output=True,
   1105         dtype=(np.float64, np.float32),
   1106         reset=reset,
   1107     )
   1108     if y.ndim == 2 and y.shape[1] == 1:
   1109         y = column_or_1d(y, warn=True)

File ~\anaconda3\lib\site-packages\sklearn\base.py:581, in BaseEstimator._validate_data(self, X, y, reset, validate_separately, **check_params)
    579         y = check_array(y, **check_y_params)
    580     else:
--> 581         X, y = check_X_y(X, y, **check_params)
    582     out = X, y
    584 if not no_val_X and check_params.get("ensure_2d", True):

File ~\anaconda3\lib\site-packages\sklearn\utils\validation.py:979, 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)
    962     raise ValueError("y cannot be None")
    964 X = check_array(
    965     X,
    966     accept_sparse=accept_sparse,
   (...)
    976     estimator=estimator,
    977 )
--> 979 y = _check_y(y, multi_output=multi_output, y_numeric=y_numeric)
    981 check_consistent_length(X, y)
    983 return X, y

File ~\anaconda3\lib\site-packages\sklearn\utils\validation.py:989, in _check_y(y, multi_output, y_numeric)
    987 """Isolated part of check_X_y dedicated to y validation"""
    988 if multi_output:
--> 989     y = check_array(
    990         y, accept_sparse="csr", force_all_finite=True, ensure_2d=False, dtype=None
    991     )
    992 else:
    993     y = column_or_1d(y, warn=True)

File ~\anaconda3\lib\site-packages\sklearn\utils\validation.py:800, 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)
    794         raise ValueError(
    795             "Found array with dim %d. %s expected <= 2."
    796             % (array.ndim, estimator_name)
    797         )
    799     if force_all_finite:
--> 800         _assert_all_finite(array, allow_nan=force_all_finite == "allow-nan")
    802 if ensure_min_samples > 0:
    803     n_samples = _num_samples(array)

File ~\anaconda3\lib\site-packages\sklearn\utils\validation.py:114, in _assert_all_finite(X, allow_nan, msg_dtype)
    107     if (
    108         allow_nan
    109         and np.isinf(X).any()
    110         or not allow_nan
    111         and not np.isfinite(X).all()
    112     ):
    113         type_err = "infinity" if allow_nan else "NaN, infinity"
--> 114         raise ValueError(
    115             msg_err.format(
    116                 type_err, msg_dtype if msg_dtype is not None else X.dtype
    117             )
    118         )
    119 # for object dtype data, we only check for NaNs (GH-13254)
    120 elif X.dtype == np.dtype("object") and not allow_nan:

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

您可能還想檢查nan

col_name = X.columns.to_series()[np.isinf(X).any() | np.isnan(X).any()]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM