Aykırı değer sınıflandırıcı toplulukları

Python ile Anomali Tespiti

Bekhruz (Bex) Tuychiev

Kaggle Master, Data Science Content Creator

Airbnb’ye geri dönüş

outliers = airbnb_df[iforest.labels_ == 1]

outlier_probs = iforest.predict_proba(outliers)
outlier_probs[:10]
array([[0.51999538, 0.48000462],
       [0.61789522, 0.38210478],
       [0.61802032, 0.38197968],
       [0.35184434, 0.64815566],
       [0.57533286, 0.42466714],
       [0.59038933, 0.40961067],
       [0.57677613, 0.42322387],
       [0.54158826, 0.45841174],
       [0.49118093, 0.50881907],
       [0.21387357, 0.78612643]])
Python ile Anomali Tespiti

Olasılık eşiği en iyi uygulama

Eşik:

  • Düşük riskte %75
  • Yüksek maliyetli durumlarda >%90, örn.:
    • tıp
    • siber güvenlik
    • sınırlı veri
Python ile Anomali Tespiti

Topluluk (ensemble) nedir?

  • İki veya daha fazla sınıflandırıcının birleşimi
  • Tahminler daha kararlı
Python ile Anomali Tespiti

Veriye bakın

google.head()
              Open    High     Low   Close    Volume  day_of_week  month  day
Date                                                                         
2006-01-03  211.47  218.05  209.32  217.83  13137450            1      1    3
2006-01-04  222.17  224.70  220.09  222.84  15292353            2      1    4
2006-01-05  223.22  226.00  220.97  225.85  10815661            3      1    5
2006-01-06  228.66  235.49  226.85  233.06  17759521            4      1    6
2006-01-09  233.44  236.94  230.70  233.68  12795837            0      1    9
Python ile Anomali Tespiti

Sayısal özellikleri ölçekleme

from sklearn.preprocessing import QuantileTransformer


# Ölçeklenecek sütunları tanımlayın to_scale = ['Open', 'High', 'Low', 'Close', 'Volume']
# Dönüştürücüyü başlatın qt = QuantileTransformer(output_distribution="normal") # Ölçekleyip sütunları geri yazın google.loc[:, to_scale] = qt.fit_transform(google[to_scale])
Python ile Anomali Tespiti

Diziler oluşturma

# Tahminleyici listesini oluşturun
estimators = [KNN(n_neighbors=20), LOF(n_neighbors=20), IForest()]

# Boş bir dizi oluşturun shape = (len(google), len(estimators)) probability_scores = np.empty(shape=shape)
Python ile Anomali Tespiti

Döngünün içinde

estimators = [KNN(n_neighbors=20), LOF(n_neighbors=20), IForest()]

shape = (len(google), len(estimators))
probability_scores = np.empty(shape=shape)

# Döngü ve eğitme for index, est in enumerate(estimators): est.fit(google)
# Olasılıkları oluşturun probs = est.predict_proba(google)
# Olasılıkları kaydedin probability_scores[:, index] = probs[:, 1]
Python ile Anomali Tespiti

Toplama - ortalama

mean_scores = np.mean(probability_scores, axis=1)

mean_scores
array([0.20699869, 0.21455413, 0.17166271, ..., 0.31255075, 0.33553513,
       0.32217186])
Python ile Anomali Tespiti

Toplama - medyan

median_scores = np.mean(probability_scores, axis=1)

median_scores
array([0.20699869, 0.21455413, 0.17166271, ..., 0.31255075, 0.33553513,
       0.32217186])
Python ile Anomali Tespiti

Olasılık filtresi

# %75 eşik ile maske oluşturun
is_outlier = median_scores > 0.75

# Aykırı değerleri filtreleyin outliers = google[is_outlier]
len(outliers)
3
Python ile Anomali Tespiti

Adımların özeti

# Tahminleyici listesini oluşturun
estimators = [KNN(n_neighbors=20), LOF(n_neighbors=20), IForest()]
probability_scores = np.empty(shape=(len(google), len(estimators)))


for index, est in enumerate(estimators): # Eğit ve olasılık üret est.fit(google) probs = est.predict_proba(google) # Olasılıkları kaydet probability_scores[:, index] = probs[:, 1]
Python ile Anomali Tespiti

Adımların özeti

# Puanları ortalayın
mean_scores = np.mean(probability_scores, axis=1)

# %75 eşikle filtreleyin
outliers = google[mean_scores > 0.75]

print(len(outliers))
3
Python ile Anomali Tespiti

Hadi pratik yapalım!

Python ile Anomali Tespiti

Preparing Video For Download...