【问题标题】:Prediction after feature selection python特征选择python后的预测
【发布时间】:2016-07-27 10:49:59
【问题描述】:

我正在尝试使用 python 构建预测模型。训练和测试数据集有超过 400 个变量。在训练数据集上使用特征选择,变量的数量减少到 180

from sklearn.feature_selection import VarianceThreshold
sel = VarianceThreshold(threshold = .9)

然后我正在使用梯度提升算法训练模型,在交叉验证中实现 0.84 AUC 准确度。

from sklearn import ensemble
from sklearn.cross_validation import train_test_split 
from sklearn.metrics import roc_auc_score as auc 
df_fit, df_eval, y_fit, y_eval= train_test_split( df, y, test_size=0.2, random_state=1 )
boosting_model = ensemble.GradientBoostingClassifier(n_estimators=100, max_depth=3, 
                                                    min_samples_leaf=100, learning_rate=0.1, 
                                                    subsample=0.5, random_state=1)
boosting_model.fit(df_fit, y_fit)

但是当我尝试使用这个模型来预测预测数据集时,它给了我错误

predict_target = boosting_model.predict(df_prediction)
Error: Number of variables in prediction data set 'df_prediction' does not match the number of variables in the model

这是有道理的,因为测试数据中的总变量仍然超过 400 个。 无论如何,我的问题是绕过这个问题并继续使用特征选择进行预测建模。因为如果我删除它,模型的准确性会下降到 0.5,这是非常差的。 谢谢!

【问题讨论】:

    标签: python machine-learning scikit-learn data-modeling feature-selection


    【解决方案1】:

    您也应该通过特征选择来转换您的预测矩阵。所以在你的代码中的某个地方你做

    df = sel.fit_transform(X)
    

    预测之前

    df_prediction = sel.transform(X_prediction)
    

    【讨论】:

    • 成功了,简直不敢相信它是多么简单。非常感谢!
    猜你喜欢
    • 2018-12-27
    • 2019-05-15
    • 1970-01-01
    • 2018-04-12
    • 2017-06-03
    • 2017-12-10
    • 2012-12-17
    • 2020-11-15
    • 2020-09-04
    相关资源
    最近更新 更多