如何在XGBClassifier中分配特征权重?

4

我正在尝试将一个特征的权重分配得比其他特征更高。以下是我的代码。

## Assign weight to High Net Worth feature
cols = list(train_X.columns.values)

# 0 - 1163 --Other Columns
# 1164 --High Net Worth

#Create an array of feature weights
other_col_wt = [1]*1164
high_net_worth_wt = [5]
feature_wt = other_col_wt + high_net_worth_wt
feature_weights = np.array(feature_wt)

# Initialize the XGBClassifier
xgboost = XGBClassifier(subsample = 0.8, # subsample = 0.8 ideal for big datasets
                        silent=False,  # whether print messages during construction
                        colsample_bytree = 0.4, # subsample ratio of columns when constructing each tree
                        gamma=10, # minimum loss reduction required to make a further partition on a leaf node of the tree, regularisation parameter
                        objective='binary:logistic',
                        eval_metric = ["auc"],
                        feature_weights = feature_weights
                      )
# Hypertuning parameters
lr = [0.1,1] # learning_rate = shrinkage for updating the rules
ne = [100] # n_estimators = number of boosting rounds
md = [3,4,5] # max_depth = maximum tree depth for base learners

# Grid Search
clf = GridSearchCV(xgboost,{
    'learning_rate':lr,
    'n_estimators':ne,
    'max_depth':md
},cv = 5,return_train_score = False)

# Fitting the model with the custom weights
clf.fit(train_X,train_y, feature_weights = feature_weights)
clf.cv_results_

我查看了这里的文档和Akshay Sehgal在stackoverflow上对类似问题的回答这里。但是当我使用上述代码时,出现以下错误:

enter image description here

请问有谁能帮我看看我哪里做错了吗?谢谢。

1个回答

3
我认为您需要从XGBClassifier的初始化中删除feature_weights。至少,当我尝试您的示例时,这种做法是可行的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接