250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- subdag
- API Gateway
- Counterfactual Explanations
- 유튜브 API
- session 유지
- XAI
- tensorflow text
- grad-cam
- BigQuery
- Airflow
- chatGPT
- gather_nd
- GenericGBQException
- API
- Retry
- 공분산
- UDF
- GCP
- login crawling
- airflow subdag
- top_k
- 상관관계
- TensorFlow
- correlation
- integrated gradient
- youtube data
- hadoop
- spark udf
- flask
- requests
Archives
- Today
- Total
데이터과학 삼학년
[scikit-learn] 카테고리 변수 embedding endcoder 적용 본문
반응형
Embedding Encoder는 범주형 변수를 다루는 강력한 라이브러리로, 간단한 사용법과 scikit-learn과의 호환성을 제공
sklearn 자체에는 범주형 변수를 임베딩으로 다룰 수 있는 기능이 아직 없음
해당 라이브러리를 이용하면 sklearn의 사용 방법을 그대로 따르면서 임베딩으로 전처리도 가능하게 구성할 수 있음
Embedding Encoder란?
Embedding Encoder는 scikit-learn의 transformer와 유사하게 동작하지만, y를 신경망의 타겟으로 사용한다는 차이.
모든 입력 열이 범주형이라고 가정하고 각 열에 대한 임베딩을 계산
간단한 사용 예제
from embedding_encoder import EmbeddingEncoder
# Regression 또는 Classification 선택
ee = EmbeddingEncoder(task="regression") # or "classification"
ee.fit(X=X, y=y)
output = ee.transform(X=X)
scikit-learn과의 호환성
Embedding Encoder는 scikit-learn과 호환되므로 파이프라인에 쉽게 통합 가능
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from embedding_encoder import EmbeddingEncoder
# 데이터셋 로드 및 전처리
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Embedding Encoder 파이프라인 구성
ee = EmbeddingEncoder(task="classification")
num_pipe = make_pipeline(SimpleImputer(strategy="mean"), StandardScaler())
cat_pipe = make_pipeline(SimpleImputer(strategy="most_frequent"), ee)
col_transformer = ColumnTransformer([("num_transformer", num_pipe, numeric_vars),
("cat_transformer", cat_pipe, categorical_vars)])
# 전체 파이프라인 생성
pipe = make_pipeline(col_transformer, LogisticRegression())
# 하이퍼파라미터 최적화를 위한 그리드 서치 설정
param_grid = {
"columntransformer__cat__embeddingencoder__layers_units": [
[64, 32, 16],
[16, 8],
]
}
cv = GridSearchCV(pipe, param_grid)
# 파이프라인 학습 및 평가
cv.fit(X_train, y_train)
accuracy = cv.score(X_test, y_test)
# 결과 출력
print(f"Accuracy: {accuracy}")
Embedding Encoder를 사용하면 범주형 데이터를 효과적으로 다룰 수 있으며, 이를 통해 모델의 성능을 향상 가능
Embedding Encoder 시각화
from embedding_encoder import EmbeddingEncoder
# Embedding Encoder 초기화
ee = EmbeddingEncoder(task="classification")
# 모델 학습
ee.fit(X=X, y=y)
# PCA를 사용하여 임베딩 시각화
ee.plot_embeddings(variable="...", model="pca")
참조
https://pypi.org/project/embedding-encoder/
embedding-encoder
scikit-learn compatible transformer that turns categorical features into dense numeric embeddings
pypi.org
728x90
반응형
LIST
'Machine Learning' 카테고리의 다른 글
[tensorflow in spark] spark를 이용해 tf model을 분산 처리?! (0) | 2024.03.08 |
---|---|
[sklearn in spark] spark분산환경을 이용한 모델 예측 (0) | 2024.03.07 |
ROC, Precision-Recall Curve for Multi classification (0) | 2024.02.20 |
Inductive Learning vs Transductive Learning (1) | 2024.02.09 |
다중공선성 확인 및 처리 방법 (0) | 2023.11.06 |
Comments