python - 了解应用于数据框的转换
问题描述
为什么.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)
解决方案
一般来说,sklearn:
fit()
用于从训练数据中学习模型参数
transform()
使用从 fit() 方法中学习到的参数来生成转换后的数据集(不改变学习到的参数)
fit_transform()
是 fit() 和 transform() 在同一数据集上的组合
所以在这个例子中,训练数据在被SelectFromModel
模型训练之前被XGBClassifier
模型转换。从 sklearn文档中,SelectFromModel
基本上将特征保持在阈值或高于阈值,并丢弃其余的。
推荐阅读
- javascript - Cloud Function Firebase request.body 未定义
- javascript - 为什么在 react-redux 上未定义 [object Object],[object Object]?
- extjs - 煎茶过滤器网格列
- php - 为什么第二组数组中有索引?
- android - 达到maxLength后如何将焦点更改为下一个EditText?
- php - laravel 文件上传会抛出错误“文件不存在或不可读”。
- c++ - 为什么循环是无限的?
- python - 两列之间的日期范围
- javascript - 在 Windows 上将 defaultPath 设置为这台电脑,可能吗?
- javascript - 我们可以禁用可编程视频 API 中的视频吗 - P2P 连接 - Twilio