2025. 6. 11. 17:57ㆍ카테고리 없음
AutoGluon은 Amazon Web Services(AWS)에서 개발한 AutoML(Auto Machine Learning) 프레임워크이다. 복잡한 하이퍼파라미터 튜닝, 모델 앙상블, 데이터 전처리 등의 과정을 자동으로 수행해주며, 초보자부터 전문가까지 쉽게 활용할 수 있도록 설계되었다.
1. AutoGluon의 특징
① 코드 몇 줄로 강력한 성능
AutoGluon은 단 몇 줄의 코드만으로도 강력한 베이스라인 모델을 빠르게 생성할 수 있다. fit() 함수를 통해 전처리, 모델 학습, 앙상블, 검증까지 자동으로 수행되므로 생산성이 매우 높다.
② 다양한 모델 앙상블
랜덤포레스트, XGBoost, LightGBM, CatBoost, 신경망 등 다양한 알고리즘을 자동으로 탐색하고 앙상블한다. 이로 인해 단일 모델보다 더 높은 예측 정확도를 기대할 수 있다.
③ Tabular, Text, Image 지원
AutoGluon은 범용적인 프레임워크이다. 구조화된 데이터(Tabular), 이미지(Image Classification), 텍스트(Text Classification) 등 다양한 도메인에 적용이 가능하다.
pip install autogluon
from autogluon.tabular import TabularPredictor
predictor = TabularPredictor(label='target').fit(train_data)
preds = predictor.predict(test_data)
위 코드만으로 데이터 학습과 예측을 한 번에 수행할 수 있다.
이 코드를 작성하여서 기본 베이스 라인을 만들고 추가적인 모델링으로 전처리를 진행하던가, Optuna 같은 하이퍼파라미터 튜닝도 호환이 가능하다.
현재 진행중인 데이콘의 사이버 공격 유형 예측 모델링 챌린지에 적용할수있다.
물론, 규칙에 AutoML 유형의 패키지 사용 불가능이라고 되어있기때문에, 결과 제출을 하면 안되지만, 성능 비교로는 가능할것이다.
간단한 코드 예시를 들어보겠다.
먼저 라이브러리를 불러온다.
!pip install autogluon --quiet
import pandas as pd
import numpy as np
from collections import Counter
그 다음 데이터를 불러온다.
train = pd.read_csv('/train.csv')
test = pd.read_csv('/test.csv')
autogluon같은 경우 결측/범주형을 자동처리하지만, 파생변수 추가와 그에따른 전처리는 필요하다.
# IP: 사설 여부
import ipaddress
def is_private_ip(ip):
try:
return int(ipaddress.ip_address(ip).is_private)
except:
return 0
for df in [train, test]:
df['ip_src_private'] = df['ip_src'].apply(is_private_ip)
df['ip_dst_private'] = df['ip_dst'].apply(is_private_ip)
# 불필요 컬럼 제거
df.drop(['ip_src', 'ip_dst', 'ID'], axis=1, errors='ignore', inplace=True)
# 간단 파생 피처
for df in [train, test]:
df['rate_ratio_pkts'] = df['rate_fwd_pkts'] / (df['rate_bwd_pkts']+1e-6)
df['is_common_port'] = df['port_dst'].isin([21,22,23,25,53,80,110,443]).astype(int)
df['duration_log'] = np.log1p(df['duration'])
그 다음으로 autogluon 라이브러리를 통해 학습을 진행하면 된다.
from autogluon.tabular import TabularPredictor
label = 'attack_type'
metric = 'f1_macro' # macro F1로 직접 튠
presets = 'best_quality'
predictor = TabularPredictor(
label=label,
eval_metric=metric,
problem_type='multiclass'
).fit(
train_data=train,
sample_weight='sample_weight',
presets=presets,
time_limit=2*60*60, # 2시간 제한 예시, Colab 돌리면 충분
num_bag_folds=5,
num_stack_levels=1
)
마지막으로 검증 점수를 확인한다.
predictor.leaderboard(silent=True)
이런식으로 autogluon을 사용하여 베이스라인을 만들고 추가적인 튜닝을 통해서 모델링을 진행할 수 있다.