Changeset 1145 for framspy/evolalg/dissimilarity
- Timestamp:
- 07/07/21 14:38:57 (3 years ago)
- Location:
- framspy/evolalg/dissimilarity
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/dissimilarity/dissimilarity.py
r1113 r1145 7 7 class Dissimilarity(Step, ABC): 8 8 9 def __init__(self, reduction="mean", output_field="dissim", *args, **kwargs):9 def __init__(self, reduction="mean", output_field="dissim", knn=None, *args, **kwargs): 10 10 super(Dissimilarity, self).__init__(*args, **kwargs) 11 11 12 12 self.output_field = output_field 13 13 self.fn_reduce = None 14 if reduction == "mean": 14 self.knn = knn 15 if reduction == "mean": # TODO change this 'elif' sequence to dictionary? 15 16 self.fn_reduce = np.mean 16 17 elif reduction == "max": … … 20 21 elif reduction == "sum": 21 22 self.fn_reduce = np.sum 22 elif reduction == "none" or reduction == None: 23 elif reduction == "knn_mean": 24 self.fn_reduce = self.knn_mean 25 elif reduction == "none" or reduction is None: 23 26 self.fn_reduce = None 24 27 else: 25 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, none")28 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none") 26 29 27 30 def reduce(self, dissim_matrix): … … 29 32 return dissim_matrix 30 33 return self.fn_reduce(dissim_matrix, axis=1) 34 35 def knn_mean(self, dissim_matrix,axis): 36 return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis) -
framspy/evolalg/dissimilarity/frams_dissimilarity.py
r1139 r1145 6 6 from evolalg.dissimilarity.dissimilarity import Dissimilarity 7 7 8 #TODO eliminate overlap with dissimilarity.py 9 8 10 9 11 class FramsDissimilarity(FramsStep): 10 12 11 def __init__(self, frams_lib, reduction="mean", output_field="dissim", *args, **kwargs):13 def __init__(self, frams_lib, reduction="mean", output_field="dissim", knn=None, *args, **kwargs): 12 14 super(FramsDissimilarity, self).__init__(frams_lib, *args, **kwargs) 13 15 14 16 self.output_field = output_field 15 17 self.fn_reduce = None 18 self.knn = knn 16 19 if reduction == "mean": 17 20 self.fn_reduce = np.mean … … 22 25 elif reduction == "sum": 23 26 self.fn_reduce = np.sum 27 elif reduction == "knn_mean": 28 self.fn_reduce = self.knn_mean 24 29 elif reduction == "none" or reduction is None: 25 30 self.fn_reduce = None 26 31 else: 27 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, none")32 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none") 28 33 29 34 def reduce(self, dissim_matrix): … … 41 46 setattr(ind, self.output_field, d) 42 47 return population 48 49 def knn_mean(self, dissim_matrix,axis): 50 return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis)
Note: See TracChangeset
for help on using the changeset viewer.