在sklearn中如何处理GradientBoostingClassifier中的大量缺失值

2024-10-03 23:24:27 发布

您现在位置:Python中文网/ 问答频道 /正文

所有的特征都是float数据类型,而有些特征则以NaN为主。我尝试通过GradientBoostingClassifier来训练模型,如下所示。在

train_x, test_x, train_y, test_y = train_test_split(features[feature_headers], features[target_header], test_size=0.33, random_state=int(time.time()))
clf = GradientBoostingClassifier(random_state=int(time.time()), learning_rate=0.1, max_leaf_nodes=None, min_samples_leaf=1, n_estimators=300, min_samples_split=2, max_features=None)
clf.fit(train_x, train_y)

但会抛出错误:

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

我不能使用一些插补方法来填充平均数、中位数或最频繁的NaN,因为从数据的角度来看,这没有任何意义。有没有更好的方法让分类器识别NaN并将其作为指示性特征来处理?谢谢。在


Tags: testnonetimetrainrandom特征nanmax
2条回答

您必须执行数据清理。为此,您需要查看要在训练数据集中包括哪些columns。对于float,可以将所有null值替换为零

df.col1 = df.col1.fillna(0)

对于字符串,可以将其替换为默认值。在

^{pr2}$

现在,如果您想放置average或一些趋势值,您可以使用相同的学习算法来预测缺失值并填充。为了运行该算法,首先替换空值,然后可以用更精确的预测值进行更改。在

Note: Any learning algorithm can't run with null values.

xgboost.XGBClassifier句柄np.nan没有插补see here。在

xgboost有一个易于使用的sklearnapi look at the documentation。在

xgboost.XGBClassifier基本上是非常接近的形式GradientBoostingClassifier,两者都是用于分类的梯度提升方法。有关示例here,请参阅。在

相关问题 更多 >