ysig / GraKeL

A scikit-learn compatible library for graph kernels
https://ysig.github.io/GraKeL/
Other
588 stars 96 forks source link

Division by zero error when using Propagation kernel #57

Closed amirabbasasadi closed 3 years ago

amirabbasasadi commented 3 years ago

Hi, sometimes a division by zero error happens when using the Propagation kernel. It seems that's because of normalizing the rows of the transition matrix in the following line:

transition_matrix[i] = (T.T / np.sum(T, axis=1)).T

when I replaced the above line by sklearn normalization function, the problem was solved and div by zero didn't happen:

from sklearn.preprocessing import normalize
transition_matrix[i] = normalize(T, axis=1, norm='l1')
ysig commented 3 years ago

This is a great idea!

Can you make a pull request so your contribution is registered?

Before doing so can you validate that the two operations are equivalent in the non-zero cases?

Thank you,

amirabbasasadi commented 3 years ago

Sure, I'll create a pull request. If all of the elements of matrix T are positive, then the two operations are equivalent. In the case of a matrix containing some negative elements, the result is different. However, since the T is a transition matrix, It wouldn't be a problem to assume the matrix elements are positive.