I already referred these two posts:
Please don't mark this as a duplicate.
I am trying to get the feature names from a bagging classifier (which does not have inbuilt feature importance).
I have the below sample data and code based on those related posts linked above
import numpy as np
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
clf = BaggingClassifier(DecisionTreeClassifier())
clf.fit(X, y)
feature_importances = np.mean([tree.feature_importances_ for tree in clf.estimators_], axis=0)
but this outputs only the feature importance (shown below) but I also want the feature names.
feature_importances
# array([0.15098599, 0.27608213, 0.33606019, 0.23687169])
How can I find the corresponding feature names for these feature importance values?