Commit 8ec5562e authored by Fize Jacques's avatar Fize Jacques
Browse files

Debug Weisfeiler_lehman + ADD BagOfNode approach

parent c874deaa
# coding = utf-8
import networkx as nx
import numpy as np
cimport numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from .base cimport Base
cdef class BOW(Base):
def __init__(self):
Base.__init__(self,0,True)
cpdef np.ndarray compare(self,list graph_list, list selected):
nodes = list()
for g in graph_list:
nodes.extend(list(g.nodes()))
vocabulary = list(set(nodes))
hash_voc = {}
i = 0
for se in vocabulary:
hash_voc[se] = i
i += 1
n, m = len(graph_list), len(hash_voc)
bow_matrix = np.zeros((n, m))
i = 0
for g in range(len(graph_list)):
graph = graph_list[g]
nodes = list(graph.nodes())
for nod in nodes:
j = hash_voc[nod]
bow_matrix[i, j] = 1
i += 1
sim_matrix = cosine_similarity(bow_matrix)
np.fill_diagonal(sim_matrix, 1)
return sim_matrix
...@@ -18,6 +18,8 @@ import networkx as nx ...@@ -18,6 +18,8 @@ import networkx as nx
import numpy as np import numpy as np
cimport numpy as np cimport numpy as np
from ..base cimport Base from ..base cimport Base
from ..base import minmax_scale
from scipy.sparse import csc_matrix,lil_matrix
cdef class WeisfeleirLehmanKernel(Base): cdef class WeisfeleirLehmanKernel(Base):
...@@ -51,7 +53,6 @@ cdef class WeisfeleirLehmanKernel(Base): ...@@ -51,7 +53,6 @@ cdef class WeisfeleirLehmanKernel(Base):
""" """
cdef int n = len(graph_list) cdef int n = len(graph_list)
cdef np.ndarray phi
cdef int n_nodes = 0 cdef int n_nodes = 0
cdef int n_max = 0 cdef int n_max = 0
cdef int i,j cdef int i,j
...@@ -67,6 +68,7 @@ cdef class WeisfeleirLehmanKernel(Base): ...@@ -67,6 +68,7 @@ cdef class WeisfeleirLehmanKernel(Base):
n_max = graph_list[i].number_of_nodes() n_max = graph_list[i].number_of_nodes()
phi = np.zeros((n_nodes, n), dtype=np.uint64) phi = np.zeros((n_nodes, n), dtype=np.uint64)
phi=lil_matrix(phi)
# INITIALIZATION: initialize the nodes labels for each graph # INITIALIZATION: initialize the nodes labels for each graph
# with their labels or with degrees (for unlabeled graphs) # with their labels or with degrees (for unlabeled graphs)
...@@ -101,9 +103,9 @@ cdef class WeisfeleirLehmanKernel(Base): ...@@ -101,9 +103,9 @@ cdef class WeisfeleirLehmanKernel(Base):
graph_list[i]=nx.relabel_nodes(graph_list[i],label_lookup) graph_list[i]=nx.relabel_nodes(graph_list[i],label_lookup)
cdef np.ndarray[np.float64_t] k # cdef np.ndarray[np.float64_t] k
k = np.dot(phi.transpose(), phi) k = np.dot(phi.transpose(), phi)
print(1)
# MAIN LOOP # MAIN LOOP
cdef int it = 0 cdef int it = 0
...@@ -140,10 +142,4 @@ cdef class WeisfeleirLehmanKernel(Base): ...@@ -140,10 +142,4 @@ cdef class WeisfeleirLehmanKernel(Base):
k += np.dot(phi.transpose(), phi) k += np.dot(phi.transpose(), phi)
it = it + 1 it = it + 1
# Compute the normalized version of the kernel return minmax_scale(k).todense()
cdef np.ndarray[np.float64_t] k_norm = np.zeros((k.shape[0],k.shape[1])) \ No newline at end of file
for i in range(k.shape[0]):
for j in range(k.shape[1]):
k_norm[i, j] = k[i, j] / np.sqrt(k[i, i] * k[j, j])
return k_norm
\ No newline at end of file
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