55from sharp .utils import scores_to_ordering
66import pandas as pd
77
8+
89# Not reviewed
910# Returns neighbors that are either close or far ranking wise
1011# AND subselects the top n neighbors in terms of feature similarity
@@ -23,7 +24,7 @@ def _find_neighbors(
2324 & (rankings <= max_ranking )
2425 & (rankings != row_rank )
2526 )
26- else : # Select neighbors that are far ranking wise
27+ else : # Select neighbors that are far ranking wise
2728 mask = (rankings < min_ranking ) | (rankings > max_ranking )
2829 data_neighbors = np .array (original_data )[mask ]
2930 cont_neighbors = np .array (contributions )[mask ]
@@ -41,7 +42,9 @@ def _find_neighbors(
4142# Not reviewed
4243# Returns all neighbors that are similar feature wise
4344# The Euclidean distance between items has to be under the threshold
44- def _find_all_neighbors (original_data , rankings , contributions , row_idx , threshold = None ):
45+ def _find_all_neighbors (
46+ original_data , rankings , contributions , row_idx , threshold = None
47+ ):
4548 row_data = np .array (original_data )[row_idx ]
4649
4750 data_neighbors = np .array (original_data )
@@ -66,11 +69,11 @@ def _find_all_neighbors(original_data, rankings, contributions, row_idx, thresho
6669 )
6770 # Or return distances from all items
6871 return (
69- data_neighbors ,
70- cont_neighbors ,
71- rank_neighbors ,
72- distances ,
73- )
72+ data_neighbors ,
73+ cont_neighbors ,
74+ rank_neighbors ,
75+ distances ,
76+ )
7477
7578
7679# Reviewed
@@ -79,10 +82,12 @@ def _get_importance_mask(row_cont, threshold):
7982 # Calculate order of absolute contributions
8083 row_abs = np .abs (row_cont )
8184 # Find n=threshold largest items
82- res = sorted (row_abs .index .values , key = lambda sub : row_abs [sub ])[- threshold :]
85+ res = sorted (row_abs .index .values , key = lambda sub : row_abs [sub ])[- threshold :]
8386 # Set mask
84- mask = pd .Series (data = [True if i in res else False for i in row_cont .index .values ],
85- index = row_cont .index .values )
87+ mask = pd .Series (
88+ data = [True if i in res else False for i in row_cont .index .values ],
89+ index = row_cont .index .values ,
90+ )
8691 else :
8792 # Calculate cumulative absolute contribution order
8893 total_contribution = np .sum (np .abs (row_cont ))
@@ -128,10 +133,12 @@ def kendall_similarity(a, b):
128133 idx_pair = list (combinations (range (len (a )), 2 ))
129134 val_pair_a = [(a [i ], a [j ]) for i , j in idx_pair if a [i ] != a [j ]]
130135 val_pair_b = [(b [i ], b [j ]) for i , j in idx_pair if b [i ] != b [j ]]
131- inversions = 0
136+ inversions = 0
132137 for (val11 , val12 ), (val21 , val22 ) in zip (val_pair_a , val_pair_b ):
133- if ((val11 > val12 ) and (val21 < val22 )) or ((val11 < val12 ) and (val21 > val22 )):
134- inversions = inversions + 1
138+ if ((val11 > val12 ) and (val21 < val22 )) or (
139+ (val11 < val12 ) and (val21 > val22 )
140+ ):
141+ inversions = inversions + 1
135142 kt = 1 - (2 * inversions ) / normalizer
136143 return (kt + 1 ) / 2
137144
@@ -223,7 +230,7 @@ def row_wise_jaccard(results1, results2, n_features):
223230 >>> n_features = 2
224231 >>> row_wise_jaccard(results1, results2, n_features)
225232 """
226-
233+
227234 if n_features is None :
228235 n_features = results1 .shape [1 ]
229236
@@ -246,9 +253,9 @@ def row_wise_euclidean(results1, results2, normalization=True):
246253 # Make vectors into unit vectors
247254 v1 = normalize ([results1 ])[0 ]
248255 v2 = normalize ([results2 ])[0 ]
249- return euclidean (v1 ,v2 )/ 2
256+ return euclidean (v1 , v2 ) / 2
250257 else :
251- return euclidean (results1 ,results2 )
258+ return euclidean (results1 , results2 )
252259
253260
254261# Reviewed
@@ -279,7 +286,8 @@ def euclidean_agreement(results1, results2, normalization):
279286 vectors in `results1` and `results2` using the Euclidean distance.
280287 """
281288 return results1 .reset_index (drop = True ).apply (
282- lambda row : 1 - row_wise_euclidean (row , results2 .iloc [row .name ], normalization ), axis = 1
289+ lambda row : 1 - row_wise_euclidean (row , results2 .iloc [row .name ], normalization ),
290+ axis = 1 ,
283291 )
284292
285293
@@ -315,6 +323,7 @@ def kendall_agreement(results1, results2):
315323 lambda row : row_wise_kendall (row , results2 .iloc [row .name ]), axis = 1
316324 )
317325
326+
318327# Reviewed
319328def jaccard_agreement (results1 , results2 , n_features = 0.8 ):
320329 """
0 commit comments