Created
September 11, 2025 14:33
-
-
Save dongwooklee96/7321643e1d17d9f0b6546d114c59c0b7 to your computer and use it in GitHub Desktop.
딥러닝 앙상블
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.ensemble import VotingClassifier | |
from sklearn.datasets import make_classification | |
from sklearn.model_selection import train_test_split, cross_val_score | |
from sklearn.preprocessing import StandardScaler | |
from skorch import NeuralNetClassifier | |
import torch | |
import torch.nn as nn | |
# 샘플 데이터 생성 | |
X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42) | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
# 데이터 정규화 | |
scaler = StandardScaler() | |
X_train = scaler.fit_transform(X_train) | |
X_test = scaler.transform(X_test) | |
# PyTorch 모델 정의 | |
class Net1(nn.Module): | |
def __init__(self): | |
super(Net1, self).__init__() | |
self.fc1 = nn.Linear(20, 64) | |
self.dropout1 = nn.Dropout(0.3) # Dropout 추가 | |
self.fc2 = nn.Linear(64, 32) | |
self.dropout2 = nn.Dropout(0.2) | |
self.fc3 = nn.Linear(32, 2) | |
def forward(self, x): | |
x = torch.relu(self.fc1(x)) | |
x = self.dropout1(x) | |
x = torch.relu(self.fc2(x)) | |
x = self.dropout2(x) | |
x = self.fc3(x) | |
return x | |
class Net2(nn.Module): | |
def __init__(self): | |
super(Net2, self).__init__() | |
self.fc1 = nn.Linear(20, 128) | |
self.dropout1 = nn.Dropout(0.4) # Dropout 추가 | |
self.fc2 = nn.Linear(128, 64) | |
self.dropout2 = nn.Dropout(0.3) | |
self.fc3 = nn.Linear(64, 2) | |
def forward(self, x): | |
x = torch.relu(self.fc1(x)) | |
x = self.dropout1(x) | |
x = torch.relu(self.fc2(x)) | |
x = self.dropout2(x) | |
x = self.fc3(x) | |
return x | |
# skorch로 래핑, 하이퍼파라미터 다양화 | |
model_1 = NeuralNetClassifier( | |
Net1, | |
max_epochs=20, # 에포크 증가 | |
lr=0.001, | |
optimizer=torch.optim.Adam, # Adam 최적화 | |
verbose=0 | |
) | |
model_2 = NeuralNetClassifier( | |
Net2, | |
max_epochs=20, | |
lr=0.002, # 다른 학습률 | |
optimizer=torch.optim.Adam, | |
verbose=0 | |
) | |
# VotingClassifier 생성 (가중치 적용) | |
ensemble = VotingClassifier( | |
estimators=[ | |
('model_1', model_1), | |
('model_2', model_2) | |
], | |
voting='soft', | |
weights=[0.6, 0.4] # model_1에 더 높은 가중치 부여 | |
) | |
# 교차 검증으로 성능 평가 | |
cv_scores = cross_val_score(ensemble, X_train.astype(np.float32), y_train, cv=5, scoring='accuracy') | |
print(f"교차 검증 정확도: {cv_scores.mean():.4f} (±{cv_scores.std():.4f})") | |
# 모델 학습 | |
ensemble.fit(X_train.astype(np.float32), y_train) | |
# 테스트 데이터로 최종 평가 | |
print("앙상블 모델 테스트 정확도:", ensemble.score(X_test.astype(np.float32), y_test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment