""" data = pd.read_csv(file_name, sep=sep, header=0) return [rdflib.URIRef(x) for x in data[col_entity]], list(data[col_label]) test_entities, test_labels = load_data(DATASET["test"][0], DATASET["test"][1], DATASET["test"][2]) train_entities, train_labels = load_data(DATASET["train"][0], DATASET["train"][1], DATASET["train"][2]) entities = train_entities + test_entities labels = train_labels + test_labels kg = rdflib_to_kg(OUTPUT, label_predicates=LABEL_PREDICATES) train_embeddings, test_embeddings = create_embeddings(kg, entities, len(train_entities), WALKERS) # Fit a support vector machine on train embeddings and evaluate on test clf = SVC(random_state=42) clf.fit(train_embeddings, train_labels) print("Support Vector Machine:") print( f"Accuracy = {accuracy_score(test_labels, clf.predict(test_embeddings))}") print(confusion_matrix(test_labels, clf.predict(test_embeddings))) # Create t-SNE embeddings from RDF2Vec embeddings (dimensionality reduction) walk_tsne = TSNE(random_state=42) X_walk_tsne = walk_tsne.fit_transform(train_embeddings + test_embeddings)
def test_rdflib_to_kg(): kg = rdflib_to_kg("samples/mutag.owl", label_predicates=label_predicates) assert type(kg) == KnowledgeGraph
import random import rdflib from pyrdf2vec.converters import rdflib_to_kg from pyrdf2vec.graph import Vertex from pyrdf2vec.walkers import RandomWalker LABEL_PREDICATE = "http://dl-learner.org/carcinogenesis#isMutagenic" KG = rdflib_to_kg("samples/mutag.owl", label_predicates=[LABEL_PREDICATE]) def generate_entities(): return [ rdflib.URIRef( f"{LABEL_PREDICATE.split('#')[0] + '#'}{random.randint(0, 335)}") for _ in range(random.randint(0, 200)) ] class TestRandomWalker: def test_extract_random_walks(self): walks = RandomWalker(4, float("inf")).extract_random_walks( KG, Vertex(str(generate_entities()))) assert type(walks) == list def test_extract(self): canonical_walks = RandomWalker(4, float("inf")).extract( KG, generate_entities()) assert type(canonical_walks) == set