def test_k_distance(): instances = ((1,1),(2,2),(3,3)) d = lof.k_distance(1,(2,2),instances) assert d == (0.0, [(2,2)]) d = lof.k_distance(1,(2.2,2.2),instances) assert d == (0.20000000000000018, [(2,2)]) d = lof.k_distance(1,(2.5,2.5),instances) assert d == (0.5, [(2,2),(3,3)])
def cal_C3(instance): k_inf = kinf[instances.index(instance)] k_avg = (2 + k_inf) / 2 (k1, neighbours) = k_distance(k_inf + 1, instance, instances) (k2, neighbours) = k_distance(k_avg, instance, instances) c3 = k1 / k2 return c3
def test_k_distance(): instances = np.array(((1, 1), (2, 2), (3, 3))) d = lof.k_distance(1, (2, 2), instances) assert np.allclose(d[0], 0.0) assert np.allclose(d[1], np.array([(2, 2)])) d = lof.k_distance(1, (2.2, 2.2), instances) assert np.allclose(d[0], 0.28284271247461928) assert np.allclose(d[1], np.array([(2, 2)])) d = lof.k_distance(1, (2.5, 2.5), instances) assert np.allclose(d[0], 0.70710678118654757) assert np.allclose(d[1], np.array([(2, 2), (3, 3)])) d = lof.k_distance(5, (2.2, 2.2), instances) assert np.allclose(d[0], 1.6970562748477143) assert np.allclose(d[1], np.array([(2, 2), (3, 3), (1, 1)]))
def update(): # 计算邻接矩阵M for instance in instances: instances_value_backup = list(instances) instances_value_backup.remove(instance) (k_distance_value, neighbours) = k_distance(len(instances_value_backup), instance, instances_value_backup) M[instances.index(instance)] = neighbours