上述ValueError Traceback (most recent call last)
Cell In[17], line 34
29 X_train, X_test, y_train, y_test = train_test_split(
30 X_processed, y, test_size=0.3, random_state=420
31 )
33 # 训练Ridge模型
---> 34 reg = Ridge(alpha=5).fit(X_train, y_train)
36 # 评估模型
37 r2 = reg.score(X_test, y_test)
File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\base.py:1365, in _fit_context.<locals>.decorator.<locals>.wrapper(estimator, *args, **kwargs)
1358 estimator._validate_params()
1360 with config_context(
1361 skip_parameter_validation=(
1362 prefer_skip_nested_validation or global_skip_validation
1363 )
1364 ):
-> 1365 return fit_method(estimator, *args, **kwargs)
File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\linear_model\_ridge.py:1238, in Ridge.fit(self, X, y, sample_weight)
1236 _accept_sparse = _get_valid_accept_sparse(sparse.issparse(X), self.solver)
1237 xp, _ = get_namespace(X, y, sample_weight)
-> 1238 X, y = validate_data(
1239 self,
1240 X,
1241 y,
1242 accept_sparse=_accept_sparse,
1243 dtype=[xp.float64, xp.float32],
1244 force_writeable=True,
1245 multi_output=True,
1246 y_numeric=True,
1247 )
1248 return super().fit(X, y, sample_weight=sample_weight)
File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\utils\validation.py:2971, in validate_data(_estimator, X, y, reset, validate_separately, skip_check_array, **check_params)
2969 y = check_array(y, input_name="y", **check_y_params)
2970 else:
-> 2971 X, y = check_X_y(X, y, **check_params)
2972 out = X, y
2974 if not no_val_X and check_params.get("ensure_2d", True):
File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\utils\validation.py:1368, in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_writeable, force_all_finite, ensure_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
1362 raise ValueError(
1363 f"{estimator_name} requires y to be passed, but the target y is None"
1364 )
1366 ensure_all_finite = _deprecate_force_all_finite(force_all_finite, ensure_all_finite)
-> 1368 X = check_array(
1369 X,
1370 accept_sparse=accept_sparse,
1371 accept_large_sparse=accept_large_sparse,
1372 dtype=dtype,
1373 order=order,
1374 copy=copy,
1375 force_writeable=force_writeable,
1376 ensure_all_finite=ensure_all_finite,
1377 ensure_2d=ensure_2d,
1378 allow_nd=allow_nd,
1379 ensure_min_samples=ensure_min_samples,
1380 ensure_min_features=ensure_min_features,
1381 estimator=estimator,
1382 input_name="X",
1383 )
1385 y = _check_y(y, multi_output=multi_output, y_numeric=y_numeric, estimator=estimator)
1387 check_consistent_length(X, y)
File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\utils\validation.py:1105, in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_writeable, force_all_finite, ensure_all_finite, ensure_non_negative, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name)
1099 raise ValueError(
1100 f"Found array with dim {array.ndim},"
1101 f" while dim <= 2 is required{context}."
1102 )
1104 if ensure_all_finite:
-> 1105 _assert_all_finite(
1106 array,
1107 input_name=input_name,
1108 estimator_name=estimator_name,
1109 allow_nan=ensure_all_finite == "allow-nan",
1110 )
1112 if copy:
1113 if _is_numpy_namespace(xp):
1114 # only make a copy if `array` and `array_orig` may share memory`
File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\utils\validation.py:120, in _assert_all_finite(X, allow_nan, msg_dtype, estimator_name, input_name)
117 if first_pass_isfinite:
118 return
--> 120 _assert_all_finite_element_wise(
121 X,
122 xp=xp,
123 allow_nan=allow_nan,
124 msg_dtype=msg_dtype,
125 estimator_name=estimator_name,
126 input_name=input_name,
127 )
File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\utils\validation.py:169, in _assert_all_finite_element_wise(X, xp, allow_nan, msg_dtype, estimator_name, input_name)
152 if estimator_name and input_name == "X" and has_nan_error:
153 # Improve the error message on how to handle missing values in
154 # scikit-learn.
155 msg_err += (
156 f"\n{estimator_name} does not accept missing values"
157 " encoded as NaN natively. For supervised learning, you might want"
(...)
167 "#estimators-that-handle-nan-values"
168 )
--> 169 raise ValueError(msg_err)
ValueError: Input X contains NaN.
Ridge does not accept missing values encoded as NaN natively. For supervised learning, you might want to consider sklearn.ensemble.HistGradientBoostingClassifier and Regressor which accept missing values encoded as NaNs natively. Alternatively, it is possible to preprocess the data, for instance by using an imputer transformer in a pipeline or drop samples with missing values. See https://scikit-learn.org/stable/modules/impute.html You can find a list of all estimators that handle NaN values at the following page: https://scikit-learn.org/stable/modules/impute.html#estimators-that-handle-nan-values
1
最新发布