Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pymf/aa.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import numpy as np
from cvxopt import solvers, base

from svd import pinv
from base import PyMFBase
from pymf.svd import pinv
from pymf.base import PyMFBase
__all__ = ["AA"]

class AA(PyMFBase):
Expand Down Expand Up @@ -104,7 +104,7 @@ def update_single_h(i):
INQb = base.matrix(0.0, (self._num_bases,1))
EQa = base.matrix(1.0, (1, self._num_bases))

for i in xrange(self._num_samples):
for i in range(self._num_samples):
update_single_h(i)

def _update_w(self):
Expand All @@ -126,7 +126,7 @@ def update_single_w(i):
INQb = base.matrix(0.0, (self._num_samples, 1))
EQa = base.matrix(1.0, (1, self._num_samples))

for i in xrange(self._num_bases):
for i in range(self._num_bases):
update_single_w(i)

self.W = np.dot(self.beta, self.data.T).T
Expand Down
6 changes: 3 additions & 3 deletions pymf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ def factorize(self, niter=100, show_progress=False,
if compute_err:
self.ferr = np.zeros(niter)

for i in xrange(niter):
for i in range(niter):
if compute_w:
self._update_w()

if compute_h:
self._update_h()
self._update_h()

if compute_err:
self.ferr[i] = self.frobenius_norm()
self._logger.info('FN: %s (%s/%s)' %(self.ferr[i], i+1, niter))
Expand Down
3 changes: 2 additions & 1 deletion pymf/bnmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
Applications. ICDM 2007
"""
import numpy as np
from base import PyMFBase
from pymf.base import PyMFBase

__all__ = ["BNMF"]


class BNMF(PyMFBase):
"""
BNMF(data, data, num_bases=4)
Expand Down
8 changes: 4 additions & 4 deletions pymf/chnmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import numpy as np

from itertools import combinations
from dist import vq
from pca import PCA
from aa import AA
from pymf.dist import vq
from pymf.pca import PCA
from pymf.aa import AA

__all__ = ["CHNMF"]

Expand Down Expand Up @@ -171,7 +171,7 @@ def select_hull_points(data, n=3):
pcamodel = PCA(self.data)
pcamodel.factorize(show_progress=False)
proj = pcamodel.H
print "PROJ", proj.shape
print("PROJ", proj.shape)
else:
R = np.random.randn(self._base_sel, self._data_dimension)
proj = np.dot(R, self.data)
Expand Down
2 changes: 1 addition & 1 deletion pymf/cmde.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Mining.
"""
import numpy as np
from cur import CUR
from pymf.cur import CUR

__all__ = ["CMD"]

Expand Down
6 changes: 3 additions & 3 deletions pymf/cmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"""
import numpy as np

import dist
from base import PyMFBase
from pymf.dist import pdist
from pymf.base import PyMFBase

__all__ = ["Cmeans"]

Expand Down Expand Up @@ -65,7 +65,7 @@ class Cmeans(PyMFBase):
def _update_h(self):
# assign samples to best matching centres ...
m = 1.75
tmp_dist = dist.pdist(self.W, self.data, metric='l2') + self._EPS
tmp_dist = pdist(self.W, self.data, metric='l2') + self._EPS
self.H[:,:] = 0.0

for i in range(self._num_bases):
Expand Down
6 changes: 3 additions & 3 deletions pymf/cnmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"""
import numpy as np
import logging
from kmeans import Kmeans
from base import PyMFBase
from pymf.kmeans import Kmeans
from pymf.base import PyMFBase

__all__ = ["CNMF"]

Expand Down Expand Up @@ -143,7 +143,7 @@ def separate_negative(m):
self.ferr = np.zeros(niter)
# iterate over W and H

for i in xrange(niter):
for i in range(niter):
# update H
XtX_neg_x_W = np.dot(XtX_neg, self.G)
XtX_pos_x_W = np.dot(XtX_pos, self.G)
Expand Down
4 changes: 2 additions & 2 deletions pymf/cur.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import numpy as np
import scipy.sparse

from svd import pinv
from base import PyMFBase3
from pymf.svd import pinv
from pymf.base import PyMFBase3


__all__ = ["CUR"]
Expand Down
4 changes: 2 additions & 2 deletions pymf/cursl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import numpy as np
import scipy.sparse

from svd import pinv, SVD
from cmde import CMD
from pymf.svd import SVD
from pymf.cmde import CMD

__all__ = ["CURSL"]

Expand Down
4 changes: 2 additions & 2 deletions pymf/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ def pdist(A, B, metric='l2' ):
# Returns a distance matrix d (n x m).
d = np.zeros((A.shape[1], B.shape[1]))
if A.shape[1] <= B.shape[1]:
for aidx in xrange(A.shape[1]):
for aidx in range(A.shape[1]):
if metric == 'l2':
d[aidx:aidx+1,:] = l2_distance(B[:,:], A[:,aidx:aidx+1]).reshape((1,-1))
if metric == 'l1':
d[aidx:aidx+1,:] = l1_distance(B[:,:], A[:,aidx:aidx+1]).reshape((1,-1))
else:
for bidx in xrange(B.shape[1]):
for bidx in range(B.shape[1]):
if metric == 'l2':
d[:, bidx:bidx+1] = l2_distance(A[:,:], B[:,bidx:bidx+1]).reshape((-1,1))
if metric == 'l1':
Expand Down
4 changes: 2 additions & 2 deletions pymf/greedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import time
import scipy.sparse
import numpy as np
from svd import *
from base import PyMFBase
from pymf.svd import pinv, SVD
from pymf.base import PyMFBase

__all__ = ["GREEDY"]

Expand Down
6 changes: 3 additions & 3 deletions pymf/greedycur.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
Reconstruction via Greedy Approximation of SVD. ISAAC'2008.
"""
import numpy as np
from greedy import GREEDY
from cur import CUR
from pymf.greedy import GREEDY
from pymf.cur import CUR

__all__ = ["GREEDYCUR"]

Expand Down Expand Up @@ -66,7 +66,7 @@ def sample(self, A, c):
"""
# set k to a value lower than the number of bases, usually
# gives better results.
k = np.round(c - c/5.0)
k = np.rint(c - c/5.0).astype(np.int64)
greedy_mdl = GREEDY(A, k=k, num_bases=c)
greedy_mdl.factorize(compute_h=False, compute_err=False, niter=1)
return greedy_mdl.select
Expand Down
8 changes: 4 additions & 4 deletions pymf/kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import numpy as np
import random

import dist
from base import PyMFBase
from pymf.base import PyMFBase
from pymf.dist import vq

__all__ = ["Kmeans"]

Expand Down Expand Up @@ -61,15 +61,15 @@ def _init_h(self):

def _init_w(self):
# set W to some random data samples
sel = random.sample(xrange(self._num_samples), self._num_bases)
sel = random.sample(range(self._num_samples), self._num_bases)

# sort indices, otherwise h5py won't work
self.W = self.data[:, np.sort(sel)]


def _update_h(self):
# and assign samples to the best matching centers
self.assigned = dist.vq(self.W, self.data)
self.assigned = vq(self.W, self.data)
self.H = np.zeros(self.H.shape)
self.H[self.assigned, range(self._num_samples)] = 1.0

Expand Down
4 changes: 1 addition & 3 deletions pymf/laesa.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
neighbour approximating and eliminating search algorithm with linear
preprocessing-time and memory requirements. Pattern Recognition Letters 1994.
"""
import scipy.sparse
import numpy as np

from dist import *
from sivm import SIVM
from pymf.sivm import SIVM

__all__ = ["LAESA"]

Expand Down
24 changes: 11 additions & 13 deletions pymf/nmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import scipy.sparse
import scipy.optimize
from cvxopt import solvers, base
from base import PyMFBase
from svd import pinv
from pymf.base import PyMFBase

__all__ = ["NMF", "RNMF", "NMFALS", "NMFNNLS"]

Expand Down Expand Up @@ -66,14 +65,14 @@ class NMF(PyMFBase):

def _update_h(self):
# pre init H1, and H2 (necessary for storing matrices on disk)
H2 = np.dot(np.dot(self.W.T, self.W), self.H) + 10**-9
self.H *= np.dot(self.W.T, self.data[:,:])
H2 = ((self.W.T @ self.W) @ self.H) + 10**-9
self.H *= self.W.T @ self.data
self.H /= H2

def _update_w(self):
# pre init W1, and W2 (necessary for storing matrices on disk)
W2 = np.dot(np.dot(self.W, self.H), self.H.T) + 10**-9
self.W *= np.dot(self.data[:,:], self.H.T)
W2 = ((self.W @ self.H) @ self.H.T) + 10**-9
self.W *= (self.data @ self.H.T)
self.W /= W2
self.W /= np.sqrt(np.sum(self.W**2.0, axis=0))

Expand Down Expand Up @@ -134,8 +133,7 @@ def soft_thresholding(self, X, lamb):
return X

def _init_h(self):
self.H = np.random.random((self._num_bases, self._num_samples))
self.H[:,:] = 1.0
self.H = np.ones((self._num_bases, self._num_samples), dtype=np.float64)

# normalized bases
Wnorm = np.sqrt(np.sum(self.W**2.0, axis=0))
Expand All @@ -146,7 +144,7 @@ def _init_h(self):

self._update_s()

def _update_s(self):
def _update_s(self):
self.S = self.data - np.dot(self.W, self.H)
self.S = self.soft_thresholding(self.S, self._lamb)

Expand Down Expand Up @@ -228,7 +226,7 @@ def updatesingleH(i):
INQa = base.matrix(-np.eye(self._num_bases))
INQb = base.matrix(0.0, (self._num_bases,1))

map(updatesingleH, xrange(self._num_samples))
list(map(updatesingleH, range(self._num_samples)))


def _update_w(self):
Expand All @@ -243,7 +241,7 @@ def updatesingleW(i):
INQa = base.matrix(-np.eye(self._num_bases))
INQb = base.matrix(0.0, (self._num_bases,1))

map(updatesingleW, xrange(self._data_dimension))
list(map(updatesingleW, range(self._data_dimension)))

self.W = self.W/np.sum(self.W, axis=1)

Expand Down Expand Up @@ -298,14 +296,14 @@ def _update_h(self):
def updatesingleH(i):
self.H[:,i] = scipy.optimize.nnls(self.W, self.data[:,i])[0]

map(updatesingleH, xrange(self._num_samples))
list(map(updatesingleH, range(self._num_samples)))


def _update_w(self):
def updatesingleW(i):
self.W[i,:] = scipy.optimize.nnls(self.H.T, self.data[i,:].T)[0]

map(updatesingleW, xrange(self._data_dimension))
list(map(updatesingleW, range(self._data_dimension)))


def _test():
Expand Down
6 changes: 3 additions & 3 deletions pymf/nndsvd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"""
import numpy as np

from base import PyMFBase
from svd import SVD
from nmf import NMF
from pymf.base import PyMFBase
from pymf.svd import SVD
from pymf.nmf import NMF

__all__ = ["NNDSVD"]

Expand Down
4 changes: 2 additions & 2 deletions pymf/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"""
import numpy as np

from base import PyMFBase
from svd import SVD
from pymf.base import PyMFBase
from pymf.svd import SVD


__all__ = ["PCA"]
Expand Down
5 changes: 3 additions & 2 deletions pymf/sivm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import scipy.sparse
import numpy as np

from dist import *
from aa import AA
from pymf.dist import l1_distance, l2_distance, cosine_distance, abs_cosine_distance, kl_divergence, weighted_abs_cosine_distance
from pymf.aa import AA

__all__ = ["SIVM"]


class SIVM(AA):
"""
SIVM(data, num_bases=4, dist_measure='l2')
Expand Down
6 changes: 3 additions & 3 deletions pymf/sivm_cur.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
Conf. on Information and Knowledge Management. ACM. 2010.
"""
import numpy as np
import scipy
from sivm import SIVM
from cur import CUR
from pymf.sivm import SIVM
from pymf.cur import CUR

__all__ = ["SIVM_CUR"]


class SIVM_CUR(CUR):
'''
SIVM_CUR(data, num_bases=4, dist_measure='l2')
Expand Down
2 changes: 1 addition & 1 deletion pymf/sivm_gsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def factorize(self, show_progress=False, compute_w=True, compute_h=True,
if compute_err:
self.ferr = np.zeros(niter)

for i in xrange(niter):
for i in range(niter):
if compute_w:
self._update_w()

Expand Down
4 changes: 2 additions & 2 deletions pymf/sivm_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import scipy.sparse
import numpy as np

from dist import pdist
from pymf.dist import pdist
from vol import *
from sivm import SIVM
from pymf.sivm import SIVM

__all__ = ["SIVM_SEARCH"]

Expand Down
Loading