차원 축소 전략 비교

Beginner

This tutorial is from open-source community. Access the source code

소개

이 실험실에서는 베이지안릿지 (BayesianRidge) 를 지도 학습 추정기로 사용하는 회귀 문제에서 특징 응집 (feature agglomeration) 과 ANOVA 를 사용한 단변량 특징 선택 (univariate feature selection) 이라는 두 가지 차원 축소 전략을 비교합니다.

VM 팁

VM 시작이 완료되면 왼쪽 상단 모서리를 클릭하여 연습을 위해 Jupyter Notebook에 접속할 수 있는 Notebook 탭으로 전환합니다.

때때로 Jupyter Notebook 이 완전히 로드되기까지 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한으로 인해 작업의 유효성 검사를 자동화할 수 없습니다.

학습 중 문제가 발생하면 Labby 에 문의하십시오. 세션 후 피드백을 제공하면 문제를 신속하게 해결해 드리겠습니다.

매개변수 설정

n_samples = 200
size = 40  ## 이미지 크기
roi_size = 15
snr = 5.0
np.random.seed(0)

데이터 생성

coef = np.zeros((size, size))
coef[0:roi_size, 0:roi_size] = -1.0
coef[-roi_size:, -roi_size:] = 1.0

X = np.random.randn(n_samples, size**2)
for x in X:  ## 데이터를 부드럽게 처리
    x[:] = ndimage.gaussian_filter(x.reshape(size, size), sigma=1.0).ravel()
X -= X.mean(axis=0)
X /= X.std(axis=0)

y = np.dot(X, coef.ravel())

노이즈 추가

noise = np.random.randn(y.shape[0])
noise_coef = (linalg.norm(y, 2) / np.exp(snr / 20.0)) / linalg.norm(noise, 2)
y += noise_coef * noise

그리드 서치를 사용한 베이지안 릿지 계수 계산

cv = KFold(2)  ## 모델 선택을 위한 교차 검증 생성기
ridge = BayesianRidge()
cachedir = tempfile.mkdtemp()
mem = Memory(location=cachedir, verbose=1)

Ward 응집 계층적 군집화 후 BayesianRidge

connectivity = grid_to_graph(n_x=size, n_y=size)
ward = FeatureAgglomeration(n_clusters=10, connectivity=connectivity, memory=mem)
clf = Pipeline([("ward", ward), ("ridge", ridge)])
## 그리드 서치를 통해 최적의 영역 수 선택
clf = GridSearchCV(clf, {"ward__n_clusters": [10, 20, 30]}, n_jobs=1, cv=cv)
clf.fit(X, y)  ## 최적의 매개변수 설정
coef_ = clf.best_estimator_.steps[-1][1].coef_
coef_ = clf.best_estimator_.steps[0][1].inverse_transform(coef_)
coef_agglomeration_ = coef_.reshape(size, size)

ANOVA 단변량 특징 선택 후 BayesianRidge

f_regression = mem.cache(feature_selection.f_regression)  ## 함수 캐싱
anova = feature_selection.SelectPercentile(f_regression)
clf = Pipeline([("anova", anova), ("ridge", ridge)])
## 그리드 서치를 통해 최적의 특징 비율 선택
clf = GridSearchCV(clf, {"anova__percentile": [5, 10, 20]}, cv=cv)
clf.fit(X, y)  ## 최적의 매개변수 설정
coef_ = clf.best_estimator_.steps[-1][1].coef_
coef_ = clf.best_estimator_.steps[0][1].inverse_transform(coef_.reshape(1, -1))
coef_selection_ = coef_.reshape(size, size)

결과 이미지로 시각화

plt.close("all")
plt.figure(figsize=(7.3, 2.7))
plt.subplot(1, 3, 1)
plt.imshow(coef, interpolation="nearest", cmap=plt.cm.RdBu_r)
plt.title("실제 가중치")
plt.subplot(1, 3, 2)
plt.imshow(coef_selection_, interpolation="nearest", cmap=plt.cm.RdBu_r)
plt.title("특징 선택")
plt.subplot(1, 3, 3)
plt.imshow(coef_agglomeration_, interpolation="nearest", cmap=plt.cm.RdBu_r)
plt.title("특징 응집")
plt.subplots_adjust(0.04, 0.0, 0.98, 0.94, 0.16, 0.26)
plt.show()

요약

이 실험은 회귀 문제에서 베이지안릿지 (BayesianRidge) 를 지도 학습 추정자로 사용하여 특징 응집과 ANOVA 를 사용한 단변량 특징 선택이라는 두 가지 차원 축소 전략을 비교합니다. 결과는 이미지에 표시되어 실제 가중치, 특징 선택 및 특징 응집을 보여줍니다.