Skip to content

Commit

Permalink
Merge pull request #17 from nwschurink/dev
Browse files Browse the repository at this point in the history
BUG: Fix Python 3 compatibility
  • Loading branch information
garydoranjr authored Jul 5, 2019
2 parents df22c4f + 3a03a49 commit edde284
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions misvm/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def set_kernel(k, normalizer=no_norm):
def K(X, Y):
if type(X) == list:
norm = lambda x: normalizer(x, k)
x_norm = matrix(map(norm, X))
x_norm = matrix(list(map(norm, X)))
if id(X) == id(Y):
# Optimize for symmetric case
norms = x_norm.T * x_norm
Expand All @@ -134,11 +134,11 @@ def K(X, Y):
diag = np.array([np.sum(k(x, x)) for x in X])
raw_kernel = upper + upper.T + spdiag(diag)
else:
y_norm = matrix(map(norm, Y))
y_norm = matrix(list(map(norm, Y)))
norms = x_norm.T * y_norm
raw_kernel = k(vstack(X), vstack(Y))
lensX = map(len, X)
lensY = map(len, Y)
lensX = list(map(len, X))
lensY = list(map(len, Y))
if any(l != 1 for l in lensX):
raw_kernel = vstack([np.sum(raw_kernel[i:j, :], axis=0)
for i, j in slices(lensX)])
Expand Down
4 changes: 2 additions & 2 deletions misvm/mica.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def fit(self, bags, y):
object containing m instances with k features
@param y : an array-like object of length n containing -1/+1 labels
"""
self._bags = map(np.asmatrix, bags)
self._bags = list(map(np.asmatrix, bags))
bs = BagSplitter(self._bags,
np.asmatrix(y).reshape((-1, 1)))
self._X = bs.instances
Expand Down Expand Up @@ -235,4 +235,4 @@ def t(list_of_lists):
Transpose a list of lists, since 'sparse'
takes arguments in column-major order.
"""
return map(list, zip(*list_of_lists))
return list(map(list, zip(*list_of_lists)))
2 changes: 1 addition & 1 deletion misvm/misssvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def fit(self, bags, y):
object containing m instances with k features
@param y : an array-like object of length n containing -1/+1 labels
"""
self._bags = map(np.asmatrix, bags)
self._bags = list(map(np.asmatrix, bags))
bs = BagSplitter(self._bags,
np.asmatrix(y).reshape((-1, 1)))
self._X = np.vstack([bs.pos_instances,
Expand Down
4 changes: 2 additions & 2 deletions misvm/nsk.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def fit(self, bags, y):
object containing m instances with k features
@param y : an array-like object of length n containing -1/+1 labels
"""
self._bags = map(np.asmatrix, bags)
self._bags = list(map(np.asmatrix, bags))
self._y = np.asmatrix(y).reshape((-1, 1))
if self.scale_C:
C = self.C / float(len(self._bags))
Expand Down Expand Up @@ -93,5 +93,5 @@ def predict(self, bags):
return np.zeros(len(bags))
else:
kernel = kernel_by_name(self.kernel, p=self.p, gamma=self.gamma)
K = kernel(map(np.asmatrix, bags), self._sv_bags)
K = kernel(list(map(np.asmatrix, bags)), self._sv_bags)
return np.array(self._b + K * spdiag(self._sv_y) * self._sv_alphas).reshape((-1,))
2 changes: 1 addition & 1 deletion misvm/sil.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ def get_params(self, deep=True):

def _inst_to_bag_preds(inst_preds, bags):
return np.array([np.max(inst_preds[slice(*bidx)])
for bidx in slices(map(len, bags))])
for bidx in slices(list(map(len, bags)))])
2 changes: 1 addition & 1 deletion misvm/smil.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def fit(self, bags, y):
object containing m instances with k features
@param y : an array-like object of length n containing -1/+1 labels
"""
bs = BagSplitter(map(np.asmatrix, bags),
bs = BagSplitter(list(map(np.asmatrix, bags)),
np.asmatrix(y).reshape((-1, 1)))
self._bags = bs.neg_inst_as_bags + bs.pos_bags
self._y = np.matrix(np.vstack([-np.ones((bs.L_n, 1)),
Expand Down

0 comments on commit edde284

Please sign in to comment.