๋๋ ์ฝ๋๋ฅผ ์คํ,
http://scikit-learn.org/stable/auto_examples/feature_stacker.html#example -feature-stacker-py
๋ค์ ์ฝ๋์ ํจ๊ป,
# Build estimator from PCA and Univariate selection:
combined_features = FeatureUnion([("pca", pca), ("univ_select", selection)])
# Use combined features to transform dataset:
X_features = combined_features.fit(X, y).transform(X)
FeatureUnion์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ ๋ ์ด๋ค ๊ธฐ๋ฅ์ด ์ ํ๋์๋์ง ์๊ณ ์ถ์ต๋๋ค. FeatureUnion ๋ฌธ์์๋ ๋ชจ๋ ๋ณํ๊ธฐ์์ ๋ชจ๋ ์ด๋ฆ์ ๊ฐ์ ธ์ค๋ get_feature_names()
ํจ์๊ฐ ์์ต๋๋ค. ๋ฐ๋ผ์ ์ด ํจ์๋ฅผ ํธ์ถํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
AttributeError: Transformer pca does not provide get_feature_names.
์ฌ์ค, ๋๋ pca์ ์ด๋ฐ ๊ธฐ๋ฅ์ด ์๋ค๋ ๊ฒ์ ์๊ณ ์์ต๋๋ค. ๊ทธ๋ฐ๋ฐ ์ FeatureUnion์ด ์ด ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๊น!?
์ด๋ค ๊ธฐ๋ฅ์ด ์ด๋ค ๊ธฐ๋ฅ์ ์ํ๋์ง ํ์ธํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์ด์ผ ํ๋ค๋ ๋ฐ ๋์ํฉ๋๋ค.
๊ตฌ์ฑ ์์, ๊ทธ๋ฆฌ๊ณ ๋๋ ์ค๋์ ์ ์ด๊ฒ์ ์ ์ํ์ง๋ง, ๋๋ ๊ทธ๊ฒ์ด ์๋๋ผ๊ณ ์๊ฐํฉ๋๋ค.
ํ์ฌ ๊ฐ๋ฅํฉ๋๋ค.
2016๋ 1์ 6์ผ 18:23์ genliu777 [email protected] ์์ ๋ค์๊ณผ ๊ฐ์ด ์ผ์ต๋๋ค.
๋๋ ์ฝ๋๋ฅผ ์คํ,
http://scikit-learnorg/stable/auto_examples/feature_stackerhtml#example -feature-stacker-py
๋ค์ ์ฝ๋์ ํจ๊ป,PCA ๋ฐ ์ผ๋ณ๋ ์ ํ์์ ์ถ์ ๊ธฐ ๊ตฌ์ถ:
Combine_features = FeatureUnion([("pca", pca), ("univ_select", ์ ํ)])
๊ฒฐํฉ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ์ธํธ ๋ณํ:
X_features = Combined_featuresfit(X, y)transform(X)
FeatureUnion์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์์ ๋ ์ด๋ค ๊ธฐ๋ฅ์ด ์ ํ๋์๋์ง ์๊ณ ์ถ์ต๋๋ค.
FeatureUnion ๋ฌธ์์๋ get_feature_names() ํจ์๊ฐ ์์ต๋๋ค.
๋ชจ๋ ๋ณํ๊ธฐ์์ ์ ๋ชจ๋ ์ด๋ฆ์ ๊ฐ์ ธ์ค๋ฏ๋ก ์ด ํจ์๋ฅผ ํธ์ถํ๋ฉด ๋ฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
AttributeError: ๋ณํ๊ธฐ pca๊ฐ get_feature_names๋ฅผ ์ ๊ณตํ์ง ์์ต๋๋ค.
์ค์ ๋ก, ๋๋ pca์ ์ด์ ๊ฐ์ ๊ธฐ๋ฅ์ด ์๋ค๋ ๊ฒ์ ์๊ณ ์์ง๋ง ์ FeatureUnion์ด
์ด ๊ธฐ๋ฅ์ ์ ๊ณต!?โ
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ํ์ธํ์ธ์.
https://github.com/scikit-learn/scikit-learn/issues/6122.
์ ํ์ฌ ๋ถ๊ฐ๋ฅ!? FeatureUnion์ get_feature_names()
๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฉฐ ์๋ํด์ผ ํฉ๋๋ค!
์๋ง๋ sklearn์ ๋ชจ๋ ๋ชจ๋ธ์๋ fit
๋ฐ transform
๊ธฐ๋ฅ์ด ์๊ณ FeatureUnion์ ๋ฃ์ ์ ์๊ณ ์ ์๋ํ ์ ์๋ ๋ชจ๋ ๋ชจ๋ธ์ ๋ง๋ค์ด์ผ ํ๋ฉฐ ์์ฑ์ ์์ค๋ก ์ ๊ณตํด์ผ ํฉ๋๋ค. get_feature_names()
if not hasattr(trans, 'get_feature_names'):
$#$ ๋ฅผ ํธ์ถํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด FeatureUnion์ด get_feature_names()
์ ๊ธฐ๋ฅ์ ๋ฐ๋์ ์ ๊ณตํ์ง๋ ์์ต๋๋ค!!
์ด๊ฒ์ PCA์ ํน์ ๋ฌธ์ ๋ฅผ ์ง์ ํด๊ฒฐํ์ง ๋ชปํ ์๋ ์์ง๋ง, ๊ทํ์ ์ง๋ฌธ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ฝ์ผ๋ฉด ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ์ ์ ์ ํ์ดํ๋ผ์ธ์ ํตํด ์์ฑ์ ์ฌ๊ณผํ๋ ๋ฐฉ๋ฒ์ด ๊ถ๊ธํ ๊ฒ์ ๋๋ค.
๋น์ฌ์์๊ฒ ๋ฆ์์ง๋ง ํ์ดํ๋ผ์ธ ๊ตฌ์กฐ๋ฅผ ์ดํด๋ณด๊ณ ์ ์ ํ ๋จ๊ณ(featureunion ๋ด์์๋)๋ฅผ ์ฐพ์ ๋ค์ ์ ์ ํ ์์ฑ์ ์ฌ์ฉํ์ฌ ์ผ๋ง๋ ๋ณต์กํ์ง์ ๊ด๊ณ์์ด ํ์ดํ๋ผ์ธ ๋ด์ ์์์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ๋ค์์ ๋ฐฉ๊ธ ์คํํ ์์ ๋๋ค.
pipeline = Pipeline([
('union', FeatureUnion([
('categoric', Pipeline([
('f_cat', feature_type_split(type = 'categoric')), #returns categoric in array for vect
('vect', vect),
])),
('numeric', Pipeline([
('f_num', feature_type_split(type = 'numeric')),
])),
])),
('select', ff),
('tree_clf', clf),
])
print(pipeline)๋ฅผ ํตํด ํ์ดํ๋ผ์ธ ๊ฐ์ฒด ์์ฒด๋ฅผ ํ์ํ๋ฉด ์ฐธ์กฐ ํฌ์ธํธ๊ฐ ์ ๊ณต๋ฉ๋๋ค.
Pipeline(steps=[('union', FeatureUnion(n_jobs=1,
transformer_list=[('categoric', Pipeline(steps=[('f_cat', feature_type_split(type='categoric')), ('vect', DictVectorizer(dtype=<type 'numpy.float64'>, separator='=', sort=True,
sparse=True))])), ('numeric', Pipeline(steps=[('f_num', feature_type...it=2, min_weight_fraction_leaf=0.0,
presort=False, random_state=None, splitter='best'))])
๊ทธ๋์ ๋ค์์ ํตํด ํตํฉ ๋จ๊ณ๋ก ์ด๋ํฉ๋๋ค.
pipeline.named_steps['union']
๊ทธ๋ฐ ๋ค์ ๋ค์์ ํตํด ๋ณํ๊ธฐ_๋ชฉ๋ก(๋๋ ๋ฒ์ฃผํ ํ์ดํ๋ผ์ธ)์ธ ๋ค์ ์์ค์ผ๋ก ์ด๋ํฉ๋๋ค.
pipeline.named_steps['union'].transformer_list[0]
๊ทธ๋ฐ ๋ค์ ๋ค์์ ํตํด ๋ฒ์ฃผํ ํ์ดํ๋ผ์ธ ๋ด์ ๋จ๊ณ์ธ ๋ค์ ์์ค์ผ๋ก ์ด๋ํฉ๋๋ค.
pipeline.named_steps['union'].transformer_list[0][1]
์์ ๊ฒฐ๊ณผ๋ ์ด์ named_steps๋ฅผ ํ์ฉํ ์ ์๋ ์ผ๋ฐ์ ์ธ ํ์ดํ๋ผ์ธ ๊ตฌ์กฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
print pipeline.named_steps['union'].transformer_list[0][1].named_steps['vect']
๋ฐ๋ผ์ ๋ค์์ ํตํด ํ์ํ ์์ฑ์ ์ก์ธ์คํฉ๋๋ค.
print pipeline.named_steps['union'].transformer_list[0][1].named_steps['vect'].get_feature_names()
TLDR;
์ฌ์ฉ์ ์ง์ ํ์ดํ๋ผ์ธ์ ์ฌ์ฉํ์ฌ ํ์ดํ๋ผ์ธ ๊ตฌ์กฐ๋ฅผ ํ๋์ฉ ์ดํด๋ณด๊ณ ํด๋น ๋ณํ/์ถ์ ๊ธฐ ๋ถ๋ถ์ ๋ํด ์ผ๋ฐ์ ์ผ๋ก ํ๋ ๊ฒ์ฒ๋ผ ์์ฑ์ ์ก์ธ์คํฉ๋๋ค.
scikit-learn์ get_feature_names
๊ฐ ์๋ํ์ง ์๋ ๊ฒฝ์ฐ์ ์๋ํ ์ ์๋ eli5
์ transform_feature_names
๋ฅผ ์๋ํ์ญ์์ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด๊ฒ์ PCA์ ํน์ ๋ฌธ์ ๋ฅผ ์ง์ ํด๊ฒฐํ์ง ๋ชปํ ์๋ ์์ง๋ง, ๊ทํ์ ์ง๋ฌธ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ฝ์ผ๋ฉด ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ์ ์ ์ ํ์ดํ๋ผ์ธ์ ํตํด ์์ฑ์ ์ฌ๊ณผํ๋ ๋ฐฉ๋ฒ์ด ๊ถ๊ธํ ๊ฒ์ ๋๋ค.
๋น์ฌ์์๊ฒ ๋ฆ์์ง๋ง ํ์ดํ๋ผ์ธ ๊ตฌ์กฐ๋ฅผ ์ดํด๋ณด๊ณ ์ ์ ํ ๋จ๊ณ(featureunion ๋ด์์๋)๋ฅผ ์ฐพ์ ๋ค์ ์ ์ ํ ์์ฑ์ ์ฌ์ฉํ์ฌ ์ผ๋ง๋ ๋ณต์กํ์ง์ ๊ด๊ณ์์ด ํ์ดํ๋ผ์ธ ๋ด์ ์์์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ๋ค์์ ๋ฐฉ๊ธ ์คํํ ์์ ๋๋ค.
pipeline = Pipeline([ ('union', FeatureUnion([ ('categoric', Pipeline([ ('f_cat', feature_type_split(type = 'categoric')), #returns categoric in array for vect ('vect', vect), ])), ('numeric', Pipeline([ ('f_num', feature_type_split(type = 'numeric')), ])), ])), ('select', ff), ('tree_clf', clf), ])
print(pipeline)๋ฅผ ํตํด ํ์ดํ๋ผ์ธ ๊ฐ์ฒด ์์ฒด๋ฅผ ํ์ํ๋ฉด ์ฐธ์กฐ ํฌ์ธํธ๊ฐ ์ ๊ณต๋ฉ๋๋ค.
Pipeline(steps=[('union', FeatureUnion(n_jobs=1, transformer_list=[('categoric', Pipeline(steps=[('f_cat', feature_type_split(type='categoric')), ('vect', DictVectorizer(dtype=<type 'numpy.float64'>, separator='=', sort=True, sparse=True))])), ('numeric', Pipeline(steps=[('f_num', feature_type...it=2, min_weight_fraction_leaf=0.0, presort=False, random_state=None, splitter='best'))])
๊ทธ๋์ ๋ค์์ ํตํด ํตํฉ ๋จ๊ณ๋ก ์ด๋ํฉ๋๋ค.
pipeline.named_steps['union']
๊ทธ๋ฐ ๋ค์ ๋ค์์ ํตํด ๋ณํ๊ธฐ_๋ชฉ๋ก(๋๋ ๋ฒ์ฃผํ ํ์ดํ๋ผ์ธ)์ธ ๋ค์ ์์ค์ผ๋ก ์ด๋ํฉ๋๋ค.
pipeline.named_steps['union'].transformer_list[0]
๊ทธ๋ฐ ๋ค์ ๋ค์์ ํตํด ๋ฒ์ฃผํ ํ์ดํ๋ผ์ธ ๋ด์ ๋จ๊ณ์ธ ๋ค์ ์์ค์ผ๋ก ์ด๋ํฉ๋๋ค.
pipeline.named_steps['union'].transformer_list[0][1]
์์ ๊ฒฐ๊ณผ๋ ์ด์ named_steps๋ฅผ ํ์ฉํ ์ ์๋ ์ผ๋ฐ์ ์ธ ํ์ดํ๋ผ์ธ ๊ตฌ์กฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
print pipeline.named_steps['union'].transformer_list[0][1].named_steps['vect']
๋ฐ๋ผ์ ๋ค์์ ํตํด ํ์ํ ์์ฑ์ ์ก์ธ์คํฉ๋๋ค.
print pipeline.named_steps['union'].transformer_list[0][1].named_steps['vect'].get_feature_names()
TLDR;
์ฌ์ฉ์ ์ง์ ํ์ดํ๋ผ์ธ์ ์ฌ์ฉํ์ฌ ํ์ดํ๋ผ์ธ ๊ตฌ์กฐ๋ฅผ ํ๋์ฉ ์ดํด๋ณด๊ณ ํด๋น ๋ณํ/์ถ์ ๊ธฐ ๋ถ๋ถ์ ๋ํด ์ผ๋ฐ์ ์ผ๋ก ํ๋ ๊ฒ์ฒ๋ผ ์์ฑ์ ์ก์ธ์คํฉ๋๋ค.