![](/img/trans.png)
[英]How to handle categorical variables in sklearn GradientBoostingClassifier?
[英]How to handle lots of missing values in GradientBoostingClassifier in sklearn
所有特征都是浮點數據類型,而有一些特征具有顯着量的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:輸入包含NaN,無窮大或對於dtype('float32')而言太大的值。
我不能使用一些Imputation方法用mean,median或most_frequent填充NaN,因為從數據的角度來看它沒有任何意義。 有沒有更好的方法讓分類器識別NaN並將其視為指示性功能? 非常感謝。
您必須執行數據清理。 為此,您需要查看要包含在訓練數據集中的所有columns
。 對於float,您可以將所有null
值替換為零
df.col1 = df.col1.fillna(0)
對於字符串,您可以將其替換為默認值。
df.col2 = df.DISTANCE_GROUP.fillna('')
現在,如果要放置average
或某些趨勢值,可以使用相同的學習算法來預測缺失值並填充。 為了運行算法,首先替換空值,然后可以使用更准確的預測值進行更改。
注意:任何學習算法都不能使用空值運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.