def test_lof_precomputed(random_state=42):
 rng = np.random.RandomState(random_state)
 X = rng.random_sample((10, 4))
 Y = rng.random_sample((3, 4))
 DXX = metrics.pairwise_distances(X, metric='euclidean')
 DYX = metrics.pairwise_distances(Y, X, metric='euclidean')
 lof_X = neighbors.LocalOutlierFactor(n_neighbors=3)
 lof_X.fit(X)
 pred_X_X = lof_X._predict()
 pred_X_Y = lof_X._predict(Y)
 lof_D = neighbors.LocalOutlierFactor(n_neighbors=3, algorithm='brute', metric='precomputed')
 lof_D.fit(DXX)
 pred_D_X = lof_D._predict()
 pred_D_Y = lof_D._predict(DYX)
 assert_array_almost_equal(pred_X_X, pred_D_X)
 assert_array_almost_equal(pred_X_Y, pred_D_Y)

def test_lof_precomputed ( random_state = 42 ) : rng = np . random . RandomState ( random_state ) X = rng . random_sample ( ( 10 , 4 ) ) Y = rng . random_sample ( ( 3 , 4 ) ) DXX = metrics . pairwise_distances ( X , metric = 'euclidean' ) DYX = metrics . pairwise_distances ( Y , X , metric = 'euclidean' ) lof_X = neighbors . LocalOutlierFactor ( n_neighbors = 3 ) lof_X . fit ( X ) pred_X_X = lof_X . _predict ( ) pred_X_Y = lof_X . _predict ( Y ) lof_D = neighbors . LocalOutlierFactor ( n_neighbors = 3 , algorithm = 'brute' , metric = 'precomputed' ) lof_D . fit ( DXX ) pred_D_X = lof_D . _predict ( ) pred_D_Y = lof_D . _predict ( DYX ) assert_array_almost_equal ( pred_X_X , pred_D_X ) assert_array_almost_equal ( pred_X_Y , pred_D_Y )
tests lof with a distance matrix .