首页 > 解决方案 > 使用 sklearn 管道时出现 ValueError:数组不得包含 infs 或 NaN

问题描述

您好我正在使用 sklearn 管道来实现 tfidf 矢量化和随机福雷斯特。使用带有 khold 交叉验证测试的 BayesSearch 进行超参数调整。

交叉验证的前几轮看起来找到了,但后来我得到了一个错误clf_search.fit(X, y)

ValueError: array must not contain infs or NaNs. 我的数据集中没有 infs 或 NaNs 值。

这是我的代码

# pipeline
pipeline = Pipeline(steps=[
      ('tfidf', TfidfVectorizer(ngram_range=(2,3), norm=None))
    , ("clf", RandomForestClassifier())
])

# parameters
n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
max_features = ['auto', 'log2']
max_depth = [int(x) for x in np.linspace(10, 100, num = 11)]
max_depth.append(None)
min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]

# Create the random grid
random_grid = {'clf__n_estimators': n_estimators
              , 'clf__max_features': max_features
              , 'clf__max_depth': max_depth
              , 'clf__min_samples_split': min_samples_split
              , 'clf__min_samples_leaf': min_samples_leaf
              , 'clf__bootstrap': bootstrap}


# Search
clf_search = BayesSearchCV(pipeline
                                , search_spaces = random_grid
                                , n_iter = 100
                                , cv = 5
                                , verbose=2
                                , random_state=42
                                , n_jobs = -1)
clf_search.fit(X, y)

标签: pythonmachine-learningscikit-learnpipelinerandom-forest

解决方案


推荐阅读