Retrieve list of training features names from classifier

前端 未结 4 1375
一整个雨季
一整个雨季 2021-01-12 03:31

Is there a way to retrieve the list of feature names used for training of a classifier, once it has been trained with the fit method? I would like to get this i

4条回答
  •  爱一瞬间的悲伤
    2021-01-12 04:17

    I have a solution which works but is not very elegant. This is an old post with no existing solutions so I suppose there are not any.

    Create and fit your model. For example

    model = GradientBoostingRegressor(**params)
    model.fit(X_train, y_train)
    

    Then you can add an attribute which is the 'feature_names' since you know them at training time

    model.feature_names = list(X_train.columns.values)
    

    I typically then put the model into a binary file to pass it around but you can ignore this

    joblib.dump(model, filename)
    loaded_model = joblib.load(filename)
    

    Then you can get the feature names back from the model to use them when you predict

    f_names = loaded_model.feature_names
    loaded_model.predict(X_pred[f_names])
    

提交回复
热议问题