局部異常因子(Local Outlier Factor, LOF)算法詳解及實驗

  局部異常因子(Local Outlier Factor, LOF)通過計算樣本點的局部相對密度來衡量這個樣本點的異常情況,可以算是一類無監督學習算法。下面首先對算法的進行介紹,然後進行實驗。

LOF算法

  下面介紹LOF算法的每個概念,以樣本點集合中的樣本點$P$爲例。下面的概念名稱中都加了一個k-,實際上部分名稱原文沒有加,但是感覺這樣更嚴謹一些。

  k-鄰近距離(k-distance):樣本點$P$與其最近的第$k$個樣本點之間的距離,表示爲$d_k(P)$。其中距離可以用各種方式度量,通常使用歐氏距離。

  k-距離鄰域:以$P$爲圓心,$d_k(P)$爲半徑的鄰域,表示爲$N_k(P)$。

  k-可達距離:$P$到某個樣本點$O$的k-可達距離,取$d_k(O)$或$P$與$O$之間距離的較大值,表示爲

$reach\_dist_k(P,O)=\max\{d_k(O),d(P,O)\}$

  也就是說,如果$P$在$N_k(O)$內部,$reach\_dist_k(P,O)$取$O$的k-鄰近距離$d_k(O)$,在$N_k(O)$外部則取$P$與$O$之間距離$d(P,O)$。需要注意k-可達距離不是對稱的。

  k-局部可達密度(local reachability density, lrd):$P$的k-局部可達密度表示爲

$\displaystyle lrd_k(P)=\left(\frac{\sum\limits_{O\in N_k(P)}reach\_dist_k(P,O)}{|N_k(P)|}\right)^{-1}$

  括號內,分子計算了$P$到其k-距離鄰域內所有樣本點$O$的k-可達距離之和,然後除以$P$的k-距離鄰域內部的樣本點個數進行平均。再加一個倒數,表示爲密度,即$P$到每個點的平均距離越小,密度越大。可以推理出,如果$P$在所有$O$的k-鄰域內部,其局部可達密度即爲

$\displaystyle lrd_k(P)=\left(\frac{\sum\limits_{O\in N_k(P)}d_k(O)}{|N_k(P)|}\right)^{-1}$

  可以看出,如果$P$是一個離羣點,那麼它不太可能存在於$N_k(P)$中各點的k-距離鄰域內,從而導致其局部可達密度偏小;如果$P$不是離羣點,其局部可達密度最大取爲上式。

  實際上我有點奇怪爲什麼要用一個最大值來將距離作一個限制,也就是使用k-可達距離,而不是直接使用距離,即定義局部密度爲下式

$\displaystyle ld_k(P)=\left(\frac{\sum\limits_{O\in N_k(P)}d(O,P)}{|N_k(P)|}\right)^{-1}$

  k-局部異常因子(Local Outlier Factor, LOF):$P$的k-局部異常因子表示爲

$\displaystyle LOF_k(P)=\frac{\frac{1}{|N_k(P)|}\sum\limits_{O\in N_k(P)}lrd_k(O)}{lrd_k(P)}$

  從直覺上理解:當$LOF_k(P)\le1$時,表明$P$處密度比其周圍點大或相當,則$P$是內點;當$LOF_k(P)>1$時,表明$P$處密度比其周圍點小,可以判別爲離羣(異常)點。

實驗

LOF算法實現

  實驗設置樣本維度爲2以便可視化。由於樣本點只包含連續值,實驗默認設置$|N_k(P)|=k$。設置$k=5$,並將閾值設爲2,即將LOF大於2的樣本點視作異常。函數定義、抽樣、計算以及可視化代碼如下。

#%% 定義函數
import torch 
import matplotlib.pyplot as plt

#計算所有樣本點[N, M]之間的距離,得到[N, N]
def get_dists(points:torch.Tensor): 
  x = torch.sum(points**2, 1).reshape(-1, 1)
  y = torch.sum(points**2, 1).reshape(1, -1)
  dists = x + y - 2 * torch.mm(points, points.permute(1,0))
  #數值計算問題,防止對角線小於0
  dists = dists - torch.diag_embed(torch.diag(dists)) 
  return torch.sqrt(dists)

#計算所有樣本的LOF
def get_LOFs(dists:torch.Tensor, k):
  #距離排序,獲取所有樣本點的k-臨近距離
  sorted_dists, sorted_inds = torch.sort(dists, 1, descending=False)
  k_dists = sorted_dists[:, k]
  neighbor_inds = sorted_inds[:, 1:k+1].reshape(-1)
  neighbor_k_dists = k_dists[neighbor_inds].reshape(-1, k)
  neighbor_k_reach_dists = torch.max(neighbor_k_dists, sorted_dists[:, 1:k+1])
  lrds = (neighbor_k_reach_dists.sum(1)/k)**-1
  neighbor_lrds = lrds[neighbor_inds].reshape(-1, k)
  LOFs = neighbor_lrds.sum(1)/k/lrds
  return LOFs

#%% 隨機生成聚集點和異常點
from torch.distributions import MultivariateNormal
torch.manual_seed(0)

crowd_mu_covs = [
  [[0.0, 0.0], [[1.0, 0.0], [0.0, 2.0]], 10],
  [[-10.0, -1.0], [[2.0, 0.8], [0.8, 2.0]], 10],
  [[-10.0, -20.0], [[5.0, -2], [-2, 3.0]], 50],
  [[5.0, -10.0], [[5.0, -2], [-2, 3.0]], 50],
  [[-3.0, -10.0], [[5.0, -2], [-2, 3.0]], 50],
  [[-13.0, -10.0], [[0.3, -0.1], [-0.1, 0.5]], 10],
  [[-4.0, -10.0], [[0.3, -0.1], [-0.1, 0.1]], 100],
]#正態分佈點的均值和方差
outliers = [[5, 5.], [3, 4], [5, -3], [4, -30], [-2, -35], [-10, -35]] #異常點

points = []
for i in crowd_mu_covs:
  mu = torch.tensor(i[0])
  cov = torch.tensor(i[1])
  ps = MultivariateNormal(mu, cov).sample([i[2]]).to('cuda')
  points.append(ps)
for o in outliers:
  points.append(torch.tensor([o]).to('cuda'))
points = torch.cat(points)

#%% 等高線圖
k, threshold = 5, 2
x = torch.arange(-16, 11, 0.5)
y = torch.arange(-36, 6, 0.5)
X, Y = torch.meshgrid(x, y)
Z = torch.zeros_like(X).to('cuda')
M = torch.stack([X,Y]).permute(1,2,0).to('cuda')
for i in range(len(x)):
  for j in range(len(y)):
    ps = torch.cat([points, M[i,j:j+1]])
    dists = get_dists(ps)
    LOFs = get_LOFs(dists, k)
    Z[i,j] = LOFs[-1]
plt.contourf(X.cpu().numpy(),Y.cpu().numpy(),Z.cpu().numpy())

#
dists = get_dists(points)
LOFs = get_LOFs(dists, k)
for i, p in enumerate(points.cpu().numpy()):
  shape, color = '.', 'black'
  if len(points) - i <= len(outliers):
    shape = '^'
    plt.annotate("%.2f"%LOFs[i].cpu().numpy(), (p[0], p[1]))
  if LOFs[i] > threshold:
    color = 'red'
    plt.annotate("%.2f"%LOFs[i].cpu().numpy(), (p[0], p[1]), color='blue')
  plt.plot(p[0], p[1], shape, color=color)
plt.show()

  實驗可視化結果如下圖所示,其中紅色點表示被標爲異常的點,三角點表示實驗設置的真實異常點。

距離代替局部可達距離

  根據前面的疑問,用距離代替局部可達距離進行相應實驗。僅在get_LOFs函數處做了相關改動,並將閾值threshold改爲2.5。get_LOFs函數修改如下。

def get_LOFs(dists:torch.Tensor, k):
  #距離排序,獲取所有樣本點的k-臨近距離
  sorted_dists, sorted_inds = torch.sort(dists, 1, descending=False)
  densities = (sorted_dists[:, 1:k+1].sum(1)/k)**-1
  neighbor_inds = sorted_inds[:, 1:k+1].reshape(-1)
  neighbor_densities = densities[neighbor_inds].reshape(-1, k)
  LOFs = neighbor_densities.sum(1)/k/densities
  return LOFs

  可視化結果如下圖所示

  效果看起來和原始LOF差不多。理論上來說爲什麼要用局部可達距離,本文不再作深究,歡迎前來討論。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章