Пример #1
0
def test_random_walk():
    """Picklability test for the Simple 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=None)

    rw_kernel = RandomWalk(verbose=verbose, normalize=normalize)
    rw_kernel.fit(train)
    assert is_picklable(rw_kernel)
Пример #2
0
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)
Пример #3
0
def test_random_walk():
    """Random input test for the Simple 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=None)

    rw_kernel = RandomWalk(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-3":
    lambda: WeisfeilerLehmanOptimalAssignment(
        n_iter=3, n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS),
    "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),
Пример #5
0
K_train = gk.fit_transform(G_train)
K_test = gk.transform(G_test)

clf = SVC(kernel='precomputed', C=1)  # Initialize SVM
clf.fit(K_train, y_train)  # Train SVM
y_pred = clf.predict(K_test)  # Predict

# Compute the classification accuracy
# hint: use the accuracy_score function of scikit-learn

print("Classification accuracy using ShortestPath",
      accuracy_score(y_test, y_pred))

# Use the random walk kernel and the pyramid match graph kernel to perform classification

gk = RandomWalk()

K_train = gk.fit_transform(G_train)
K_test = gk.transform(G_test)

clf = SVC(kernel='precomputed', C=1)  # Initialize SVM
clf.fit(K_train, y_train)  # Train SVM
y_pred = clf.predict(K_test)  # Predict

print("Classification accuracy using RandomWalk",
      accuracy_score(y_test, y_pred))

gk = PyramidMatch(with_labels=False)

K_train = gk.fit_transform(G_train)
K_test = gk.transform(G_test)
Пример #6
0
clf.fit(K_train, y_train)  # Train SVM
y_pred = clf.predict(K_test)  # Predict

# Compute the classification accuracy
# hint: use the accuracy_score function of scikit-learn

##################
# your code here #
print(accuracy_score(y_test, y_pred))
##################

# Use the random walk kernel and the pyramid match graph kernel to perform classification

##################
# your code here #
gk1 = RandomWalk()
K_train1 = gk1.fit_transform(G_train)
K_test1 = gk1.transform(G_test)

clf1 = SVC(kernel='precomputed', C=1)  # Initialize SVM
clf1.fit(K_train, y_train)  # Train SVM
y_pred1 = clf1.predict(K_test)  # Predict

print(accuracy_score(y_test, y_pred1))
##################

############## Question 3
# Classify the graphs of a real-world dataset using graph kernels

# Load the MUTAG dataset
# hint: use the fetch_dataset function of GraKeL