def test_random_walk_labels(): """Picklability test for the Labelled Random Walk kernel.""" train, _ = generate_dataset(n_graphs=100, r_vertices=(10, 20), r_connectivity=(0.4, 0.8), r_weight_edges=(0.01, 12.0), n_graphs_test=40, random_state=rs, features=('nl', 3)) rw_kernel = RandomWalkLabeled(verbose=verbose, normalize=normalize) rw_kernel.fit(train) assert is_picklable(rw_kernel)
def main(): graphlet = Graphlet('data/annotated-trace.csv') graplet_test = Graphlet('data/not-annotated-trace.csv', test=True) X = graphlet.profile_graphlets y = graphlet.get_graphlets_label() y =[0 if el=='normal' else 1 for el in y] X_test = graplet_test.profile_graphlets train = list(graph_from_networkx(X, node_labels_tag='type')) G_test = list(graph_from_networkx(X_test, node_labels_tag='type')) # Splits the dataset into a training and a validation set G_train, G_val, y_train, y_val = train_test_split(train, y, test_size=0.1, random_state=42) gk = RandomWalkLabeled(n_jobs= 4) K_train = gk.fit_transform(train) K_val = gk.transform(G_val) pickle.dump(K_train, open( "k_train.p", "wb" ) ) pickle.dump(K_val, open( "k_val.p", "wb" ) ) pickle.dump(gk, open("gk.p", "wb")) # Uses the SVM classifier to perform classification clf = SVC(kernel="precomputed") clf.fit(K_train, y) y_pred = clf.predict(K_val) # Computes and prints the classification accuracy print(acc) dump(clf, 'svm_rand_walk.joblib') K_test = gk.transform(G_test) y_test_pred = clf.predict(K_test) print(y_test_pred) pickle.dump(K_test, open( "k_test.p", "wb" ) )
def test_random_walk(): """Eigenvalue test for the Simple, Labelled Random Walk kernel.""" rw_kernel = RandomWalk(verbose=verbose, normalize=normalize, lamda=0.01) if verbose: print_kernel("Random Walk", rw_kernel, dataset_tr, dataset_te) else: positive_eig(rw_kernel, dataset) rw_kernel_lab = RandomWalkLabeled(verbose=verbose, normalize=normalize, lamda=0.0001) if verbose: print_kernel("Random Walk Labelled", rw_kernel_lab, dataset_tr, dataset_te) else: positive_eig(rw_kernel_lab, dataset)
def gk_function(algorithm, graphs, par): """ Function to run the kernel on the param grid. Since different kernels have different numbers of parameters, this is necessary. """ print("parameters", par) if algorithm == "SP_gkl": gk = ShortestPath(with_labels=True).fit_transform(graphs) elif algorithm == "EH_gkl": gk = EdgeHistogram().fit_transform(graphs) elif algorithm == "WL_gkl": gk = WeisfeilerLehman(n_iter=par).fit_transform(graphs) elif algorithm == "RW_gkl": lam, p = par gk = RandomWalkLabeled(lamda=lam, p=p).fit_transform(graphs) elif algorithm == "CSM_gkl": c, k = par # testing lambda function. c should reset for each iteration gk = SubgraphMatching( k=k, ke=lambda p1, p2: ke_kernel(p1, p2, c), # inline lambda kv=kv_kernel ).fit_transform(graphs) return(gk)
def test_random_walk_labels(): """Random input test for the Labelled Random Walk kernel.""" train, test = generate_dataset(n_graphs=100, r_vertices=(10, 20), r_connectivity=(0.4, 0.8), r_weight_edges=(0.01, 12.0), n_graphs_test=40, random_state=rs, features=('nl', 3)) rw_kernel = RandomWalkLabeled(verbose=verbose, normalize=normalize) try: rw_kernel.fit_transform(train) rw_kernel.transform(test) assert True except Exception as exception: assert False, exception
"GK-WL-OA-4": lambda: WeisfeilerLehmanOptimalAssignment( n_iter=4, n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS), "GK-WL-OA-5": lambda: WeisfeilerLehmanOptimalAssignment( n_iter=5, n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS), } NOT_TESTED = { "GK-ShortestPathA": lambda: ShortestPathAttr(normalize=NORMALIZING_GRAPH_KERNELS), "GK-RandomWalk": lambda: RandomWalk(n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS ), # taking too long "GK-RandomWalkLabeled": lambda: RandomWalkLabeled( n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS), # taking too long "GK-GraphHopper": lambda: GraphHopper(normalize=NORMALIZING_GRAPH_KERNELS), "GK-PyramidMatch": lambda: PyramidMatch(normalize=NORMALIZING_GRAPH_KERNELS), # Error with PG "GK-LovaszTheta": lambda: LovaszTheta(normalize=NORMALIZING_GRAPH_KERNELS), "GK-SvmTheta": lambda: SvmTheta(normalize=NORMALIZING_GRAPH_KERNELS), "GK-Propagation": lambda: Propagation(normalize=NORMALIZING_GRAPH_KERNELS), "GK-PropagationA": lambda: PropagationAttr(normalize=NORMALIZING_GRAPH_KERNELS), "GK-MScLaplacian": lambda: MultiscaleLaplacian(normalize=NORMALIZING_GRAPH_KERNELS), "GK-OddSth":