0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

数列の各数値の異常度を計算する

Last updated at Posted at 2024-10-19

数列の各数値の異常度を計算する

ある数列に平均100、分散30のいくつかの数値、平均300、分散30のいくつかの数値があります、それらを平凡な数値、又は標準的な数値とします
その数列の中に少数、例えば1~3個だけ200、1000といった数値があり、これらを異常な数値とします
この異常度合いを数値化します

手法

数列のあるインデックスの数値とそれ以外の数値との距離平均の逆数を計算し
「値が小さいほどに孤立している」とします
クラスタから孤立している値は距離総和が大きくなる事を利用
孤立している=異常 とする

前提条件

十分なクラスタが形成されている数列であること
人間的な感覚やタスクに依存するため明確な条件は出せないです

用途

あらかじめのクラスターモデルが作成できない状況で少ない計算数で異常値検出したい時
かなり限定的ですがこのような時に役立つと思います

pythonコード


import numpy as np

def create_sample():
    sequence1 = np.random.normal(loc=100, scale=30, size=100)
    sequence2 = np.random.normal(loc=300, scale=30, size=100)
    sequence = np.concatenate([sequence1, sequence2, [200.0]])
    return sequence

def calc_rarity_sub(sequence, index):
    index_value = sequence[index]
    distances = []
    for i, n in enumerate(sequence):
        if i == index:
            continue
        d = (index_value - n) ** 2
        distances.append(d)
    norm = np.mean(distances)
    rarity = 1 / (1 + norm)
    return rarity

def calc_rarity(sequence):
    rarity = []
    for i, n in enumerate(sequence):
        r = calc_rarity_sub(sequence, i)
        rarity.append(r)
    return np.array(rarity)

def main():
    sequence = create_sample()
    rarity = calc_rarity(sequence)
    print(f"sequence={sequence}")
    print(f"rarity={rarity}")
    rarity_indexes = np.argsort(rarity)[::-1][:10]
    print(f"rarity_indexes={rarity_indexes}")
    print(f"rarity values={sequence[rarity_indexes]}")

if __name__ == "__main__":
    main()

出力サンプル

sequence=[124.29876839  75.02881561 138.41220478 101.6527263   94.7371052
 113.42615812  72.66859411  31.25465717 101.33472792  87.29804857
 107.72585006 100.38198613 104.73793237  82.16052739 100.15095879
  64.86018994  49.17057368  98.6835166  107.2462804   -4.5710227
  66.26970438 111.5457916   71.30385694 101.52769522  66.75159355
 118.5111038  119.44054042  99.20009529 106.01769889 112.62551595
  50.65019549  74.72565872  86.19665278  56.32820564 107.05601727
 112.14223614 136.68760951  85.74151346  69.16903356  48.55164721
  98.81719321 110.37497397  81.97966749  83.00045684 160.39796084
 116.09577521 139.77676752 122.4632853  104.63039644 146.83667298
  78.812625    74.62383563  70.87856031  86.87455284 136.96808479
  82.62654582  78.21896433 131.18227641  98.54738597 126.07336047
 105.91849912 100.33466802 136.29223216  57.73718986 109.3984029
 114.37511021  90.94937239  97.47320617  30.75693145 149.21776041
  94.83192864 118.7402205  112.26702322  51.27696882 124.85821305
  83.34389433  90.85403672  69.47568562  95.6086013  114.67465708
 109.57497389 125.88313965 114.95912662 164.46160633 112.06845805
 139.57424985  80.21616764 101.0723674  102.24459071 116.09231528
  79.60918629 127.11563279  68.32004989 111.72746914  80.63959161
 121.82831118  81.69972569  95.72299245 111.55984939  73.28074706
 306.94944466 307.04789077 343.11746125 290.54074108 286.52004874
 296.11784801 279.80461354 292.85099545 279.11612826 276.92270067
 314.77916006 342.98569993 275.02620364 262.04197765 302.85032415
 296.14550883 368.31479524 297.34438305 288.46315398 314.03274277
 277.95280887 299.78791615 294.45724071 350.78805026 311.07847439
 351.94119135 328.97211959 296.33153887 330.52739206 277.66118765
 289.11951872 284.47158643 354.25973431 226.31218746 321.23977812
 350.63775318 273.48299834 261.91030439 370.28854138 293.38827038
 229.31133889 252.8816536  260.49187595 280.33006547 275.95562655
 264.47653569 250.21703647 287.04216932 217.23943659 320.19909938
 301.4945721  275.27950946 358.61011635 323.82708474 250.59404788
 321.9924     299.38045207 236.57675083 255.23406088 289.36421902
 320.61513994 317.77694066 287.44089113 335.07555435 276.92457039
 346.92690031 295.10603743 303.02473492 309.45664387 259.37410308
 252.24891244 284.49113793 335.41237346 313.10354908 235.63433856
 303.19589027 308.51299215 273.2544621  285.97938195 301.53895257
 333.88245429 316.74991927 255.50498219 311.61913162 281.1793757
 242.89622943 301.63236046 249.40417227 284.23907046 301.15366493
 253.89998704 300.27249261 313.70687763 236.34533985 265.76598624
 323.30437587 324.36600336 347.93026193 258.82174833 315.65150503
 200.        ]
rarity=[6.29829797e-05 3.93198917e-05 7.12288171e-05 5.08564220e-05
 4.75628974e-05 5.69271635e-05 3.84428863e-05 2.63054015e-05
 5.07004237e-05 4.42541332e-05 5.39177688e-05 5.02356494e-05
 5.23923007e-05 4.21100169e-05 5.01235380e-05 3.56991237e-05
 3.08645724e-05 4.94168305e-05 5.36704520e-05 1.95393280e-05
 3.61769198e-05 5.59205572e-05 3.79460462e-05 5.07950345e-05
 3.63420076e-05 5.97128627e-05 6.02313870e-05 4.96645438e-05
 5.30411623e-05 5.64969367e-05 3.12845215e-05 3.92059755e-05
 4.37848464e-05 3.29635386e-05 5.35725920e-05 5.62384059e-05
 7.02129380e-05 4.35924596e-05 3.71837145e-05 3.06910037e-05
 4.94808207e-05 5.53005957e-05 4.20366071e-05 4.24527734e-05
 8.35169751e-05 5.83785218e-05 7.20317031e-05 6.19357373e-05
 5.23380899e-05 7.61444589e-05 4.07736563e-05 3.91677982e-05
 3.77927360e-05 4.40730635e-05 7.03781915e-05 4.22998154e-05
 4.05416271e-05 6.69755347e-05 4.93517456e-05 6.40033264e-05
 5.29906229e-05 5.02126684e-05 6.99799839e-05 3.33972720e-05
 5.47875813e-05 5.74401420e-05 4.58478186e-05 4.88410084e-05
 2.61915729e-05 7.75036085e-05 4.76066427e-05 5.98404313e-05
 5.63050764e-05 3.14645655e-05 6.33038552e-05 4.25937941e-05
 4.58054645e-05 3.72921121e-05 4.79664342e-05 5.76027472e-05
 5.48800594e-05 6.38936114e-05 5.77574654e-05 8.54832138e-05
 5.61990158e-05 7.19126262e-05 4.13281184e-05 5.05720465e-05
 5.11479240e-05 5.83766245e-05 4.10873141e-05 6.46058481e-05
 3.68855290e-05 5.60172306e-05 4.14970240e-05 6.15755336e-05
 4.19232561e-05 4.80196494e-05 5.59280330e-05 3.86681772e-05
 4.34265130e-05 4.33851865e-05 3.08669972e-05 5.09071332e-05
 5.29184012e-05 4.82322640e-05 5.64254441e-05 4.97827030e-05
 5.67949173e-05 5.79835962e-05 4.02693600e-05 3.09041102e-05
 5.90250373e-05 6.64327435e-05 4.51847149e-05 4.82193355e-05
 2.47043906e-05 4.76622158e-05 5.19378735e-05 4.05591413e-05
 5.74231868e-05 4.65462554e-05 4.90145603e-05 2.88000628e-05
 4.17290540e-05 2.85047202e-05 3.51871002e-05 4.81324735e-05
 3.46774822e-05 5.75814527e-05 5.16102507e-05 5.39690508e-05
 2.79225496e-05 8.61805772e-05 3.78569260e-05 2.88388446e-05
 5.98813523e-05 6.65097222e-05 2.42921429e-05 4.95245158e-05
 8.47858625e-05 7.18189658e-05 6.73403951e-05 5.61446598e-05
 5.85131135e-05 6.50143368e-05 7.33827682e-05 5.26533772e-05
 8.97972871e-05 3.82340864e-05 4.57823981e-05 5.88852280e-05
 2.68709305e-05 3.69378388e-05 7.31620247e-05 3.75868497e-05
 4.67305199e-05 8.10913953e-05 7.04338715e-05 5.14885770e-05
 3.80827866e-05 3.91288013e-05 5.24517550e-05 3.32375940e-05
 5.79825756e-05 2.98178271e-05 4.87074820e-05 4.51084111e-05
 4.23870636e-05 6.79965837e-05 7.21909751e-05 5.39589418e-05
 3.31338869e-05 4.09231414e-05 8.15921852e-05 4.50336612e-05
 4.27751084e-05 6.00088170e-05 5.31940314e-05 4.57627053e-05
 3.36081601e-05 3.95153787e-05 7.02742490e-05 4.15121905e-05
 5.56930379e-05 7.76070450e-05 4.57212859e-05 7.38579662e-05
 5.40893904e-05 4.59339567e-05 7.12196666e-05 4.63280692e-05
 4.06863824e-05 8.12148855e-05 6.42674077e-05 3.71213999e-05
 3.67496999e-05 2.95490113e-05 6.83212596e-05 3.99336307e-05
 9.34226327e-05]
rarity_indexes=[200 148 133  83 140  44 174 193 157 185]
rarity values=[200.         217.23943659 226.31218746 164.46160633 229.31133889
 160.39796084 235.63433856 236.34533985 236.57675083 242.89622943]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?