@@ -454,27 +454,18 @@ def trustworthiness(X, X_embedded, n_neighbors=5,
454
454
"parameter instead." , DeprecationWarning )
455
455
metric = 'precomputed'
456
456
dist_X = pairwise_distances (X , metric = metric )
457
- if metric == 'precomputed' :
458
- dist_X = dist_X .copy ()
459
- # we set the diagonal to np.inf to exclude the points themselves from
460
- # their own neighborhood
461
- np .fill_diagonal (dist_X , np .inf )
462
457
ind_X = np .argsort (dist_X , axis = 1 )
463
- # `ind_X[i]` is the index of sorted distances between i and other samples
464
458
ind_X_embedded = NearestNeighbors (n_neighbors ).fit (X_embedded ).kneighbors (
465
459
return_distance = False )
466
460
467
- # We build an inverted index of neighbors in the input space: For sample i,
468
- # we define `inverted_index[i]` as the inverted index of sorted distances:
469
- # inverted_index[i][ind_X[i]] = np.arange(1, n_sample + 1)
470
461
n_samples = X .shape [0 ]
471
- inverted_index = np . zeros (( n_samples , n_samples ), dtype = int )
472
- ordered_indices = np .arange ( n_samples + 1 )
473
- inverted_index [ ordered_indices [: - 1 , np . newaxis ],
474
- ind_X ] = ordered_indices [ 1 :]
475
- ranks = inverted_index [ ordered_indices [: - 1 , np . newaxis ],
476
- ind_X_embedded ] - n_neighbors
477
- t = np .sum (ranks [ranks > 0 ])
462
+ t = 0.0
463
+ ranks = np .zeros ( n_neighbors )
464
+ for i in range ( n_samples ):
465
+ for j in range ( n_neighbors ):
466
+ ranks [ j ] = np . where ( ind_X [ i ] == ind_X_embedded [ i , j ])[ 0 ][ 0 ]
467
+ ranks -= n_neighbors
468
+ t + = np .sum (ranks [ranks > 0 ])
478
469
t = 1.0 - t * (2.0 / (n_samples * n_neighbors *
479
470
(2.0 * n_samples - 3.0 * n_neighbors - 1.0 )))
480
471
return t
0 commit comments