New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to know which feature is selected by FeatureUnion? #6122
Comments
I agree that there should be a way to see which features belong to which On 6 January 2016 at 18:23, genliu777 notifications@github.com wrote:
|
why currently impossible!? you know, FeatureUnion gives the function like, maybe all of them, models in sklearn, have the function |
This may not address your particular issue with PCA directly, but if I read into your question correctly, you are wondering in general how to percolate attributes through the custom pipeline. Late to the party, but you can access elements within the pipeline, regardless how complicated, by walking through the pipeline structure, finding the appropriate step (even within featureunion) and then using the appropriate attribute. Here is an example I just ran:
Showing the pipeline object itself via print(pipeline) gives me a point of reference:
So I walk through to the union step via:
Then walk to the next level which is transformer_list (or the categoric pipeline) via:
Then walk to the next level which is the steps within the categoric pipeline via:
The above outputs a typical pipeline structure, where we can now utilize named_steps: And therefore access the attribute we need via: TLDR; |
Please try |
@joshhamanngaia Awesome. Thank you for not just showing what but also showing how and why. |
On from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.feature_selection import SelectKBest
iris = load_iris(as_frame=True)
X, y = iris.data, iris.target
pca = PCA(n_components=2)
selection = SelectKBest(k=1)
combined_features = FeatureUnion([("pca", pca), ("univ_select", selection)])
svm = SVC(kernel="linear")
pipeline = Pipeline([("features", combined_features), ("svm", svm)])
pipeline.fit(X, y)
# slice the pipeline to include all steps excluding the last one
pipeline[:-1].get_feature_names_out()
# array(['pca__pca0', 'pca__pca1', 'univ_select__petal length (cm)'], dtype=object) In 1.1, we will all transformers will define a |
i run the code of,
http://scikit-learn.org/stable/auto_examples/feature_stacker.html#example-feature-stacker-py
and with the following code,
with data put into FeatureUnion, i want to know which feature is selected. in the doc of FeatureUnion, there is a funtion
get_feature_names()
which gets all the names from all the transformer. so just call this function and get error like this,actually, i know pca does not have function like this. but why FeatureUnion provide this function!?
The text was updated successfully, but these errors were encountered: