異常點檢測

2021-09-23 15:31:38 字數 1908 閱讀 4618

import numpy as np

from sklearn.ensemble import isolationforest

import matplotlib.pyplot as plt

np.random.seed(29)

x = np.random.normal(size=[100,2])

x = 0.05*x

x1 = x+2

x2 = x-2

訓練資料

x_train = np.vstack([x1,x2])

異常資料

x_error = np.random.uniform(-3.5,3.5,size=[20,2])

測試資料

x_test = 0.05 *np.random.normal(size=[20,2])

x_test = np.vstack([x_test+2,x_test-2])

istf = isolationforest(n_estimators=100, random_state=28, contamination=0.05)

istf.fit(x_train)

x_train_pr=istf.predict(x_train)

x_error_pr=istf.predict(x_error)

x_test_pr=istf.predict(x_test)

print(「正常資料的**值:{}」.format(x_train_pr))

print(「測試正常資料的**值:{}」.format(x_test_pr))

print(「測試異常資料的**值:{}」.format(x_error_pr))

train_decision = istf.decision_function(x_train)

test_decision = istf.decision_function(x_test)

outliers_decision = istf.decision_function(x_error)

print(「正常資料的**值:\n{}」.format(train_decision))

print(「測試正常資料的**值:\n{}」.format(test_decision))

print(「測試異常資料的**值:\n{}」.format(outliers_decision))

estimators_ = istf.estimators_

print(estimators_)

estimators_features_ = istf.estimators_features_

print(estimators_features_)

t1 = np.linspace(-4, 4, 50)

t2 = np.linspace(-4, 4, 50)

x1, x2 = np.meshgrid(t1, t2) # 產生網格點的座標

異常點檢測演算法

異常點檢測演算法 對於train data 中的資料,對其中重要的特徵 或者每個特徵 x1,x2,xn,計算其高斯分布 對new data,計算 x 每個特徵 在訓練資料分布下的 p 值並相乘,若p x 小於某個臨界值,則判斷其為異常點 什麼時候選擇使用 p x 根據分布概率來判斷異常點,什麼時候使...

python離群點檢測例子 異常點 離群點檢測演算法

異常點 離群點檢測演算法 瀏覽次數 456 sklearn中關於異常檢測的方法主要有兩種 1 novelty detection 當訓練資料中沒有離群點,我們的目標是用訓練好的模型去檢測另外新發現的樣本 2 outlier detection 當訓練資料中包含離群點,模型訓練時要匹配訓練資料的中心樣...

資料探勘之異常點檢測

異常點檢測方法 一 基本概念 異常物件被稱作離群點。異常檢測也稱偏差檢測和例外挖掘。常見的異常成因 資料 於不同的類 異常物件來自於乙個與大多數資料物件源 類 不同的源 類 的思想 自然變異,以及資料測量或收集誤差。異常檢測的方法 1 基於模型的技術 首先建立乙個資料模型,異常是那些同模型不能完美擬...