반응형
LOF
Local Outlier Factor는 데이터 분석 및 머신러닝에서 사용되는 비지도 학습 알고리즘으로, 데이터 포인트가 해당 포인트의 근처에 있는 다른 데이터 포인트와 비교하여 얼마나 이상적인지(또는 비정상적인지)를 평가합니다.
import matplotlib.pyplot as plt
import numpy as np
from sklearn.neighbors import LocalOutlierFactor
# Generate sample data
np.random.seed(42)
X_inliers = 0.3 * np.random.randn(100, 2)
X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2))
X = np.r_[X_inliers + 2, X_inliers - 2, X_outliers]
# Fit the model
clf = LocalOutlierFactor(n_neighbors=20, contamination=0.1)
y_pred = clf.fit_predict(X)
LOF_scores = -clf.negative_outlier_factor_
# Plot the results
plt.figure(figsize=(10, 6))
# Inliers
plt.scatter(X[y_pred == 1, 0], X[y_pred == 1, 1], color='b', s=50, edgecolor='k', label='Inliers')
# Outliers
plt.scatter(X[y_pred == -1, 0], X[y_pred == -1, 1], color='r', s=50, edgecolor='k', label='Outliers')
# Plot LOF scores
radius = (LOF_scores.max() - LOF_scores) / (LOF_scores.max() - LOF_scores.min())
plt.scatter(X[:, 0], X[:, 1], s=1000 * radius, edgecolors='r', facecolors='none', label='LOF Score')
plt.title("Local Outlier Factor (LOF)")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.grid(True)
plt.show()
- 파란색 점: 정상 데이터 (Inliers)
- 빨간색 점: 이상 데이터 (Outliers)
- 원 크기: 각 데이터 포인트의 LOF 점수를 반영. 큰 원일수록 LOF 점수가 높아 해당 포인트가 더 비정상적이라는 것을 의미.
반응형
'Machine Learning' 카테고리의 다른 글
MLOps: 머신러닝 운영의 핵심 기능과 도구 총정리 (0) | 2025.05.29 |
---|---|
머신러닝 상품화를 위한 ML LifeCycle 총정리 (0) | 2025.05.29 |
이상치(Outlier), 이상(Abnormal) 비교 (1) | 2024.06.05 |
Optuna 라이브러리에서 suggest_int 메서드 (0) | 2023.07.17 |
데이터분석 프로젝트 프로세스 (0) | 2023.06.29 |