Skip to content

I created 20 anomaly detection models and evaluated their performance using randomly generated table and time-series data.

Notifications You must be signed in to change notification settings

taejongK/AnomalyDetectionModels

Repository files navigation

AI 이상감지 및 예지보전 모델 20개 개발

개발 현황

모델

번호 모델 진행상황 데이터 비고
1 CNN 완료 시계열
2 ARIMA 완료 시계열
3 DNN 완료 테이블
4 DNN AE 완료 시계열
5 Isolation Forest 완료 테이블
6 PCA + k-means clustering 완료 테이블
7 SVM 완료 테이블
8 LDA 완료 테이블
9 LSTM 완료 시계열
10 one-class SVM 완료 테이블
11 DeepSVDD 완료 테이블
12 Transformer 완료 시계열
13 DeepSAD + DNN 완료 테이블
14 DeepSAD + LSTM 완료 시계열
15 Random Forest 완료 시계열
16 LOF 완료 테이블
17 XGBoost 완료 시계열
18 LightGBM 완료 시계열
19 CatBoost 완료 시계열
20 GMM 완료 테이블

실행 방법

터미널에서 아래 명령어 실행

  1. git clone
git clone git@gitlab.brique.kr:dst/da/brique_product.git
  1. 디렉토리 이동
cd brique_product/patent
  1. 필요 라이브러리 설치
pip install -r requirements.txt
  1. 전체 모델 학습
python3 Train.py
  1. 시나리오 MainScenario.ipynb 파일을 실행하여 결과 확인

데이터

  1. Tabular Data
    image.png

  2. Time Series Data
    image.png

시나리오

MainScenario.ipynb 파일에 시나리오를 작성하였습니다.

데이터 생성

Tabular Data

  1. 선형으로 구분되는 데이터

image.png

  • code

    from pyod.utils.data import generate_data
    
    # 데이터 생성
    X, _, y, _ = generate_data(
        n_train=500,         # 학습 데이터 수
        n_features=2,        # 피처 수 (차원)
        contamination=0.1,   # 이상치 비율 (10%)
        random_state=42      # 랜덤 시드
    )
    
    # 데이터 시각화
    plt.figure(figsize=(8, 6))
    plt.scatter(X[y == 0, 0], X[y == 0, 1], label='Normal', alpha=0.7, s=15)
    plt.scatter(X[y == 1, 0], X[y == 1, 1], label='Outliers', alpha=0.7, s=15, color='red')
    plt.title("Generated Data with Outliers")
    plt.xlabel("Feature 1")
    plt.ylabel("Feature 2")
    plt.legend()
    plt.show()

Time Seriese Data

  • code

    import pandas as pd
    
    # 데이터 생성 함수
    def create_time_series(n_points=2, add_noise=False, noise_level=0.1):
        index = np.arange(n_points)
        time = pd.date_range(start='2020-03-09 16:16:30', periods=n_points, freq='s')
        if add_noise == False:
            data = np.sin(0.1 * index - np.pi/2)
        else:
            data = np.sin(0.1 * index) + np.random.normal(scale=noise_level, size=n_points)
        return pd.DataFrame({'time': time, 'value': data})
    
    # Point anomaly 추가
    def add_point_anomaly(data, anomaly_index, anomaly_value):
        data.loc[data.index==anomaly_index,'value'] = anomaly_value
        return data
    
    # Contextual anomaly 추가
    def add_contextual_anomaly(data, start_index, end_index, anomaly_value):
        data.loc[start_index:end_index, 'value'] = anomaly_value
        return data
    
    # Collective anomaly 추가
    def add_collective_anomaly(data, start_index, end_index, anomaly_type='linear'):
        if anomaly_type == 'linear':
            data.loc[start_index:end_index-1, 'value'] = np.linspace(0.5, -0.5, end_index - start_index)
        elif anomaly_type == 'exponential':
            data.loc[start_index:end_index-1, 'value'] = np.exp(np.linspace(-2, -0.5, end_index - start_index))
        return data
    
    # normal_data.plot(x='time', y='value', figsize=(12, 6), title='Normal Data add Noise')
    n_points = 500
    
    # 데이터 생성
    normal_data = create_time_series(n_points=n_points, add_noise=True, noise_level=0.1) # 정상 데이터
    point_anomaly_data = add_point_anomaly(normal_data.copy(), anomaly_index=250, anomaly_value=2.0) # Point anomaly
    contextual_anomaly_data = add_contextual_anomaly(normal_data.copy(), start_index=250, end_index=300, anomaly_value=2.0) # Contextual anomaly
    collective_anomaly_data = add_collective_anomaly(normal_data.copy(), start_index=250, end_index=300, anomaly_type='linear') # Collective anomaly
    
    # 데이터 시각화
    fig, ax = plt.subplots(4, 1, figsize=(12, 12))
    normal_data.plot(x='time', y='value', ax=ax[0], title='Normal Data')
    point_anomaly_data.plot(x='time', y='value', ax=ax[1], title='Point Anomaly')
    contextual_anomaly_data.plot(x='time', y='value', ax=ax[2], title='Contextual Anomaly')
    collective_anomaly_data.plot(x='time', y='value', ax=ax[3], title='Collective Anomaly')
    plt.tight_layout()
    plt.show()

샘플 모델 테스트 (Random Forest)

Data

image.png

Model

# SVM 모델 학습
svm_model = SVC(kernel='rbf', C=1.0, random_state=config.random_state)

# LDA
lda_model = LinearDiscriminantAnalysis()

Predict Results

image.png

image.png

Evaluate

Model Accuracy Precision Recall F1
SVM 1.0 1.0 1.0 1.0
LDA 1.0 1.0 1.0 1.0

image.png

About

I created 20 anomaly detection models and evaluated their performance using randomly generated table and time-series data.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published