Commit 02786270 authored by markus's avatar markus
Browse files

Added KNN, not working tho

parent 8cab8361
......@@ -22,7 +22,7 @@ def knn(p : List, n : List, instance : List, d = eucl_dist) -> bool:
:param d: Distance function. Default is euclidian distance
:return: Classification result of instance
"""
k = int((len(p) + len(n))**0.5)
k = int(min(len(p), len(n))**0.5)
if k % 2 == 0:
k += 1 #make k uneven for majority vote
distances = [(True, d(instance, i)) for i in p] + [(False,d(instance, i)) for i in n]
......
from rdflib import Graph, URIRef
from pykeen_embeddings import generate_and_save_embeddings, load_embeddings_from_file
from lib import knn
from lib import ml_metrics
def read(path, format):
"""
......@@ -12,6 +14,40 @@ def read(path, format):
return g
def run_knn(g_ttl: Graph, path_to_embedding : str, lp : int):
"""Run knn on learning problem lp.
:param g_ttl: Graph containing LP definitions
:param path_to_embedding: path to tsv file containing embeddings
:param lp: Number of lp to try
"""
embeddings = load_embeddings_from_file(path_to_embedding)
p, n = extract_resources(g_ttl, lp)
p = [embeddings[str(x)] for x in p]
n = [embeddings[str(x)] for x in n]
TP = 0
FP = 0
TN = 0
FN = 0
for i in range(len(p) - 1):
if knn.knn(p[:i] + p[i+1:], n, p[i]):
TP += 1
else:
FN += 1
for i in range(len(n) - 1):
if not knn.knn(p, n[:i] + n[i+1:], n[i]):
TN += 1
else:
FP += 1
ml_metrics.print_metrics(TP, TN, FP, FN)
def extract_resources(g: Graph, lp: int):
"""
......@@ -36,8 +72,10 @@ def extract_resources(g: Graph, lp: int):
g1 = read(path='data/kg-mini-project-train.ttl', format='turtle')
g2 = read(path='data/carcinogenesis.owl', format='application/rdf+xml')
#g2 = read(path='data/carcinogenesis.owl', format='application/rdf+xml')
run_knn(g1, "data/embeddings_carcinogenesis.tsv", 4)
generate_and_save_embeddings(g2, "data/carcinogenesis.tsv", "data/embeddings_carcinogenesis.tsv")
#generate_and_save_embeddings(g2, "data/carcinogenesis.tsv", "data/embeddings_carcinogenesis.tsv")
#for lp in range(1, 26):
# print(extract_resources(g1, lp))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment