diff --git a/mil/bag_representation/mapping.py b/mil/bag_representation/mapping.py index d6a87b9..e5927e4 100644 --- a/mil/bag_representation/mapping.py +++ b/mil/bag_representation/mapping.py @@ -149,7 +149,7 @@ class DiscriminativeMapping(MILESBase): Multi-instance Learning with Discriminative Bag Mapping (Wu et al.) http://www.cse.fau.edu/~xqzhu/papers/TKDE.Wu.2017.Multiinstance.pdf """ - def __init__(self, m=2, sigma2=8e5): + def __init__(self, m=2, sigma2=8e5, distance_metric="MILES"): """ Parameters ---------- @@ -158,6 +158,7 @@ def __init__(self, m=2, sigma2=8e5): """ super(DiscriminativeMapping, self).__init__(sigma2) self.m = m + self.distance_metric = distance_metric def fit(self, X, y): """ @@ -186,8 +187,9 @@ def calculate_iip(self, X, y): # calculate Q Y = np.array([i * j for i in label for j in label]).reshape(len(label), len(label)) B, A = np.unique(Y, return_counts=True)[1] - Q = np.where(Y==-1, -1/A, 1/B) - + + Q = np.where(Y == 1, -1 / A, 1 / B) + # calculate J J = np.sum(ins_bag @ Q, axis=1) @@ -197,4 +199,31 @@ def calculate_iip(self, X, y): # select dip self.iip_ = np.array(bags2instances(X))[self.items_] - \ No newline at end of file + def transform(self, X): + """ + Gets the minimum distance between each bag-point pair. + + """ + if self.distance_metric == "MILES": + return super(DiscriminativeMapping, self).transform(X) + elif self.distance_metric == "HAUSDORF": + dists = np.zeros((len(X), len(self.iip_))) + for i, bag in enumerate(X): + for j, p in enumerate(self.iip_): + # calculate minimum distance between bag and point + dists[i][j] = min([calc_distance(bag_pnt, p) for bag_pnt in bag]) + return dists + else: + return None + + +def calc_distance(A, B): + """ + Calculates euclidean distance between 2 n-dimensional points + """ + dist = 0 + for i in range(len(A)): + dist += (A[i] - B[i])**2 + dist = dist**0.5 + return dist +