def test_medoid_axis_zero(): m = hd.medoid(DATA1, axis=0) r = np.array([1281, 1128, 932, 870, 888, 1227, 1213, 1375, 1454, 1353, 1592, 1606, 1609, 1640, 1556, 1296, 548, 1527, 938, 1082, 1456, 798, 972, 378, 522]) assert_equal(m, r)
def label_clusters_medoid(socd, clusters, vecs): flattened = np.array([np.hstack(i) for i in vecs]) lookup = dict(zip([hash(str(i)) for i in flattened], socd.task)) da = (pd.DataFrame(np.hstack([clusters.reshape(-1, 1), flattened])) .rename(columns = {0: 'cluster'}) .sort_values('cluster') .groupby('cluster') .apply(lambda df: medoid(df.iloc[:, 1:].values.T)) .map(lambda a: lookup[hash(str(a))])) d = sorted(list(da.to_dict().items()), key=lambda t: t[0]) return np.array([b for a,b in d])
def find_interface_centers(self): self.center_face = [] coords = self.M.faces.center[:] for cface in self.M.coarse.interfaces_faces: tcoords = coords[cface].reshape(len(cface),3) u = int(cface[hd.medoid(tcoords,axis=0,indexonly=True)].ravel()) #import pdb; pdb.set_trace() self.center_face.append(u) #self.center_face.append(cface[hd.medoid(tcoords,axis=0,indexonly=True) ] ) #np.array(hd.geomedian(coarse_volume.faces.center[coarse_volume.faces.boundary].T)) pass
def cal_Medoid(self, input_weights): # input_weights: list of client weights # input_weights[0]: list of a certain client weights # input_weights[i][j]: ndarray of a certain layer of a certain client weights res = [] for layer_idx in range( len(input_weights[0]) ): # record the shape of the current layer shape_cur_layer = input_weights[0][layer_idx].shape one_layer_set = [ item[layer_idx].flatten() for item in input_weights ] one_layer_set = np.array( one_layer_set ).astype(float) one_layer_set = hdm.medoid(one_layer_set, axis = 0) one_layer_set = np.reshape( np.array(one_layer_set), shape_cur_layer ) res.append( one_layer_set ) return res
def generate_center_image(instance, center, ids, one_hot): """ Generates a `center_image` which is one (True) for all center locations and zero (False) otherwise. Parameters ---------- instance: numpy array `instance` image containing unique `ids` for each object (YX) or present in a one-hot encoded style where each object is one in it own slice and zero elsewhere. center: string One of 'centroid', 'approximate-medoid' or 'medoid'. ids: list Unique ids corresponding to the objects present in the instance image. one_hot: boolean True (in this case, `instance` has shape DYX) or False (in this case, `instance` has shape YX). """ if (not one_hot): center_image = np.zeros(instance.shape, dtype=bool) else: center_image = np.zeros((instance.shape[-2], instance.shape[-1]), dtype=bool) for j, id in enumerate(ids): if (not one_hot): y, x = np.where(instance == id) else: y, x = np.where(instance[id] == 1) if len(y) != 0 and len(x) != 0: if (center == 'centroid'): ym, xm = np.mean(y), np.mean(x) elif (center == 'approximate-medoid'): ym_temp, xm_temp = np.median(y), np.median(x) imin = np.argmin((x - xm_temp)**2 + (y - ym_temp)**2) ym, xm = y[imin], x[imin] elif (center == 'medoid'): #dist_matrix = distance_matrix(np.vstack((x, y)).transpose(), np.vstack((x, y)).transpose()) #imin = np.argmin(np.sum(dist_matrix, axis=0)) #ym, xm = y[imin], x[imin] ym, xm = hd.medoid(np.vstack((y, x))) center_image[int(np.round(ym)), int(np.round(xm))] = True return center_image
def test_medoid_axis_zero_indexonly(): m = hd.medoid(DATA1, axis=0, indexonly=True) assert_equal(m, 2)
def test_medoid_noaxis_indexonly(): m = hd.medoid(DATA1, indexonly=True) assert_equal(m, 5)
def test_medoid_axis_one(): m = hd.medoid(DATA1, axis=1) r = np.array([721, 966, 1227, 2249, 3577, 2693]) assert_equal(m, r)
def test_medoid_in_set_random(): a = np.random.normal(1, size=(6, 10)) s = [list(x) for x in a.T] m = hd.medoid(a) idx = s.index(list(m)) assert_true(idx > -1)
def test_medoid_shape_axis_one(): a = np.random.normal(1, size=(6, 10)) m = hd.medoid(a, axis=1) assert_equal(m.shape, (6, ))
def test_medoid_shape_axis_zero(): a = np.random.normal(1, size=(6, 10)) m = hd.medoid(a, axis=0) assert_equal(m.shape, (10, ))
def test_medoid_shape_noaxis(): a = np.random.normal(1, size=(6, 10)) m = hd.medoid(a) assert_equal(m.shape, (6, ))
def test_medoid_two_obs(): data = np.array([[1.0, 2.0, 1.0], [2.0, 1.0, 1.0]]) m = hd.medoid(data, axis=0) r = np.array([1.0, 2.0, 1.0]) assert_array_almost_equal(m, r, decimal=3)
def test_medoid_1d(): data = np.ones((1, 3)) m = hd.medoid(data, axis=1) r = np.median(data, axis=1) assert_array_almost_equal(m, r, decimal=3)
def test_medoid_axis_one_indexonly(): m = hd.medoid(DATA1, axis=1, indexonly=True) assert_equal(m, 5)
def pcoord(X, c=None): n, p = X.shape dims = range(1, p + 1) for obs in X: plt.plot(dims, obs, c=c) from sklearn import datasets iris = datasets.load_iris() X = iris.data y = iris.target X1 = X[y == 1] plt.figure(figsize=(10, 3), dpi=200) pcoord(X1, c='#aaaaaa') md = hd.medoid(X1, axis=0) gm = hd.geomedian(X1, axis=0) xx = np.arange(X.shape[1]) + 1 plt.plot(xx, md, c='m', ls='--', lw=2, label='Medoid') plt.plot(xx, gm, c='r', ls='-', lw=2, label='Geometric Median') plt.xticks(xx, iris.feature_names) plt.title('Iris data set (' + iris.target_names[1].title() + ' class)') plt.grid(color='k', ls=':', axis='x') plt.legend(framealpha=1.0) plt.savefig('docs/fig1.svg') # n, p = (40, 20) # loc1 = np.random.normal(1, 2.0, size=(p,)) # loc2 = loc1 + np.random.normal(1.0, 1.0, size=(p,)) # sd = np.random.uniform(0.1, 0.2, size=(p,)) # X1 = np.random.normal(loc=loc1, scale=sd, size=(n, p))