diff --git a/README.md b/README.md index c125c65..b6f126e 100644 --- a/README.md +++ b/README.md @@ -13,24 +13,27 @@ for binary sequences and even kernels for labeled graphs. ## Sample usage - from sklearn.svm import SVC - from sklearn.metrics import accuracy_score - import numpy as np +```python +from __future__ import print_function +from sklearn.svm import SVC +from sklearn.metrics import accuracy_score +import numpy as np - from pykernels.basic import RBF +from pykernels.basic import RBF - X = np.array([[1,1], [0,0], [1,0], [0,1]]) - y = np.array([1, 1, 0, 0]) +X = np.array([[1,1], [0,0], [1,0], [0,1]]) +y = np.array([1, 1, 0, 0]) - print 'Testing XOR' +print('Testing XOR') - for clf, name in [(SVC(kernel=RBF(), C=1000), 'pykernel'), (SVC(kernel='rbf', C=1000), 'sklearn')]: - clf.fit(X, y) - print name - print clf - print 'Predictions:', clf.predict(X) - print 'Accuracy:', accuracy_score(clf.predict(X), y) - print +for clf, name in [(SVC(kernel=RBF(), C=1000), 'pykernel'), (SVC(kernel='rbf', C=1000), 'sklearn')]: + clf.fit(X, y) + print(name) + print(clf) + print('Predictions:', clf.predict(X)) + print('Accuracy:', accuracy_score(clf.predict(X), y)) + print() +``` ## implemented Kernels diff --git a/example.py b/example.py index 6e97350..a6603df 100644 --- a/example.py +++ b/example.py @@ -1,3 +1,4 @@ +from __future__ import print_function from sklearn.svm import SVC from sklearn.metrics import accuracy_score import numpy as np @@ -7,12 +8,11 @@ X = np.array([[1,1], [0,0], [1,0], [0,1]]) y = np.array([1, 1, 0, 0]) -print 'Testing XOR' +print('Testing XOR') for clf, name in [(SVC(kernel=RBF(), C=1000), 'pykernel'), (SVC(kernel='rbf', C=1000), 'sklearn')]: clf.fit(X, y) - print name - print clf - print 'Predictions:', clf.predict(X) - print 'Accuracy:', accuracy_score(clf.predict(X), y) - print + print(name) + print(clf) + print('Predictions:', clf.predict(X)) + print('Accuracy:', accuracy_score(clf.predict(X), y)) diff --git a/pykernels/__init__.py b/pykernels/__init__.py index 53f0736..de26884 100644 --- a/pykernels/__init__.py +++ b/pykernels/__init__.py @@ -1,6 +1,6 @@ -from basic import Linear, Polynomial, RBF -from regular import Exponential, Laplacian, RationalQuadratic, InverseMultiquadratic, Cauchy, TStudent,\ +from .basic import Linear, Polynomial, RBF +from .regular import Exponential, Laplacian, RationalQuadratic, InverseMultiquadratic, Cauchy, TStudent,\ ANOVA, Spline, Min, Log, Power, Chi2, AdditiveChi2, GeneralizedHistogramIntersection,\ Tanimoto, Sorensen, MinMax, Wavelet, Fourier -from graph.randomwalk import RandomWalk -from graph.allgraphlets import All34Graphlets +from .graph.randomwalk import RandomWalk +from .graph.allgraphlets import All34Graphlets diff --git a/pykernels/basic.py b/pykernels/basic.py index 0c72a67..b5a86f4 100644 --- a/pykernels/basic.py +++ b/pykernels/basic.py @@ -5,9 +5,9 @@ __author__ = 'lejlot' -from pykernels.base import Kernel import numpy as np -from utils import euclidean_dist_matrix +from .base import Kernel +from .utils import euclidean_dist_matrix class Linear(Kernel): """ diff --git a/pykernels/graph/__init__.py b/pykernels/graph/__init__.py index 0d777da..44ee814 100644 --- a/pykernels/graph/__init__.py +++ b/pykernels/graph/__init__.py @@ -1,3 +1,3 @@ -from randomwalk import RandomWalk -from allgraphlets import All34Graphlets -from shortestpath import ShortestPath \ No newline at end of file +from .randomwalk import RandomWalk +from .allgraphlets import All34Graphlets +from .shortestpath import ShortestPath diff --git a/pykernels/graph/allgraphlets.py b/pykernels/graph/allgraphlets.py index 4681328..497214d 100644 --- a/pykernels/graph/allgraphlets.py +++ b/pykernels/graph/allgraphlets.py @@ -6,8 +6,9 @@ import itertools import numpy as np -from pykernels.base import Kernel, GraphKernel -import basic +from ..base import Kernel, GraphKernel +from .basic import graphs_to_adjacency_lists + def dec2bin(k, bitlength=0): """Decimal to binary""" @@ -114,8 +115,8 @@ def __init__(self, k=3): self.graphlet_array = _generate_graphlets(k) def _compute(self, data_1, data_2): - data_1 = basic.graphs_to_adjacency_lists(data_1) - data_2 = basic.graphs_to_adjacency_lists(data_2) + data_1 = graphs_to_adjacency_lists(data_1) + data_2 = graphs_to_adjacency_lists(data_2) d1 = np.zeros((data_1.shape[0], _number_of_graphlets(self.k))) d2 = np.zeros((data_2.shape[0], _number_of_graphlets(self.k))) for i, g in enumerate(data_1): diff --git a/pykernels/graph/basic.py b/pykernels/graph/basic.py index effd26c..82f07a1 100644 --- a/pykernels/graph/basic.py +++ b/pykernels/graph/basic.py @@ -21,10 +21,10 @@ def graphs_to_adjacency_lists(data): try: if data.ndim == 3: return np.array(data) - except Exception, exc: + except Exception: try: return np.array([G.adjacency_matix for G in data]) - except Exception, exc: + except Exception: return np.array(data) def relabel(data, data_2): diff --git a/pykernels/graph/randomwalk.py b/pykernels/graph/randomwalk.py index 7a69088..c85f229 100644 --- a/pykernels/graph/randomwalk.py +++ b/pykernels/graph/randomwalk.py @@ -5,10 +5,10 @@ __author__ = 'kasiajanocha' import numpy as np -from pykernels.base import Kernel, GraphKernel from scipy.sparse import lil_matrix, kron,identity from scipy.sparse.linalg import lsqr -import basic +from ..base import Kernel, GraphKernel +from .basic import graphs_to_adjacency_lists def _norm(adj_mat): """Normalize adjacency matrix""" @@ -29,8 +29,8 @@ def __init__(self, lmb=0.5, tolerance=1e-8, maxiter=20): # either tensor of dimention 3 (list of adjacency matrices) def _compute(self, data_1, data_2): - data_1 = basic.graphs_to_adjacency_lists(data_1) - data_2 = basic.graphs_to_adjacency_lists(data_2) + data_1 = graphs_to_adjacency_lists(data_1) + data_2 = graphs_to_adjacency_lists(data_2) res = np.zeros((len(data_1), len(data_2))) N = len(data_1) * len(data_2) for i, graph1 in enumerate(data_1): diff --git a/pykernels/graph/shortestpath.py b/pykernels/graph/shortestpath.py index d62f627..1afe552 100644 --- a/pykernels/graph/shortestpath.py +++ b/pykernels/graph/shortestpath.py @@ -2,12 +2,16 @@ A module containing Shortest Path Kernel. """ __author__ = 'kasiajanocha' - import numpy as np import numpy.matlib as matlib -import basic -from pykernels.base import Kernel, GraphKernel from scipy.sparse import lil_matrix +from ..base import Kernel, GraphKernel +from .basic import graphs_to_adjacency_lists, relabel +try: + range = xrange +except: + pass + def floyd_warshall(adj_mat, weights): """ @@ -18,9 +22,9 @@ def floyd_warshall(adj_mat, weights): res = res + ((adj_mat != 0) * weights) res[res == 0] = np.inf np.fill_diagonal(res, 0) - for i in xrange(N): - for j in xrange(N): - for k in xrange(N): + for i in range(N): + for j in range(N): + for k in range(N): if res[i, j] + res[j, k] < res[i, k]: res[i, k] = res[i, j] + res[j, k] return res @@ -85,8 +89,8 @@ def _create_accum_list(self, shortest_paths, maxpath): return res def _compute(self, data_1, data_2): - ams_1 = basic.graphs_to_adjacency_lists(data_1) - ams_2 = basic.graphs_to_adjacency_lists(data_2) + ams_1 = graphs_to_adjacency_lists(data_1) + ams_2 = graphs_to_adjacency_lists(data_2) sp_1, max1 = _apply_floyd_warshall(np.array(ams_1)) sp_2, max2 = _apply_floyd_warshall(np.array(ams_2)) maxpath = max(max1, max2) @@ -94,7 +98,7 @@ def _compute(self, data_1, data_2): accum_list_1 = self._create_accum_list(sp_1, maxpath) accum_list_2 = self._create_accum_list(sp_2, maxpath) else: - labels_1, labels_2, numlabels = basic.relabel( + labels_1, labels_2, numlabels = relabel( [G.node_labels for G in data_1], [G.node_labels for G in data_2]) accum_list_1 = self._create_accum_list_labeled(sp_1, maxpath, labels_1, numlabels) diff --git a/pykernels/regular.py b/pykernels/regular.py index c65f01e..bafa655 100644 --- a/pykernels/regular.py +++ b/pykernels/regular.py @@ -5,10 +5,11 @@ __author__ = 'lejlot' -from pykernels.base import Kernel import numpy as np -from utils import euclidean_dist_matrix import warnings +from .base import Kernel +from .utils import euclidean_dist_matrix + class Cossim(Kernel): """