首页 > 解决方案 > 了解应用于数据框的转换

问题描述

为什么.transform一直在下面的代码中使用?

资源

thresholds = sort(model.feature_importances_)
for thresh in thresholds:
    # select features using threshold
    selection = SelectFromModel(model, threshold=thresh, prefit=True)
    select_X_train = selection.transform(X_train) ####What is this doing? 
    # train model
    selection_model = XGBClassifier()
    selection_model.fit(select_X_train, y_train)
    # eval model
    select_X_test = selection.transform(X_test)
    y_pred = selection_model.predict(select_X_test)

标签: pythonlistdataframe

解决方案


一般来说,sklearn:

fit()用于从训练数据中学习模型参数

transform()使用从 fit() 方法中学习到的参数来生成转换后的数据集(不改变学习到的参数)

fit_transform()是 fit() 和 transform() 在同一数据集上的组合

所以在这个例子中,训练数据在被SelectFromModel模型训练之前被XGBClassifier模型转换。从 sklearn文档中,SelectFromModel基本上将特征保持在阈值或高于阈值,并丢弃其余的。


推荐阅读