def cpca_cluster(request): if request.method == 'POST': request_data = json.loads(request.body) alpha = None if 'alpha' in request_data.keys(): alpha = request_data['alpha'] bg_data_idx = request_data['dataIndex'] data = rs.state['data'] homo_idx = rs.state['outputs_server'] == rs.state['outputs_client'] bg = data[bg_data_idx] if bg.shape[0] == 1: return JsonResponse(rs.state['cpca_all_result']) fg = np.concatenate((data[homo_idx], bg)) local_data, _ = load_samples(dataset=rs['dataset'], client_name=rs['client'], sampling_type='local') if USE_GPU: with torch.no_grad(): cPCA = CPCA_GPU(n_components=2) cPCA.fit(target=fg, background=bg, alpha=alpha) projected_data = cPCA.transform(local_data) components = cPCA.get_components() alpha = cPCA.get_alpha() else: cPCA = CPCA(n_components=2) cPCA.fit(target=fg, background=bg, alpha=alpha) projected_data = cPCA.transform(local_data) components = cPCA.get_components() alpha = cPCA.get_alpha() projected_data = np.round(projected_data, 6).astype(float) components = np.round(components, 6).astype(float) alpha = np.round(alpha, 6).astype(float) data = { 'alpha': alpha, 'cPC1': components[0].tolist(), 'cPC2': components[1].tolist(), 'projectedData': projected_data.tolist(), } return JsonResponse(data)
def cpca_all(request): if request.method == 'POST': request_data = json.loads(request.body) alpha = None if 'alpha' in request_data.keys(): alpha = request_data['alpha'] data = rs.state['data'] hetero_labels = rs.state['outputs_server'] != rs.state['outputs_client'] if USE_GPU: with torch.no_grad(): cPCA = CPCA_GPU(n_components=2) projected_data = cPCA.fit_transform( target=data, background=data[hetero_labels], alpha=alpha) components = cPCA.get_components() alpha = cPCA.get_alpha() else: cPCA = CPCA(n_components=2) projected_data = cPCA.fit_transform(target=data, background=data[hetero_labels], alpha=alpha) components = cPCA.get_components() alpha = cPCA.get_alpha() projected_data = np.round(projected_data, 6).astype(float) components = np.round(components, 6).astype(float) alpha = np.round(alpha, 6).astype(float) data = { 'alpha': alpha, 'cPC1': components[0].tolist(), 'cPC2': components[1].tolist(), 'projectedData': projected_data.tolist(), } rs.set('cpca_all_result', data) return JsonResponse(data)
import time import matplotlib.pyplot as plt import numpy as np from sklearn import datasets, preprocessing from cpca import CPCA from ccpca import CCPCA dataset = datasets.load_wine() X = dataset.data y = dataset.target X = preprocessing.scale(X) cpca = CPCA() cpca.fit(fg=X[y == 0], bg=X[y != 0], alpha=2.15) X_r = cpca.transform(X) plt.figure() colors = ['navy', 'turquoise', 'darkorange'] lw = 2 for color, i, target_name in zip(colors, [0, 1, 2], [0, 1, 2]): plt.scatter(X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=.8, lw=lw, label=target_name)
import time import matplotlib.pyplot as plt import numpy as np from sklearn import datasets, preprocessing from cpca import CPCA from ccpca import CCPCA dataset = datasets.load_wine() X = dataset.data y = dataset.target X = preprocessing.scale(X) cpca = CPCA() # manual alpha selection # cpca.fit(fg=X[y == 0], bg=X[y != 0], alpha=2.15) # auto alpha selection cpca.fit(fg=X[y == 0], bg=X[y != 0]) X_r = cpca.transform(X) plt.figure() colors = ['navy', 'turquoise', 'darkorange'] lw = 2 for color, i, target_name in zip(colors, [0, 1, 2], [0, 1, 2]): plt.scatter(X_r[y == i, 0], X_r[y == i, 1],
# Load data. Refer to http://www-personal.umich.edu/~mejn/netdata/ for the details of datasets tg = gt.load_graph('./data/dolphin.xml.gz') bg = gt.load_graph('./data/karate.xml.gz') # Prepare network representation learning method nrl = DeepGL(base_feat_defs=[ 'total_degree', 'betweenness', 'closeness', 'eigenvector', 'pagerank', 'katz' ], rel_feat_ops=['mean', 'sum', 'maximum', 'lp_norm'], nbr_types=['all'], ego_dist=3, lambda_value=0.7) # Prepare contrastive learning method cl = CPCA() # Set network representation and contrastive learning methods # using DeepGL and cPCA is i-cNRL (interpretable cNRL) cnrl = CNRL(nrl, cl) # Learning cnrl.fit(tg, bg) # Obtain results for plotting tg_feat_mat = preprocessing.scale(cnrl.tg_feat_mat) bg_feat_mat = preprocessing.scale(cnrl.bg_feat_mat) tg_emb = cnrl.transform(feat_mat=tg_feat_mat) bg_emb = cnrl.transform(feat_mat=bg_feat_mat) feat_defs = cnrl.feat_defs