示例#1
0
def test_vertex_histogram():
    """Eigenvalue test for the Vertex Histogram Kernel."""
    vh_kernel = VertexHistogram(verbose=verbose, normalize=normalize)
    if verbose:
        print_kernel("Vertex Histogram", vh_kernel, dataset_tr, dataset_te)
    else:
        positive_eig(vh_kernel, dataset)
示例#2
0
def test_vertex_histogram():
    """Picklability test for the Vertex Histogram Kernel [+ generic-wrapper]."""
    train, _ = generate_dataset(n_graphs=100,
                                r_vertices=(10, 20),
                                r_connectivity=(0.4, 0.8),
                                r_weight_edges=(1, 1),
                                n_graphs_test=40,
                                random_state=rs,
                                features=('nl', 5))

    vh_kernel = VertexHistogram(verbose=verbose, normalize=normalize)
    gk = GraphKernel(kernel={"name": "vertex_histogram"},
                     verbose=verbose,
                     normalize=normalize)

    vh_kernel.fit(train)
    assert is_picklable(vh_kernel)
    gk.fit(train)
    assert is_picklable(gk)
示例#3
0
def test_vertex_histogram():
    """Random input test for the Vertex Histogram Kernel [+ generic-wrapper]."""
    train, test = generate_dataset(n_graphs=100,
                                   r_vertices=(10, 20),
                                   r_connectivity=(0.4, 0.8),
                                   r_weight_edges=(1, 1),
                                   n_graphs_test=40,
                                   random_state=rs,
                                   features=('nl', 5))

    vh_kernel = VertexHistogram(verbose=verbose, normalize=normalize)
    gk = GraphKernel(kernel="VH", verbose=verbose, normalize=normalize)

    try:
        vh_kernel.fit_transform(train)
        vh_kernel.transform(test)
        gk.fit_transform(train)
        gk.transform(test)
        assert True
    except Exception as exception:
        assert False, exception
                for score_type, score_field in zip(scoring, score_fields)
            }
            data["method"] = method_id
            data["time"] = graphs[f"timings_{kernel_set}_{level}"].sum()
            results = results.append(pd.DataFrame(data), ignore_index=True)

    return results


GRAKEL_KERNELS = {
    "GK-SPath":
    lambda: ShortestPath(normalize=NORMALIZING_GRAPH_KERNELS),
    "GK-EHist":
    lambda: EdgeHistogram(normalize=NORMALIZING_GRAPH_KERNELS),
    "GK-VHist":
    lambda: VertexHistogram(normalize=NORMALIZING_GRAPH_KERNELS),
    "GK-GSamp":
    lambda: GraphletSampling(normalize=NORMALIZING_GRAPH_KERNELS),
    "GK-WL-1":
    lambda: WeisfeilerLehman(
        n_iter=1, n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS),
    "GK-WL-2":
    lambda: WeisfeilerLehman(
        n_iter=2, n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS),
    "GK-WL-3":
    lambda: WeisfeilerLehman(
        n_iter=3, n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS),
    "GK-WL-4":
    lambda: WeisfeilerLehman(
        n_iter=4, n_jobs=N_JOBS, normalize=NORMALIZING_GRAPH_KERNELS),
    "GK-WL-5":
示例#5
0
# Classify the graphs of a real-world dataset using graph kernels

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

mutag = fetch_dataset("MUTAG", verbose=False)
G, y = mutag.data, mutag.target

# Split dataset into a training and a test set
# hint: use the train_test_split function of scikit-learn

G_train, G_test, y_train, y_test = train_test_split(G, y, test_size=0.1)

# Perform graph classification using different kernels and evaluate performance

gk = VertexHistogram()

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 VertexHistogram",
      accuracy_score(y_test, y_pred))

gk = ShortestPath(with_labels=True)

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