GridSearchCV():ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值
Posted
技术标签:
【中文标题】GridSearchCV():ValueError:输入包含 NaN、无穷大或对于 dtype(\'float64\') 来说太大的值【英文标题】:GridSearchCV(): ValueError: Input contains NaN, infinity or a value too large for dtype('float64')GridSearchCV():ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值 【发布时间】:2020-08-16 00:51:39 【问题描述】:当我尝试在 MLP 分类器上执行 GridsearchCV 时,标题中出现 ValueError。当然,我检查了我的数据集中是否存在任何 np.inf 或 np.nan,但它们不存在:
print(np.any(np.isnan(X)))
返回假
print(np.all(np.isfinite(X)))
返回真
我还将所有值都转换为 np.float64
X = X.values.astype(np.float64)
Y = Y.values
我的 scikit-learn 版本是 0.22.2.post1(最新)
我正在尝试执行的代码:
from scipy.stats import randint as sp_randint
hiddenlayers = [(sp_randint.rvs(100,600,1),sp_randint.rvs(100,600,1),), (sp_randint.rvs(100,600,1),)]
alpha_range = 10.0 ** np.arange(-2, 1)
param_grid_MLP = ['solver': ['lbfgs'],
'hidden_layer_sizes': hiddenlayers,
'activation': ['identity','tanh', 'relu', 'logistic'],
'alpha': alpha_range
,
'solver': ['sgd'],
'hidden_layer_sizes': hiddenlayers,
'activation': ['identity','tanh', 'relu', 'logistic'],
'alpha': alpha_range,
'learning_rate':['constant','invscaling','adaptive']
,
'solver': ['adam'],
'hidden_layer_sizes': hiddenlayers,
'activation': ['identity','tanh', 'relu', 'logistic'],
'alpha': alpha_range
]
mlp = MLPClassifier(random_state=0)
cross_validation = StratifiedKFold(5)
# scoring = 'AUC': 'roc_auc',
# 'Accuracy': make_scorer(accuracy_score),
# 'Recall':make_scorer(recall_score,pos_label='crafted'),
# 'Precision': make_scorer(precision_score,pos_label='crafted')
scoring = 'AUC': 'roc_auc',
'Accuracy': make_scorer(accuracy_score),
'Recall':make_scorer(recall_score,pos_label='crafted')
grid_search_MLP = GridSearchCV(estimator=mlp,
param_grid=param_grid_MLP,
scoring=scoring,cv=cross_validation.split(X_train,y_train),
refit='Recall',
n_jobs=-1,
verbose=True)
grid_search_MLP.fit(X_train,y_train)
print('Best score: '.format(grid_search_MLP.best_score_))
print('Best index: '.format(grid_search_MLP.best_index_))
print('Best parameters: '.format(grid_search_MLP.best_params_))
mlp = grid_search_MLP.best_estimator_
mlp
完整的错误回溯:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/joblib/externals/loky/process_executor.py", line 418, in _process_worker
r = call_item()
File "/usr/local/lib/python3.7/dist-packages/joblib/externals/loky/process_executor.py", line 272, in __call__
return self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.7/dist-packages/joblib/_parallel_backends.py", line 608, in __call__
return self.func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/joblib/parallel.py", line 256, in __call__
for func, args, kwargs in self.items]
File "/usr/local/lib/python3.7/dist-packages/joblib/parallel.py", line 256, in <listcomp>
for func, args, kwargs in self.items]
File "/usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_validation.py", line 544, in _fit_and_score
test_scores = _score(estimator, X_test, y_test, scorer)
File "/usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_validation.py", line 591, in _score
scores = scorer(estimator, X_test, y_test)
File "/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_scorer.py", line 87, in __call__
*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_scorer.py", line 332, in _score
return self._sign * self._score_func(y, y_pred, **self._kwargs)
File "/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_ranking.py", line 369, in roc_auc_score
y_score = check_array(y_score, ensure_2d=False)
File "/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py", line 578, in check_array
allow_nan=force_all_finite == 'allow-nan')
File "/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py", line 60, in _assert_all_finite
msg_dtype if msg_dtype is not None else X.dtype)
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
【问题讨论】:
你从哪里得到你的阵列?一个csv文件?你如何建造它?你能打印出来吗? 【参考方案1】:在我看来,您的数组中的值可能已损坏,或者是非数值。在转换为浮点数之前,尝试检查数组中是否还有其他类型。还尝试找到数组中的最小值和最大值,这可能有助于找到引发错误的值。
【讨论】:
【参考方案2】:尝试给出一个大数字,或者一个一个地运行网格的 3 个部分。如果你意识到sgd
给出了问题,它可能在这里解释MLPRegressor error when solver sgd is used
【讨论】:
以上是关于GridSearchCV():ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值的主要内容,如果未能解决你的问题,请参考以下文章
GridSearchCV 给出 ValueError:DecisionTreeRegressor 不支持连续
GridSearchCV 和 ValueError:估计器管道的参数 alpha 无效
GridSearchCV():ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值
ValueError 在 Scikit 中找到最佳超参数时使用 GridSearchCV 学习 LogisticRegression
GridSearchCV 和 LogisticRegression 引发 ValueError:无法处理连续和二进制的混合
GridSearchCV 在管道中将 fit_params 传递给 XGBRegressor 会产生“ValueError:需要超过 1 个值才能解包”