【问题标题】:How to assign feature weights in XGBClassifier?如何在 XGBClassifier 中分配特征权重?
【发布时间】:2023-01-28 01:33:15
【问题描述】:

我正在尝试为一个特征分配比其他特征更高的权重。这是我的代码。

## 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_

我浏览了文档 here 和这个 Akshay Sehgal 的 stackoverflow 响应 here 对于类似的问题。但是当我使用上面的代码时,出现以下错误?

谁能帮我解决我做错的地方?谢谢。

【问题讨论】:

    标签: python machine-learning scikit-learn xgboost


    【解决方案1】:

    我认为您需要从 XGBClassifier 的初始化中删除 feature_weights。至少,这在我尝试您的示例时有效。

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 1970-01-01
      • 2014-05-06
      • 2014-02-11
      • 2018-10-04
      • 2011-01-10
      • 2023-03-09
      • 2017-12-30
      • 2017-12-02
      相关资源
      最近更新 更多