Skip to content

Instantly share code, notes, and snippets.

@nadesai
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadesai/0727db6af37383a153e9 to your computer and use it in GitHub Desktop.
Save nadesai/0727db6af37383a153e9 to your computer and use it in GitHub Desktop.
output of 2to3 on nimfa
From 64f305eb0e05c75ccecf1d9378eab43d22818d2e Mon Sep 17 00:00:00 2001
From: Nikhil Desai <ndesai@iqt.org>
Date: Fri, 25 Jul 2014 13:48:20 -0700
Subject: [PATCH] Running 2to3 for python3 compatibility
---
docs/source/code/usage.py | 110 ++++++++++++++++----------------
docs/source/conf.py | 12 ++--
nimfa/__init__.py | 2 +-
nimfa/examples/__init__.py | 16 ++---
nimfa/examples/all_aml.py | 16 ++---
nimfa/examples/cbcl_images.py | 26 ++++----
nimfa/examples/documents.py | 30 ++++-----
nimfa/examples/gene_func_prediction.py | 72 ++++++++++-----------
nimfa/examples/medulloblastoma.py | 16 ++---
nimfa/examples/orl_images.py | 28 ++++----
nimfa/examples/recommendations.py | 32 +++++-----
nimfa/examples/synthetic.py | 26 ++++----
nimfa/methods/__init__.py | 4 +-
nimfa/methods/factorization/__init__.py | 24 +++----
nimfa/methods/factorization/bd.py | 16 ++---
nimfa/methods/factorization/bmf.py | 2 +-
nimfa/methods/factorization/icm.py | 18 +++---
nimfa/methods/factorization/lfnmf.py | 18 +++---
nimfa/methods/factorization/lsnmf.py | 12 ++--
nimfa/methods/factorization/nmf.py | 12 ++--
nimfa/methods/factorization/nsnmf.py | 4 +-
nimfa/methods/factorization/pmf.py | 10 +--
nimfa/methods/factorization/pmfcc.py | 2 +-
nimfa/methods/factorization/psmf.py | 62 +++++++++---------
nimfa/methods/factorization/snmf.py | 34 +++++-----
nimfa/methods/factorization/snmnmf.py | 2 +-
nimfa/methods/seeding/__init__.py | 10 +--
nimfa/methods/seeding/fixed.py | 2 +-
nimfa/methods/seeding/nndsvd.py | 8 +--
nimfa/methods/seeding/random_c.py | 10 +--
nimfa/methods/seeding/random_vcol.py | 2 +-
nimfa/mf_run.py | 6 +-
nimfa/models/__init__.py | 14 ++--
nimfa/models/nmf.py | 28 ++++----
nimfa/models/nmf_mm.py | 2 +-
nimfa/models/nmf_ns.py | 2 +-
nimfa/models/nmf_std.py | 2 +-
nimfa/utils/__init__.py | 4 +-
nimfa/utils/linalg.py | 58 ++++++++---------
39 files changed, 377 insertions(+), 377 deletions(-)
diff --git a/docs/source/code/usage.py b/docs/source/code/usage.py
index e8c9d78..bb7f5b6 100644
--- a/docs/source/code/usage.py
+++ b/docs/source/code/usage.py
@@ -14,7 +14,7 @@ from numpy import dot
V = csr_matrix((array([1,2,3,4,5,6]), array([0,2,2,0,1,2]), array([0,2,3,6])), shape=(3,3))
# Print this tiny matrix in dense format
-print V.todense()
+print((V.todense()))
# Run Standard NMF rank 4 algorithm
# Update equations and cost function are Standard NMF specific parameters (among others).
@@ -28,27 +28,27 @@ fctr_res = nimfa.mf_run(fctr)
# Basis matrix. It is sparse, as input V was sparse as well.
W = fctr_res.basis()
-print "Basis matrix"
-print W.todense()
+print("Basis matrix")
+print((W.todense()))
# Mixture matrix. We print this tiny matrix in dense format.
H = fctr_res.coef()
-print "Coef"
-print H.todense()
+print("Coef")
+print((H.todense()))
# Return the loss function according to Kullback-Leibler divergence. By default Euclidean metric is used.
-print "Distance Kullback-Leibler: %5.3e" % fctr_res.distance(metric = "kl")
+print(("Distance Kullback-Leibler: %5.3e" % fctr_res.distance(metric = "kl")))
# Compute generic set of measures to evaluate the quality of the factorization
sm = fctr_res.summary()
# Print sparseness (Hoyer, 2004) of basis and mixture matrix
-print "Sparseness Basis: %5.3f Mixture: %5.3f" % (sm['sparseness'][0], sm['sparseness'][1])
+print(("Sparseness Basis: %5.3f Mixture: %5.3f" % (sm['sparseness'][0], sm['sparseness'][1])))
# Print actual number of iterations performed
-print "Iterations: %d" % sm['n_iter']
+print(("Iterations: %d" % sm['n_iter']))
# Print estimate of target matrix V
-print "Estimate"
-print dot(W.todense(), H.todense())
+print("Estimate")
+print((dot(W.todense(), H.todense())))
####
@@ -64,7 +64,7 @@ import numpy as np
V = np.matrix([[1,2,3],[4,5,6],[6,7,8]])
# Print this tiny matrix
-print V
+print(V)
# Run LSNMF rank 3 algorithm
# We don't specify any algorithm specific parameters. Defaults will be used.
@@ -77,29 +77,29 @@ fctr_res = nimfa.mf_run(fctr)
# Basis matrix.
W = fctr_res.basis()
-print "Basis matrix"
-print W
+print("Basis matrix")
+print(W)
# Mixture matrix.
H = fctr_res.coef()
-print "Coef"
-print H
+print("Coef")
+print(H)
# Print the loss function according to Kullback-Leibler divergence. By default Euclidean metric is used.
-print "Distance Kullback-Leibler: %5.3e" % fctr_res.distance(metric = "kl")
+print(("Distance Kullback-Leibler: %5.3e" % fctr_res.distance(metric = "kl")))
# Compute generic set of measures to evaluate the quality of the factorization
sm = fctr_res.summary()
# Print residual sum of squares (Hutchins, 2008). Can be used for estimating optimal factorization rank.
-print "Rss: %8.3f" % sm['rss']
+print(("Rss: %8.3f" % sm['rss']))
# Print explained variance.
-print "Evar: %8.3f" % sm['evar']
+print(("Evar: %8.3f" % sm['evar']))
# Print actual number of iterations performed
-print "Iterations: %d" % sm['n_iter']
+print(("Iterations: %d" % sm['n_iter']))
# Print estimate of target matrix V
-print "Estimate"
-print np.dot(W, H)
+print("Estimate")
+print((np.dot(W, H)))
####
@@ -115,7 +115,7 @@ import numpy as np
V = np.matrix([[1,2,3],[4,5,6],[6,7,8]])
# Print this tiny matrix
-print V
+print(V)
# Run LSNMF rank 3 algorithm
# We don't specify any algorithm specific parameters. Defaults will be used.
@@ -130,29 +130,29 @@ fctr_res = nimfa.mf_run(fctr)
# Basis matrix.
W = fctr_res.basis()
-print "Basis matrix"
-print W
+print("Basis matrix")
+print(W)
# Mixture matrix.
H = fctr_res.coef()
-print "Coef"
-print H
+print("Coef")
+print(H)
# Error tracking.
-print "Error tracking"
+print("Error tracking")
# A list of objective function values for each iteration in factorization is printed.
# If error tracking is enabled and user specifies multiple runs of the factorization, get_error(run = n) return a list of objective values from n-th run.
# fctr_res.fit.tracker is an instance of Mf_track -- isinstance(fctr_res.fit.tracker, nimfa.models.mf_track.Mf_track)
-print fctr_res.fit.tracker.get_error()
+print((fctr_res.fit.tracker.get_error()))
# Compute generic set of measures to evaluate the quality of the factorization
sm = fctr_res.summary()
# Print residual sum of squares (Hutchins, 2008). Can be used for estimating optimal factorization rank.
-print "Rss: %8.3f" % sm['rss']
+print(("Rss: %8.3f" % sm['rss']))
# Print explained variance.
-print "Evar: %8.3f" % sm['evar']
+print(("Evar: %8.3f" % sm['evar']))
# Print actual number of iterations performed
-print "Iterations: %d" % sm['n_iter']
+print(("Iterations: %d" % sm['n_iter']))
####
@@ -168,14 +168,14 @@ import numpy as np
V = np.matrix([[1,2,3],[4,5,6],[6,7,8]])
# Print this tiny matrix
-print V
+print(V)
# This will be our callback_init function called prior to factorization.
# We will only print the initialized matrix factors.
def init_info(model):
- print "Initialized basis matrix\n", model.basis()
- print "Initialized mixture matrix\n", model.coef()
+ print(("Initialized basis matrix\n", model.basis()))
+ print(("Initialized mixture matrix\n", model.coef()))
# Run ICM rank 3 algorithm
# We don't specify any algorithm specific parameters. Defaults will be used.
@@ -190,26 +190,26 @@ fctr_res = nimfa.mf_run(fctr)
# Basis matrix.
W = fctr_res.basis()
-print "Resulting basis matrix"
-print W
+print("Resulting basis matrix")
+print(W)
# Mixture matrix.
H = fctr_res.coef()
-print "Resulting mixture matrix"
-print H
+print("Resulting mixture matrix")
+print(H)
# Compute generic set of measures to evaluate the quality of the factorization
sm = fctr_res.summary()
# Print residual sum of squares (Hutchins, 2008). Can be used for estimating optimal factorization rank.
-print "Rss: %8.3e" % sm['rss']
+print(("Rss: %8.3e" % sm['rss']))
# Print explained variance.
-print "Evar: %8.3e" % sm['evar']
+print(("Evar: %8.3e" % sm['evar']))
# Print actual number of iterations performed
-print "Iterations: %d" % sm['n_iter']
+print(("Iterations: %d" % sm['n_iter']))
# Print distance according to Kullback-Leibler divergence
-print "KL divergence: %5.3e" % sm['kl']
+print(("KL divergence: %5.3e" % sm['kl']))
# Print distance according to Euclidean metric
-print "Euclidean distance: %5.3e" % sm['euclidean']
+print(("Euclidean distance: %5.3e" % sm['euclidean']))
####
@@ -224,10 +224,10 @@ V = nimfa.examples.medulloblastoma.read(normalize = True)
fctr = nimfa.mf(V, seed = 'random_vcol', method = 'lsnmf', rank = 40, max_iter = 65)
fctr_res = nimfa.mf_run(fctr)
-print 'Rss: %5.4f' % fctr_res.fit.rss()
-print 'Evar: %5.4f' % fctr_res.fit.evar()
-print 'K-L divergence: %5.4f' % fctr_res.distance(metric = 'kl')
-print 'Sparseness, W: %5.4f, H: %5.4f' % fctr_res.fit.sparseness()
+print(('Rss: %5.4f' % fctr_res.fit.rss()))
+print(('Evar: %5.4f' % fctr_res.fit.evar()))
+print(('K-L divergence: %5.4f' % fctr_res.distance(metric = 'kl')))
+print(('Sparseness, W: %5.4f, H: %5.4f' % fctr_res.fit.sparseness()))
####
@@ -250,18 +250,18 @@ fctr = nimfa.mf(V, method = "bmf", max_iter = 10, rank = 30, n_run = 3, track_fa
fctr_res = nimfa.mf_run(fctr)
# Print the loss function according to Kullback-Leibler divergence.
-print "Distance Kullback-Leibler: %5.3e" % fctr_res.distance(metric = "kl")
+print(("Distance Kullback-Leibler: %5.3e" % fctr_res.distance(metric = "kl")))
# Compute generic set of measures to evaluate the quality of the factorization.
sm = fctr_res.summary()
# Print residual sum of squares.
-print "Rss: %8.3f" % sm['rss']
+print(("Rss: %8.3f" % sm['rss']))
# Print explained variance.
-print "Evar: %8.3f" % sm['evar']
+print(("Evar: %8.3f" % sm['evar']))
# Print actual number of iterations performed.
-print "Iterations: %d" % sm['n_iter']
+print(("Iterations: %d" % sm['n_iter']))
# Print cophenetic correlation. Can be used for rank estimation.
-print "cophenetic: %8.3f" % sm['cophenetic']
+print(("cophenetic: %8.3f" % sm['cophenetic']))
####
@@ -290,13 +290,13 @@ fctr = nimfa.mf(V, method = "nmf", seed = "fixed", W = init_W, H = init_H, rank
fctr_res = nimfa.mf_run(fctr)
# Print the loss function (Euclidean distance between target matrix and its estimate).
-print "Euclidean distance: %5.3e" % fctr_res.distance(metric = "euclidean")
+print(("Euclidean distance: %5.3e" % fctr_res.distance(metric = "euclidean")))
# It should print 'fixed'.
-print fctr_res.seeding
+print((fctr_res.seeding))
# By default, max 30 iterations are performed.
-print fctr_res.n_iter
+print((fctr_res.n_iter))
diff --git a/docs/source/conf.py b/docs/source/conf.py
index 6c8630c..ae94afa 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -40,8 +40,8 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
-project = u'nimfa - A Python Library for Nonnegative Matrix Factorization Techniques'
-copyright = u'2011, Marinka Zitnik'
+project = 'nimfa - A Python Library for Nonnegative Matrix Factorization Techniques'
+copyright = '2011, Marinka Zitnik'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
@@ -178,8 +178,8 @@ htmlhelp_basename = 'nimfa-APythonLibraryforNonnegativeMatrixFactorizationTechni
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
- ('index', 'nimfa-APythonLibraryforNonnegativeMatrixFactorizationTechniques.tex', u'nimfa - A Python Library for Nonnegative Matrix Factorization Techniques Documentation',
- u'Marinka Zitnik', 'manual'),
+ ('index', 'nimfa-APythonLibraryforNonnegativeMatrixFactorizationTechniques.tex', 'nimfa - A Python Library for Nonnegative Matrix Factorization Techniques Documentation',
+ 'Marinka Zitnik', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
@@ -211,8 +211,8 @@ latex_documents = [
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
- ('index', 'nimfa-APythonLibraryforNonnegativeMatrixFactorizationTechniques', u'nimfa - A Python Library for Nonnegative Matrix Factorization Techniques Documentation',
- [u'Marinka Zitnik'], 1)
+ ('index', 'nimfa-APythonLibraryforNonnegativeMatrixFactorizationTechniques', 'nimfa - A Python Library for Nonnegative Matrix Factorization Techniques Documentation',
+ ['Marinka Zitnik'], 1)
]
diff --git a/nimfa/__init__.py b/nimfa/__init__.py
index f7100cc..53d0d14 100644
--- a/nimfa/__init__.py
+++ b/nimfa/__init__.py
@@ -17,4 +17,4 @@ __version__ = "3.0"
__maintainer__ = "Marinka Zitnik"
__email__ = "marinka.zitnik@fri.uni-lj.si"
-from mf_run import *
+from .mf_run import *
diff --git a/nimfa/examples/__init__.py b/nimfa/examples/__init__.py
index 1765ac6..6413635 100644
--- a/nimfa/examples/__init__.py
+++ b/nimfa/examples/__init__.py
@@ -21,11 +21,11 @@
* recommendation systems.
"""
-import synthetic
-import all_aml
-import medulloblastoma
-import cbcl_images
-import documents
-import orl_images
-import recommendations
-import gene_func_prediction
+from . import synthetic
+from . import all_aml
+from . import medulloblastoma
+from . import cbcl_images
+from . import documents
+from . import orl_images
+from . import recommendations
+from . import gene_func_prediction
diff --git a/nimfa/examples/all_aml.py b/nimfa/examples/all_aml.py
index 79d1230..ca8c5c8 100644
--- a/nimfa/examples/all_aml.py
+++ b/nimfa/examples/all_aml.py
@@ -111,7 +111,7 @@ from warnings import warn
try:
from matplotlib.pyplot import savefig, imshow, set_cmap
-except ImportError, exc:
+except ImportError as exc:
warn("Matplotlib must be installed to run ALL AML example.")
@@ -119,7 +119,7 @@ def run():
"""Run Standard NMF on leukemia data set. For each rank 50 Standard NMF runs are performed. """
# read gene expression data
V = read()
- for rank in xrange(2, 4):
+ for rank in range(2, 4):
run_one(V, rank)
@@ -133,9 +133,9 @@ def run_one(V, rank):
:param rank: Factorization rank.
:type rank: `int`
"""
- print "================= Rank = %d =================" % rank
+ print(("================= Rank = %d =================" % rank))
consensus = np.mat(np.zeros((V.shape[1], V.shape[1])))
- for i in xrange(50):
+ for i in range(50):
# Standard NMF with Euclidean update equations is used. For initialization random Vcol method is used.
# Objective function is the number of consecutive iterations in which the connectivity matrix has not changed.
# We demand that factorization does not terminate before 30 consecutive iterations in which connectivity matrix
@@ -152,7 +152,7 @@ def run_one(V, rank):
conn_change=40,
initialize_only=True)
fit = nimfa.mf_run(model)
- print "%2d / 50 :: %s - init: %s ran with ... %3d / 200 iters ..." % (i + 1, fit.fit, fit.fit.seed, fit.fit.n_iter)
+ print(("%2d / 50 :: %s - init: %s ran with ... %3d / 200 iters ..." % (i + 1, fit.fit, fit.fit.seed, fit.fit.n_iter)))
# Compute connectivity matrix of factorization.
# Again, we could use multiple runs support of the nimfa library, track factorization model across 50 runs and then
# just call fit.consensus()
@@ -186,8 +186,8 @@ def reorder(C):
:param C: Consensus matrix.
:type C: `numpy.matrix`
"""
- c_vec = np.array([C[i, j] for i in xrange(C.shape[0] - 1)
- for j in xrange(i + 1, C.shape[1])])
+ c_vec = np.array([C[i, j] for i in range(C.shape[0] - 1)
+ for j in range(i + 1, C.shape[1])])
# convert similarities to distances
Y = 1 - c_vec
Z = linkage(Y, method='average')
@@ -208,7 +208,7 @@ def read():
V = np.matrix(np.zeros((5000, 38)))
i = 0
for line in open(dirname(dirname(abspath(__file__))) + sep + 'datasets' + sep + 'ALL_AML' + sep + 'ALL_AML_data.txt'):
- V[i, :] = map(float, line.split('\t'))
+ V[i, :] = list(map(float, line.split('\t')))
i += 1
return V
diff --git a/nimfa/examples/cbcl_images.py b/nimfa/examples/cbcl_images.py
index 8ac2892..173f19d 100644
--- a/nimfa/examples/cbcl_images.py
+++ b/nimfa/examples/cbcl_images.py
@@ -86,13 +86,13 @@ from warnings import warn
try:
from matplotlib.pyplot import savefig, imshow, set_cmap
-except ImportError, exc:
+except ImportError as exc:
warn("Matplotlib must be installed to run CBCL images example.")
try:
from PIL.Image import open, fromarray, new
from PIL.ImageOps import expand
-except ImportError, exc:
+except ImportError as exc:
warn("PIL must be installed to run CBCL images example.")
@@ -127,15 +127,15 @@ def factorize(V):
inner_sub_iter=10,
beta=0.1,
min_residuals=1e-8)
- print "Performing %s %s %d factorization ..." % (model, model.seed, model.rank)
+ print(("Performing %s %s %d factorization ..." % (model, model.seed, model.rank)))
fit = nimfa.mf_run(model)
- print "... Finished"
+ print("... Finished")
sparse_w, sparse_h = fit.fit.sparseness()
- print """Stats:
+ print(("""Stats:
- iterations: %d
- final projected gradients norm: %5.3f
- Euclidean distance: %5.3f
- - Sparseness basis: %5.3f, mixture: %5.3f""" % (fit.fit.n_iter, fit.distance(), fit.distance(metric='euclidean'), sparse_w, sparse_h)
+ - Sparseness basis: %5.3f, mixture: %5.3f""" % (fit.fit.n_iter, fit.distance(), fit.distance(metric='euclidean'), sparse_w, sparse_h)))
return fit.basis(), fit.coef()
@@ -147,14 +147,14 @@ def read():
Return the CBCL faces data matrix.
"""
- print "Reading CBCL faces database ..."
+ print("Reading CBCL faces database ...")
dir = dirname(dirname(abspath(__file__))) + sep + \
'datasets' + sep + 'CBCL_faces' + sep + 'face'
V = np.matrix(np.zeros((19 * 19, 2429)))
- for image in xrange(2429):
+ for image in range(2429):
im = open(dir + sep + "face0" + str(image + 1).zfill(4) + ".pgm")
V[:, image] = np.mat(np.asarray(im).flatten()).T
- print "... Finished."
+ print("... Finished.")
return V
@@ -167,14 +167,14 @@ def preprocess(V):
:param V: The CBCL faces data matrix.
:type V: `numpy.matrix`
"""
- print "Preprocessing data matrix ..."
+ print("Preprocessing data matrix ...")
V = V - V.mean()
V = V / np.sqrt(np.multiply(V, V).mean())
V = V + 0.25
V = V * 0.25
V = np.minimum(V, 1)
V = np.maximum(V, 0)
- print "... Finished."
+ print("... Finished.")
return V
@@ -187,8 +187,8 @@ def plot(W):
"""
set_cmap('gray')
blank = new("L", (133 + 6, 133 + 6))
- for i in xrange(7):
- for j in xrange(7):
+ for i in range(7):
+ for j in range(7):
basis = np.array(W[:, 7 * i + j])[:, 0].reshape((19, 19))
basis = basis / np.max(basis) * 255
basis = 255 - basis
diff --git a/nimfa/examples/documents.py b/nimfa/examples/documents.py
index 3f1b461..7e6df9e 100644
--- a/nimfa/examples/documents.py
+++ b/nimfa/examples/documents.py
@@ -105,7 +105,7 @@ from warnings import warn
try:
import matplotlib.pylab as plb
-except ImportError, exc:
+except ImportError as exc:
warn("Matplotlib must be installed to run Documents example.")
@@ -138,15 +138,15 @@ def factorize(V):
initialize_only=True,
update='divergence',
objective='div')
- print "Performing %s %s %d factorization ..." % (model, model.seed, model.rank)
+ print(("Performing %s %s %d factorization ..." % (model, model.seed, model.rank)))
fit = nimfa.mf_run(model)
- print "... Finished"
+ print("... Finished")
sparse_w, sparse_h = fit.fit.sparseness()
- print """Stats:
+ print(("""Stats:
- iterations: %d
- KL Divergence: %5.3f
- Euclidean distance: %5.3f
- - Sparseness basis: %5.3f, mixture: %5.3f""" % (fit.fit.n_iter, fit.distance(), fit.distance(metric='euclidean'), sparse_w, sparse_h)
+ - Sparseness basis: %5.3f, mixture: %5.3f""" % (fit.fit.n_iter, fit.distance(), fit.distance(metric='euclidean'), sparse_w, sparse_h)))
return fit.basis(), fit.coef()
@@ -160,7 +160,7 @@ def read():
Return the Medlars sparse data matrix in LIL format, term-to-index `dict` translator and index-to-term
`dict` translator.
"""
- print "Reading Medlars medical abstracts data set ..."
+ print("Reading Medlars medical abstracts data set ...")
dir = dirname(dirname(abspath(__file__))) + sep + \
'datasets' + sep + 'Medlars' + sep + 'med.all'
doc = open(dir)
@@ -169,7 +169,7 @@ def read():
idx2term = {}
n_free = 0
line = doc.readline()
- for abstract in xrange(1033):
+ for abstract in range(1033):
ii = int(line.split()[1])
# omit .W char
doc.readline()
@@ -183,7 +183,7 @@ def read():
n_free += 1
V[term2idx[term], ii - 1] += 1
line = doc.readline().strip()
- print "... Finished."
+ print("... Finished.")
return V, term2idx, idx2term
@@ -203,7 +203,7 @@ def preprocess(V, term2idx, idx2term):
:param idx2term: Index-to-term translator.
:type idx2term: `dict`
"""
- print "Preprocessing data matrix ..."
+ print("Preprocessing data matrix ...")
# remove stop words, digits, too short words
rem = set()
for term in term2idx:
@@ -211,10 +211,10 @@ def preprocess(V, term2idx, idx2term):
rem.add(term2idx[term])
# remove words that appear two times or less in corpus
V = V.tocsr()
- for r in xrange(V.shape[0]):
+ for r in range(V.shape[0]):
if V[r, :].sum() <= 2 or V[r,:].sum() >= 50:
rem.add(r)
- retain = set(xrange(V.shape[0])).difference(rem)
+ retain = set(range(V.shape[0])).difference(rem)
n_free = 0
V1 = sp.lil_matrix((V.shape[0] - len(rem), 1033))
for r in retain:
@@ -222,7 +222,7 @@ def preprocess(V, term2idx, idx2term):
idx2term[n_free] = idx2term[r]
V1[n_free, :] = V[r,:]
n_free += 1
- print "... Finished."
+ print("... Finished.")
return V1.tocsr(), term2idx, idx2term
@@ -235,8 +235,8 @@ def plot(W, idx2term):
:param idx2term: Index-to-term translator.
:type idx2term: `dict`
"""
- print "Plotting highest weighted terms in basis vectors ..."
- for c in xrange(W.shape[1]):
+ print("Plotting highest weighted terms in basis vectors ...")
+ for c in range(W.shape[1]):
if sp.isspmatrix(W):
top10 = sorted(
enumerate(W[:, c].todense().ravel().tolist()[0]), key=itemgetter(1), reverse=True)[:10]
@@ -253,7 +253,7 @@ def plot(W, idx2term):
plb.title("Highest Weighted Terms in Basis Vector W%d" % (c + 1))
plb.grid(True)
plb.savefig("documents_basisW%d.png" % (c + 1), bbox_inches="tight")
- print "... Finished."
+ print("... Finished.")
stop_words = [
"a", "able", "about", "across", "after", "all", "almost", "also", "am", "among", "an", "and", "any", "are", "as", "at", "be",
diff --git a/nimfa/examples/gene_func_prediction.py b/nimfa/examples/gene_func_prediction.py
index e0dddf0..a346d0b 100644
--- a/nimfa/examples/gene_func_prediction.py
+++ b/nimfa/examples/gene_func_prediction.py
@@ -62,7 +62,7 @@ from warnings import warn
try:
import matplotlib.pylab as plb
-except ImportError, exc:
+except ImportError as exc:
warn("Matplotlib must be installed to run Gene Function prediction example.")
@@ -96,7 +96,7 @@ def run():
# correlation computation
corrs = compute_correlations(tv_data, test_data)
for method in 0.5 * np.random.random_sample(50) + 1.:
- print method
+ print(method)
# class assignments
func2gene = assign_labels(corrs, tv_data, idx2class, method=method)
# precision and recall measurements
@@ -110,26 +110,26 @@ def read():
Return attributes' values and class information of the test data set and joined train and validation data set. Additional mapping functions
are returned mapping attributes' names and classes' names to indices.
"""
- print "Reading S. cerevisiae FunCat annotated sequence data set (D1 FC seq) ..."
+ print("Reading S. cerevisiae FunCat annotated sequence data set (D1 FC seq) ...")
dir = dirname(dirname(abspath(__file__))) + sep + 'datasets' + \
sep + 'S_cerevisiae_FC' + sep + 'seq_yeast_FUN' + sep
train_data = dir + 'seq_yeast_FUN.train.arff'
valid_data = dir + 'seq_yeast_FUN.valid.arff'
test_data = dir + 'seq_yeast_FUN.test.arff'
- print " Reading S. cerevisiae FunCat annotated sequence (D1 FC seq) TRAIN set ..."
+ print(" Reading S. cerevisiae FunCat annotated sequence (D1 FC seq) TRAIN set ...")
train, idx2attr, idx2class = transform_data(
train_data, include_meta=True)
- print " ... Finished."
- print " Reading S. cerevisiae FunCat annotated sequence (D1 FC seq) VALIDATION set ..."
+ print(" ... Finished.")
+ print(" Reading S. cerevisiae FunCat annotated sequence (D1 FC seq) VALIDATION set ...")
valid = transform_data(valid_data)
- print " ... Finished."
- print " Reading S. cerevisiae FunCat annotated sequence (D1 FC seq) TEST set ..."
+ print(" ... Finished.")
+ print(" Reading S. cerevisiae FunCat annotated sequence (D1 FC seq) TEST set ...")
test = transform_data(test_data)
- print " ... Finished."
- print " Joining S. cerevisiae FunCat annotated sequence (D1 FC seq) TEST and VALIDATION set ..."
+ print(" ... Finished.")
+ print(" Joining S. cerevisiae FunCat annotated sequence (D1 FC seq) TEST and VALIDATION set ...")
tv_data = _join(train, valid)
- print " ... Finished."
- print "... Finished"
+ print(" ... Finished.")
+ print("... Finished")
return tv_data, test, idx2attr, idx2class
@@ -174,14 +174,14 @@ def transform_data(path, include_meta=False):
idx += 1
if line_type == "@DATA":
section = 'd'
- idxs = set(xrange(idx)).intersection(used_idx)
+ idxs = set(range(idx)).intersection(used_idx)
attr_data = np.mat(np.zeros((1e4, len(attr2idx))))
class_data = np.mat(np.zeros((1e4, len(class2idx))))
elif section == 'd':
d, _, comment = line.strip().partition("%")
values = d.split(",")
# update class information for current feature
- class_var = map(str.strip, values[idx_class].split("@"))
+ class_var = list(map(str.strip, values[idx_class].split("@")))
for cl in class_var:
# update direct class information
class_data[feature, class2idx[cl]] += 10.
@@ -234,7 +234,7 @@ def _reverse(object2idx):
:type object2idx: `dict`
:rtype: `dict`
"""
- return dict(zip(object2idx.values(), object2idx.keys()))
+ return dict(list(zip(list(object2idx.values()), list(object2idx.keys()))))
def preprocess(data):
@@ -247,10 +247,10 @@ def preprocess(data):
:param data: Transformed data set containing attributes' values, class information and possibly additional meta information.
:type data: `tuple`
"""
- print "Preprocessing data matrix ..."
+ print("Preprocessing data matrix ...")
data['attr'] = (data['attr'] - data['attr'].min() + np.finfo(
data['attr'].dtype).eps) / (data['attr'].max() - data['attr'].min())
- print "... Finished."
+ print("... Finished.")
return data
@@ -283,15 +283,15 @@ def factorize(data):
beta=1e-4,
i_conv=10,
w_min_change=0)
- print "Performing %s %s %d factorization ..." % (model, model.seed, model.rank)
+ print(("Performing %s %s %d factorization ..." % (model, model.seed, model.rank)))
fit = nimfa.mf_run(model)
- print "... Finished"
+ print("... Finished")
sparse_w, sparse_h = fit.fit.sparseness()
- print """Stats:
+ print(("""Stats:
- iterations: %d
- KL Divergence: %5.3f
- Euclidean distance: %5.3f
- - Sparseness basis: %5.3f, mixture: %5.3f""" % (fit.fit.n_iter, fit.distance(), fit.distance(metric='euclidean'), sparse_w, sparse_h)
+ - Sparseness basis: %5.3f, mixture: %5.3f""" % (fit.fit.n_iter, fit.distance(), fit.distance(metric='euclidean'), sparse_w, sparse_h)))
data['W'] = fit.basis()
data['H'] = fit.coef()
return data
@@ -309,7 +309,7 @@ def compute_correlations(train, test):
:type test: `dict`
:rtype: `numpy.matrix`
"""
- print "Estimating correlation coefficients ..."
+ print("Estimating correlation coefficients ...")
corrs = np.corrcoef(train['W'], test['W'])
# alternative, it is time consuming - can be used for partial evaluation
"""corrs = {}
@@ -317,7 +317,7 @@ def compute_correlations(train, test):
corrs.setdefault(i, np.mat(np.zeros((train['W'].shape[0], 1))))
for j in xrange(train['W'].shape[0]):
corrs[i][j, 0] = _corr(test['W'][i, :], train['W'][j, :])"""
- print "... Finished."
+ print("... Finished.")
return np.mat(corrs)
@@ -389,14 +389,14 @@ def assign_labels(corrs, train, idx2class, method=0.):
:type method: `float` or `str`
:rtype: `dict`
"""
- print "Assigning class labels - gene functions to genes ..."
+ print("Assigning class labels - gene functions to genes ...")
func2gene = {}
n_train = train['feat']
n_cl = len(idx2class)
- for cl_idx in xrange(n_cl):
+ for cl_idx in range(n_cl):
func2gene.setdefault(cl_idx, [])
key = 0
- for test_idx in xrange(n_train, corrs.shape[0]):
+ for test_idx in range(n_train, corrs.shape[0]):
if method == "average":
# weighted summation of correlations over respective index sets
avg_corr_A = np.sum(
@@ -405,7 +405,7 @@ def assign_labels(corrs, train, idx2class, method=0.):
np.multiply(np.tile(corrs[:n_train, test_idx], (1, n_cl)), train['class'] != 0), 0)
avg_corr_A = avg_corr_A / (np.sum(train['class'] != 0, 0) + 1)
avg_corr_B = avg_corr_B / (np.sum(train['class'] == 0, 0) + 1)
- for cl_idx in xrange(n_cl):
+ for cl_idx in range(n_cl):
if (avg_corr_A[0, cl_idx] > avg_corr_B[0, cl_idx]):
func2gene[cl_idx].append(key)
elif method == "maximal":
@@ -413,21 +413,21 @@ def assign_labels(corrs, train, idx2class, method=0.):
np.multiply(np.tile(corrs[:n_train, test_idx], (1, n_cl)), train['class']), 0)
max_corr_B = np.amax(
np.multiply(np.tile(corrs[:n_train, test_idx], (1, n_cl)), train['class'] != 0), 0)
- for cl_idx in xrange(n_cl):
+ for cl_idx in range(n_cl):
if (max_corr_A[0, cl_idx] > max_corr_B[0, cl_idx]):
func2gene[cl_idx].append(key)
elif isinstance(method, float):
max_corr = np.amax(
np.multiply(np.tile(corrs[:n_train, test_idx], (1, n_cl)), train['class']), 0)
- for cl_idx in xrange(n_cl):
+ for cl_idx in range(n_cl):
if (max_corr[0, cl_idx] >= method):
func2gene[cl_idx].append(key)
else:
raise ValueError("Unrecognized class assignment rule.")
key += 1
if key % 100 == 0:
- print " %d/%d" % (key, corrs.shape[0] - n_train)
- print "... Finished."
+ print((" %d/%d" % (key, corrs.shape[0] - n_train)))
+ print("... Finished.")
return func2gene
@@ -452,7 +452,7 @@ def plot(func2gene, test, idx2class):
:type idx2class: `dict`
:rtype: `tuple`
"""
- print "Computing PR evaluations measures ..."
+ print("Computing PR evaluations measures ...")
def tp(g_function):
# number of true positives for g_function (correctly predicted positive
@@ -468,7 +468,7 @@ def plot(func2gene, test, idx2class):
# number of false negatives for g_function (positive instances that are
# incorrectly predicted negative)
n_pred = list(
- set(xrange(len(idx2class))).difference(func2gene[g_function]))
+ set(range(len(idx2class))).difference(func2gene[g_function]))
return (test['class'][n_pred, g_function] != 0).sum()
tp_sum = 0.
fp_sum = 0.
@@ -479,9 +479,9 @@ def plot(func2gene, test, idx2class):
fn_sum += fn(g_function)
avg_precision = tp_sum / (tp_sum + fp_sum)
avg_recall = tp_sum / (tp_sum + fn_sum)
- print "Average precision over all gene functions: %5.3f" % avg_precision
- print "Average recall over all gene functions: %5.3f" % avg_recall
- print "... Finished."
+ print(("Average precision over all gene functions: %5.3f" % avg_precision))
+ print(("Average recall over all gene functions: %5.3f" % avg_recall))
+ print("... Finished.")
return avg_precision, avg_recall
if __name__ == "__main__":
diff --git a/nimfa/examples/medulloblastoma.py b/nimfa/examples/medulloblastoma.py
index d63f62d..b497933 100644
--- a/nimfa/examples/medulloblastoma.py
+++ b/nimfa/examples/medulloblastoma.py
@@ -101,7 +101,7 @@ from warnings import warn
try:
from matplotlib.pyplot import savefig, imshow, set_cmap
-except ImportError, exc:
+except ImportError as exc:
warn("Matplotlib must be installed to run Medulloblastoma example.")
@@ -109,7 +109,7 @@ def run():
"""Run Standard NMF on medulloblastoma data set. For each rank 50 Standard NMF runs are performed. """
# read gene expression data
V = read()
- for rank in xrange(2, 4):
+ for rank in range(2, 4):
run_one(V, rank)
@@ -123,9 +123,9 @@ def run_one(V, rank):
:param rank: Factorization rank.
:type rank: `int`
"""
- print "================= Rank = %d =================" % rank
+ print(("================= Rank = %d =================" % rank))
consensus = np.mat(np.zeros((V.shape[1], V.shape[1])))
- for i in xrange(50):
+ for i in range(50):
# Standard NMF with Euclidean update equations is used. For initialization random Vcol method is used.
# Objective function is the number of consecutive iterations in which the connectivity matrix has not changed.
# We demand that factorization does not terminate before 30 consecutive iterations in which connectivity matrix
@@ -142,7 +142,7 @@ def run_one(V, rank):
conn_change=40,
initialize_only=True)
fit = nimfa.mf_run(model)
- print "%2d / 50 :: %s - init: %s ran with ... %3d / 200 iters ..." % (i + 1, fit.fit, fit.fit.seed, fit.fit.n_iter)
+ print(("%2d / 50 :: %s - init: %s ran with ... %3d / 200 iters ..." % (i + 1, fit.fit, fit.fit.seed, fit.fit.n_iter)))
# Compute connectivity matrix of factorization.
# Again, we could use multiple runs support of the nimfa library, track factorization model across 50 runs and then
# just call fit.consensus()
@@ -176,8 +176,8 @@ def reorder(C):
:param C: Consensus matrix.
:type C: `numpy.matrix`
"""
- c_vec = np.array([C[i, j] for i in xrange(C.shape[0] - 1)
- for j in xrange(i + 1, C.shape[1])])
+ c_vec = np.array([C[i, j] for i in range(C.shape[0] - 1)
+ for j in range(i + 1, C.shape[1])])
# convert similarities to distances
Y = 1 - c_vec
Z = linkage(Y, method='average')
@@ -198,7 +198,7 @@ def read(normalize=False):
V = np.matrix(np.zeros((5893, 34)))
i = 0
for line in open(dirname(dirname(abspath(__file__))) + sep + 'datasets' + sep + 'Medulloblastoma' + sep + 'Medulloblastoma_data.txt'):
- V[i, :] = map(float, line.split('\t'))
+ V[i, :] = list(map(float, line.split('\t')))
i += 1
if normalize:
V -= V.min()
diff --git a/nimfa/examples/orl_images.py b/nimfa/examples/orl_images.py
index f4e885d..b8aa1a4 100644
--- a/nimfa/examples/orl_images.py
+++ b/nimfa/examples/orl_images.py
@@ -97,13 +97,13 @@ from warnings import warn
try:
from matplotlib.pyplot import savefig, imshow, set_cmap
-except ImportError, exc:
+except ImportError as exc:
warn("Matplotlib must be installed to run ORL images example.")
try:
from PIL.Image import open, fromarray, new
from PIL.ImageOps import expand
-except ImportError, exc:
+except ImportError as exc:
warn("PIL must be installed to run ORL images example.")
@@ -138,13 +138,13 @@ def factorize(V):
inner_sub_iter=10,
beta=0.1,
min_residuals=1e-8)
- print "Performing %s %s %d factorization ..." % (model, model.seed, model.rank)
+ print(("Performing %s %s %d factorization ..." % (model, model.seed, model.rank)))
fit = nimfa.mf_run(model)
- print "... Finished"
- print """Stats:
+ print("... Finished")
+ print(("""Stats:
- iterations: %d
- final projected gradients norm: %5.3f
- - Euclidean distance: %5.3f""" % (fit.fit.n_iter, fit.distance(), fit.distance(metric='euclidean'))
+ - Euclidean distance: %5.3f""" % (fit.fit.n_iter, fit.distance(), fit.distance(metric='euclidean'))))
return fit.basis(), fit.coef()
@@ -156,17 +156,17 @@ def read():
Return the ORL faces data matrix.
"""
- print "Reading ORL faces database ..."
+ print("Reading ORL faces database ...")
dir = dirname(dirname(abspath(__file__))) + \
sep + 'datasets' + sep + 'ORL_faces' + sep + 's'
V = np.matrix(np.zeros((46 * 56, 400)))
- for subject in xrange(40):
- for image in xrange(10):
+ for subject in range(40):
+ for image in range(10):
im = open(dir + str(subject + 1) + sep + str(image + 1) + ".pgm")
# reduce the size of the image
im = im.resize((46, 56))
V[:, image * subject + image] = np.mat(np.asarray(im).flatten()).T
- print "... Finished."
+ print("... Finished.")
return V
@@ -179,14 +179,14 @@ def preprocess(V):
:param V: The ORL faces data matrix.
:type V: `numpy.matrix`
"""
- print "Preprocessing data matrix ..."
+ print("Preprocessing data matrix ...")
min_val = V.min(axis=0)
V = V - np.mat(np.ones((V.shape[0], 1))) * min_val
max_val = V.max(axis=0) + 1e-4
V = (255. * V) / (np.mat(np.ones((V.shape[0], 1))) * max_val)
# avoid too large values
V = V / 100.
- print "... Finished."
+ print("... Finished.")
return V
@@ -199,8 +199,8 @@ def plot(W):
"""
set_cmap('gray')
blank = new("L", (225 + 6, 280 + 6))
- for i in xrange(5):
- for j in xrange(5):
+ for i in range(5):
+ for j in range(5):
basis = np.array(W[:, 5 * i + j])[:, 0].reshape((56, 46))
basis = basis / np.max(basis) * 255
basis = 255 - basis
diff --git a/nimfa/examples/recommendations.py b/nimfa/examples/recommendations.py
index 18092da..1da9558 100644
--- a/nimfa/examples/recommendations.py
+++ b/nimfa/examples/recommendations.py
@@ -48,7 +48,7 @@ from warnings import warn
try:
import matplotlib.pylab as plb
-except ImportError, exc:
+except ImportError as exc:
warn("Matplotlib must be installed to run Recommendations example.")
@@ -91,14 +91,14 @@ def factorize(V):
beta=1e-4,
i_conv=10,
w_min_change=0)
- print "Performing %s %s %d factorization ..." % (model, model.seed, model.rank)
+ print(("Performing %s %s %d factorization ..." % (model, model.seed, model.rank)))
fit = nimfa.mf_run(model)
- print "... Finished"
+ print("... Finished")
sparse_w, sparse_h = fit.fit.sparseness()
- print """Stats:
+ print(("""Stats:
- iterations: %d
- Euclidean distance: %5.3f
- - Sparseness basis: %5.3f, mixture: %5.3f""" % (fit.fit.n_iter, fit.distance(metric='euclidean'), sparse_w, sparse_h)
+ - Sparseness basis: %5.3f, mixture: %5.3f""" % (fit.fit.n_iter, fit.distance(metric='euclidean'), sparse_w, sparse_h)))
return fit.basis(), fit.coef()
@@ -114,14 +114,14 @@ def read(data_set):
:param data_set: Name of the split data set to be read.
:type data_set: `str`
"""
- print "Reading MovieLens ratings data set ..."
+ print("Reading MovieLens ratings data set ...")
dir = dirname(dirname(abspath(__file__))) + sep + \
'datasets' + sep + 'MovieLens' + sep + data_set + '.base'
V = sp.lil_matrix((943, 1682))
for line in open(dir):
- u, i, r, _ = map(int, line.split())
+ u, i, r, _ = list(map(int, line.split()))
V[u - 1, i - 1] = r
- print "... Finished."
+ print("... Finished.")
return V
@@ -135,17 +135,17 @@ def preprocess(V):
:param V: The MovieLens data matrix.
:type V: `scipy.sparse.lil_matrix`
"""
- print "Preprocessing data matrix ..."
+ print("Preprocessing data matrix ...")
V = V.tocsr()
- maxs = [np.max(V[i, :].todense()) for i in xrange(V.shape[0])]
+ maxs = [np.max(V[i, :].todense()) for i in range(V.shape[0])]
now = 0
- for row in xrange(V.shape[0]):
+ for row in range(V.shape[0]):
upto = V.indptr[row + 1]
while now < upto:
col = V.indices[now]
V.data[now] /= maxs[row]
now += 1
- print "... Finished."
+ print("... Finished.")
return V, maxs
@@ -162,18 +162,18 @@ def plot(W, H, data_set, maxs):
:param maxs: Users' maximum ratings (used in normalization).
:type maxs: `list`
"""
- print "Plotting RMSE rates ..."
+ print("Plotting RMSE rates ...")
dir = dirname(dirname(abspath(__file__))) + sep + \
'datasets' + sep + 'MovieLens' + sep + data_set + '.test'
rmse = 0
n = 0
for line in open(dir):
- u, i, r, _ = map(int, line.split())
+ u, i, r, _ = list(map(int, line.split()))
rmse += ((W[u - 1, :] * H[:, i - 1])[0, 0] + maxs[u - 1] - r) ** 2
n += 1
rmse /= n
- print rmse
- print "... Finished."
+ print(rmse)
+ print("... Finished.")
if __name__ == "__main__":
"""Run the Recommendations example."""
diff --git a/nimfa/examples/synthetic.py b/nimfa/examples/synthetic.py
index 049b38e..36d3f26 100644
--- a/nimfa/examples/synthetic.py
+++ b/nimfa/examples/synthetic.py
@@ -58,29 +58,29 @@ def print_info(fit, idx=None):
multiple NMF model.
:type idx: `str` with values 'coef' or 'coef1' (`int` value of 0 or 1, respectively)
"""
- print "================================================================================================="
- print "Factorization method:", fit.fit
- print "Initialization method:", fit.fit.seed
- print "Basis matrix W: "
- print __fact_factor(fit.basis())
- print "Mixture (Coefficient) matrix H%d: " % (idx if idx != None else 0)
- print __fact_factor(fit.coef(idx))
- print "Distance (Euclidean): ", fit.distance(metric='euclidean', idx=idx)
+ print("=================================================================================================")
+ print(("Factorization method:", fit.fit))
+ print(("Initialization method:", fit.fit.seed))
+ print("Basis matrix W: ")
+ print((__fact_factor(fit.basis())))
+ print(("Mixture (Coefficient) matrix H%d: " % (idx if idx != None else 0)))
+ print((__fact_factor(fit.coef(idx))))
+ print(("Distance (Euclidean): ", fit.distance(metric='euclidean', idx=idx)))
# We can access actual number of iteration directly through fitted model.
# fit.fit.n_iter
- print "Actual number of iterations: ", fit.summary(idx)['n_iter']
+ print(("Actual number of iterations: ", fit.summary(idx)['n_iter']))
# We can access sparseness measure directly through fitted model.
# fit.fit.sparseness()
- print "Sparseness basis: %7.4f, Sparseness mixture: %7.4f" % (fit.summary(idx)['sparseness'][0], fit.summary(idx)['sparseness'][1])
+ print(("Sparseness basis: %7.4f, Sparseness mixture: %7.4f" % (fit.summary(idx)['sparseness'][0], fit.summary(idx)['sparseness'][1])))
# We can access explained variance directly through fitted model.
# fit.fit.evar()
- print "Explained variance: ", fit.summary(idx)['evar']
+ print(("Explained variance: ", fit.summary(idx)['evar']))
# We can access residual sum of squares directly through fitted model.
# fit.fit.rss()
- print "Residual sum of squares: ", fit.summary(idx)['rss']
+ print(("Residual sum of squares: ", fit.summary(idx)['rss']))
# There are many more ... but just cannot print out everything =] and some measures need additional data or more runs
# e.g. entropy, predict, purity, coph_cor, consensus, select_features, score_features, connectivity
- print "================================================================================================="
+ print("=================================================================================================")
def run_snmnmf(V, V1):
diff --git a/nimfa/methods/__init__.py b/nimfa/methods/__init__.py
index e418f39..c5924fd 100644
--- a/nimfa/methods/__init__.py
+++ b/nimfa/methods/__init__.py
@@ -4,8 +4,8 @@
"""
-import factorization
-import seeding
+from . import factorization
+from . import seeding
def list_mf_methods():
diff --git a/nimfa/methods/factorization/__init__.py b/nimfa/methods/factorization/__init__.py
index 2e70648..6b8f253 100644
--- a/nimfa/methods/factorization/__init__.py
+++ b/nimfa/methods/factorization/__init__.py
@@ -4,18 +4,18 @@
"""
-import bd
-import icm
-import lfnmf
-import lsnmf
-import nmf
-import nsnmf
-import pmf
-import psmf
-import snmf
-import bmf
-import snmnmf
-import pmfcc
+from . import bd
+from . import icm
+from . import lfnmf
+from . import lsnmf
+from . import nmf
+from . import nsnmf
+from . import pmf
+from . import psmf
+from . import snmf
+from . import bmf
+from . import snmnmf
+from . import pmfcc
methods = {"bd": bd.Bd,
"icm": icm.Icm,
diff --git a/nimfa/methods/factorization/bd.py b/nimfa/methods/factorization/bd.py
index caedc3f..8b6fcf2 100644
--- a/nimfa/methods/factorization/bd.py
+++ b/nimfa/methods/factorization/bd.py
@@ -105,7 +105,7 @@ class Bd(nmf_std.Nmf_std):
"""
self.v = multiply(self.V, self.V).sum() / 2.
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
p_obj = c_obj = sys.float_info.max
@@ -198,13 +198,13 @@ class Bd(nmf_std.Nmf_std):
def update(self, iter):
"""Update basis and mixture matrix."""
- for _ in xrange(self.skip * (iter == 0) + self.stride * (iter > 0)):
+ for _ in range(self.skip * (iter == 0) + self.stride * (iter > 0)):
# update basis matrix
C = dot(self.H, self.H.T)
D = dot(self.V, self.H.T)
- for n in xrange(self.rank):
+ for n in range(self.rank):
if not self.n_w[n]:
- nn = list(xrange(n)) + list(xrange(n + 1, self.rank))
+ nn = list(range(n)) + list(range(n + 1, self.rank))
temp = self._randr(
sop(D[:, n] - dot(self.W[:, nn], C[nn, n]), C[
n, n] + np.finfo(C.dtype).eps, div),
@@ -212,7 +212,7 @@ class Bd(nmf_std.Nmf_std):
if not sp.isspmatrix(self.W):
self.W[:, n] = temp
else:
- for j in xrange(self.W.shape[0]):
+ for j in range(self.W.shape[0]):
self.W[j, n] = temp[j]
# update sigma
if self.n_sigma == False:
@@ -225,9 +225,9 @@ class Bd(nmf_std.Nmf_std):
# update mixture matrix
E = dot(self.W.T, self.W)
F = dot(self.W.T, self.V)
- for n in xrange(self.rank):
+ for n in range(self.rank):
if not self.n_h[n]:
- nn = list(xrange(n)) + list(xrange(n + 1, self.rank))
+ nn = list(range(n)) + list(range(n + 1, self.rank))
temp = self._randr(
sop((F[n, :] - dot(E[n, nn], self.H[nn, :])).T, E[
n, n] + np.finfo(E.dtype).eps, div),
@@ -235,7 +235,7 @@ class Bd(nmf_std.Nmf_std):
if not sp.isspmatrix(self.H):
self.H[n, :] = temp.T
else:
- for j in xrange(self.H.shape[1]):
+ for j in range(self.H.shape[1]):
self.H[n, j] = temp[j]
def _randr(self, m, s, l):
diff --git a/nimfa/methods/factorization/bmf.py b/nimfa/methods/factorization/bmf.py
index 5a73894..ca9e742 100644
--- a/nimfa/methods/factorization/bmf.py
+++ b/nimfa/methods/factorization/bmf.py
@@ -98,7 +98,7 @@ class Bmf(nmf_std.Nmf_std):
"""
self._lambda_w = 1. / self.max_iter if self.max_iter else 1. / 10
self._lambda_h = self._lambda_w
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
self.normalize()
diff --git a/nimfa/methods/factorization/icm.py b/nimfa/methods/factorization/icm.py
index 7552955..bc79a98 100644
--- a/nimfa/methods/factorization/icm.py
+++ b/nimfa/methods/factorization/icm.py
@@ -80,7 +80,7 @@ class Icm(nmf_std.Nmf_std):
"""
self.v = multiply(self.V, self.V).sum() / 2.
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
p_obj = c_obj = sys.float_info.max
@@ -172,15 +172,15 @@ class Icm(nmf_std.Nmf_std):
# update basis matrix
C = dot(self.H, self.H.T)
D = dot(self.V, self.H.T)
- for _ in xrange(self.iiter):
- for n in xrange(self.rank):
- nn = list(xrange(n)) + list(xrange(n + 1, self.rank))
+ for _ in range(self.iiter):
+ for n in range(self.rank):
+ nn = list(range(n)) + list(range(n + 1, self.rank))
temp = max(
sop(D[:, n] - dot(self.W[:, nn], C[nn, n]) - self.sigma * self.alpha[:, n], C[n, n] + np.finfo(C.dtype).eps, div), 0.)
if not sp.isspmatrix(self.W):
self.W[:, n] = temp
else:
- for i in xrange(self.W.shape[0]):
+ for i in range(self.W.shape[0]):
self.W[i, n] = temp[i, 0]
# 0/1 values special handling
#l = np.logical_or((self.W == 0).all(0), (self.W == 1).all(0))
@@ -193,15 +193,15 @@ class Icm(nmf_std.Nmf_std):
# update mixture matrix
E = dot(self.W.T, self.W)
F = dot(self.W.T, self.V)
- for _ in xrange(self.iiter):
- for n in xrange(self.rank):
- nn = list(xrange(n)) + list(xrange(n + 1, self.rank))
+ for _ in range(self.iiter):
+ for n in range(self.rank):
+ nn = list(range(n)) + list(range(n + 1, self.rank))
temp = max(
sop(F[n, :] - dot(E[n, nn], self.H[nn, :]) - self.sigma * self.beta[n, :], E[n, n] + np.finfo(E.dtype).eps, div), 0.)
if not sp.isspmatrix(self.H):
self.H[n, :] = temp
else:
- for i in xrange(self.H.shape[1]):
+ for i in range(self.H.shape[1]):
self.H[n, i] = temp[0, i]
# 0/1 values special handling
#l = np.logical_or((self.H == 0).all(1), (self.H == 1).all(1))
diff --git a/nimfa/methods/factorization/lfnmf.py b/nimfa/methods/factorization/lfnmf.py
index 8e646e6..eaf10ec 100644
--- a/nimfa/methods/factorization/lfnmf.py
+++ b/nimfa/methods/factorization/lfnmf.py
@@ -69,7 +69,7 @@ class Lfnmf(nmf_std.Nmf_std):
Return fitted factorization model.
"""
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
self.Sw, self.Sb = np.mat(
@@ -148,25 +148,25 @@ class Lfnmf(nmf_std.Nmf_std):
C = len(c2m)
ksi = 1.
# update mixture matrix H
- for k in xrange(self.H.shape[0]):
- for l in xrange(self.H.shape[1]):
+ for k in range(self.H.shape[0]):
+ for l in range(self.H.shape[1]):
n_r = len(c2m[idxH[0, l]])
u_c = avgs[idxH[0, l]][k, 0]
t_1 = (2 * u_c - 1.) / (4 * ksi)
t_2 = (1. - 2 * u_c) ** 2 + 8 * ksi * self.H[k, l] * sum(self.W[i, k] * self.V[i, l] /
- (dot(self.W[i, :], self.H[:, l])[0, 0] + 1e-5) for i in xrange(self.W.shape[0]))
+ (dot(self.W[i, :], self.H[:, l])[0, 0] + 1e-5) for i in range(self.W.shape[0]))
self.H[k, l] = t_1 + sqrt(t_2) / (4 * ksi)
# update basis matrix W
- for i in xrange(self.W.shape[0]):
- for k in xrange(self.W.shape[1]):
+ for i in range(self.W.shape[0]):
+ for k in range(self.W.shape[1]):
w_1 = sum(self.H[k, j] * self.V[i, j] / (dot(self.W[i, :], self.H[:, j])[0, 0] + 1e-5)
- for j in xrange(self.V.shape[0]))
+ for j in range(self.V.shape[0]))
self.W[i, k] = self.W[i, k] * w_1 / self.H[k, :].sum()
W2 = repmat(self.W.sum(axis=0), self.V.shape[0], 1)
self.W = elop(self.W, W2, div)
# update within class scatter and between class
self.Sw = sum(sum(dot(self.H[:, c2m[i][j]] - avgs[i], (self.H[:, c2m[i][j]] - avgs[i]).T)
- for j in xrange(len(c2m[i]))) for i in c2m)
+ for j in range(len(c2m[i]))) for i in c2m)
avgs_t = np.mat(np.zeros((self.rank, 1)))
for k in avgs:
avgs_t += avgs[k]
@@ -177,7 +177,7 @@ class Lfnmf(nmf_std.Nmf_std):
"""Compute class membership and mean class value of encoding (mixture) matrix H."""
c2m = {}
avgs = {}
- for i in xrange(idxH.shape[1]):
+ for i in range(idxH.shape[1]):
# group columns of encoding matrix H by class membership
c2m.setdefault(idxH[0, i], [])
c2m[idxH[0, i]].append(i)
diff --git a/nimfa/methods/factorization/lsnmf.py b/nimfa/methods/factorization/lsnmf.py
index 8059ff5..38e5f36 100644
--- a/nimfa/methods/factorization/lsnmf.py
+++ b/nimfa/methods/factorization/lsnmf.py
@@ -71,7 +71,7 @@ class Lsnmf(nmf_std.Nmf_std):
Return fitted factorization model.
"""
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
self.gW = dot(self.W, dot(self.H, self.H.T)) - dot(
@@ -192,13 +192,13 @@ class Lsnmf(nmf_std.Nmf_std):
# decrease condition smaller beta more aggressively reduces the step
# size, but may cause the step size alpha being too small
alpha = 1.
- for iter in xrange(self.sub_iter):
+ for iter in range(self.sub_iter):
grad = dot(WtW, H) - WtV
projgrad = norm(self.__extract(grad, H))
if projgrad < epsH:
break
# search for step size alpha
- for n_iter in xrange(self.inner_sub_iter):
+ for n_iter in range(self.inner_sub_iter):
Hn = max(H - alpha * grad, 0)
d = Hn - H
gradd = multiply(grad, d).sum()
@@ -281,11 +281,11 @@ class Lsnmf(nmf_std.Nmf_std):
r2 = r2[yt]
c2 = c2[yt]
- idx1 = zip(r1, c1)
- idx2 = zip(r2, c2)
+ idx1 = list(zip(r1, c1))
+ idx2 = list(zip(r2, c2))
idxf = set(idx1).union(set(idx2))
- rf, cf = zip(*idxf)
+ rf, cf = list(zip(*idxf))
return X[rf, cf].T
else:
return X[np.logical_or(X < 0, Y > 0)].flatten().T
diff --git a/nimfa/methods/factorization/nmf.py b/nimfa/methods/factorization/nmf.py
index eadbd2f..5480915 100644
--- a/nimfa/methods/factorization/nmf.py
+++ b/nimfa/methods/factorization/nmf.py
@@ -82,7 +82,7 @@ class Nmf(nmf_std.Nmf_std):
Return fitted factorization model.
"""
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
p_obj = c_obj = sys.float_info.max
@@ -188,18 +188,18 @@ class Nmf(nmf_std.Nmf_std):
def euclidean_update(self):
"""Update basis and mixture matrix based on Euclidean distance multiplicative update rules."""
self.H = multiply(
- self.H, elop(dot(self.W.T, self.V), dot(self.W.T, dot(self.W, self.H)), div))
+ self.H, elop(dot(self.W.T, self.V), dot(self.W.T, dot(self.W, self.H)), floordiv))
self.W = multiply(
- self.W, elop(dot(self.V, self.H.T), dot(self.W, dot(self.H, self.H.T)), div))
+ self.W, elop(dot(self.V, self.H.T), dot(self.W, dot(self.H, self.H.T)), floordiv))
def divergence_update(self):
"""Update basis and mixture matrix based on divergence multiplicative update rules."""
H1 = repmat(self.W.sum(0).T, 1, self.V.shape[1])
self.H = multiply(
- self.H, elop(dot(self.W.T, elop(self.V, dot(self.W, self.H), div)), H1, div))
+ self.H, elop(dot(self.W.T, elop(self.V, dot(self.W, self.H), floordiv)), H1, floordiv))
W1 = repmat(self.H.sum(1).T, self.V.shape[0], 1)
self.W = multiply(
- self.W, elop(dot(elop(self.V, dot(self.W, self.H), div), self.H.T), W1, div))
+ self.W, elop(dot(elop(self.V, dot(self.W, self.H), floordiv), self.H.T), W1, floordiv))
def fro_objective(self):
"""Compute squared Frobenius norm of a target matrix and its NMF estimate."""
@@ -209,7 +209,7 @@ class Nmf(nmf_std.Nmf_std):
def div_objective(self):
"""Compute divergence of target matrix from its NMF estimate."""
Va = dot(self.W, self.H)
- return (multiply(self.V, sop(elop(self.V, Va, div), op=np.log)) - self.V + Va).sum()
+ return (multiply(self.V, sop(elop(self.V, Va, floordiv), op=np.log)) - self.V + Va).sum()
def conn_objective(self):
"""
diff --git a/nimfa/methods/factorization/nsnmf.py b/nimfa/methods/factorization/nsnmf.py
index 2ea8302..646f411 100644
--- a/nimfa/methods/factorization/nsnmf.py
+++ b/nimfa/methods/factorization/nsnmf.py
@@ -69,12 +69,12 @@ class Nsnmf(nmf_ns.Nmf_ns):
Return fitted factorization model.
"""
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
self.S = sop(
(1 - self.theta) * sp.spdiags(
- [1 for _ in xrange(
+ [1 for _ in range(
self.rank)], 0, self.rank, self.rank, 'csr'),
self.theta / self.rank, add)
p_obj = c_obj = sys.float_info.max
diff --git a/nimfa/methods/factorization/pmf.py b/nimfa/methods/factorization/pmf.py
index ff7d6db..37b34fe 100644
--- a/nimfa/methods/factorization/pmf.py
+++ b/nimfa/methods/factorization/pmf.py
@@ -48,7 +48,7 @@ class Pmf(nmf_std.Nmf_std):
Return fitted factorization model.
"""
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
self.W = elop(
@@ -58,7 +58,7 @@ class Pmf(nmf_std.Nmf_std):
self.v_factor = self.V.sum()
self.V_n = sop(self.V.copy(), self.v_factor, div)
self.P = sp.spdiags(
- [1. / self.rank for _ in xrange(self.rank)], 0, self.rank, self.rank, 'csr')
+ [1. / self.rank for _ in range(self.rank)], 0, self.rank, self.rank, 'csr')
self.sqrt_P = sop(self.P, s=None, op=np.sqrt)
p_obj = c_obj = sys.float_info.max
best_obj = c_obj if run == 0 else best_obj
@@ -143,7 +143,7 @@ class Pmf(nmf_std.Nmf_std):
algorithm. """
# E step
Qnorm = dot(dot(self.W, self.P), self.H)
- for k in xrange(self.rank):
+ for k in range(self.rank):
# E-step
Q = elop(self.P[k, k] * dot(self.W[:, k], self.H[k, :]), sop(
Qnorm, np.finfo(Qnorm.dtype).eps, add), div)
@@ -151,11 +151,11 @@ class Pmf(nmf_std.Nmf_std):
# M-step
dum = V_nQ.sum(axis=1)
s_dum = dum.sum()
- for i in xrange(self.W.shape[0]):
+ for i in range(self.W.shape[0]):
self.W[i, k] = dum[i, 0] / s_dum
dum = V_nQ.sum(axis=0)
s_dum = dum.sum()
- for i in xrange(self.H.shape[1]):
+ for i in range(self.H.shape[1]):
self.H[k, i] = dum[0, i] / s_dum
def objective(self):
diff --git a/nimfa/methods/factorization/pmfcc.py b/nimfa/methods/factorization/pmfcc.py
index 4294f32..3a1f765 100644
--- a/nimfa/methods/factorization/pmfcc.py
+++ b/nimfa/methods/factorization/pmfcc.py
@@ -59,7 +59,7 @@ class Pmfcc(smf.Smf):
self._Theta_p = multiply(self.Theta, sop(self.Theta, 0, operator.gt))
self._Theta_n = multiply(self.Theta, sop(self.Theta, 0, operator.lt)*(-1))
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
# [FWang2008]_; H = G.T, W = F (Table 2)
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
diff --git a/nimfa/methods/factorization/psmf.py b/nimfa/methods/factorization/psmf.py
index 0886b8e..1739816 100644
--- a/nimfa/methods/factorization/psmf.py
+++ b/nimfa/methods/factorization/psmf.py
@@ -92,7 +92,7 @@ class Psmf(nmf_std.Nmf_std):
if sp.isspmatrix(self.V):
self.V = self.V.todense()
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
# initialize P and Q distributions
# internal computation is done with numpy arrays as n-(n >
# 2)dimensionality is needed
@@ -155,10 +155,10 @@ class Psmf(nmf_std.Nmf_std):
"""Initialize the major cached parameter."""
outer_zeta = np.dot(self.zeta, self.zeta.T)
self.cross_terms = {}
- for n1 in xrange(self.N):
- for n2 in xrange(n1 + 1, self.N):
+ for n1 in range(self.N):
+ for n2 in range(n1 + 1, self.N):
self.cross_terms[n1, n2] = np.zeros((self.V.shape[0], 1))
- for c in xrange(self.rank):
+ for c in range(self.rank):
sigmat = np.tile((self.sigma[:, c, n2] * self.lamb[:, c]).reshape((self.lamb.shape[0], 1)), (1, self.zeta.shape[0]))
outer_zetat = np.tile(outer_zeta[c, :], (self.rho.shape[0], 1))
self.cross_terms[n1, n2] += (self.sigma[:, :, n1] * self.lamb *
@@ -195,7 +195,7 @@ class Psmf(nmf_std.Nmf_std):
self.prior = self.options.get('prior', self.rank)
try:
self.prior = [
- 1. / self.prior for _ in xrange(int(round(self.prior)))]
+ 1. / self.prior for _ in range(int(round(self.prior)))]
except TypeError:
self.prior = self.options['prior']
self.track_factor = self.options.get('track_factor', False)
@@ -216,17 +216,17 @@ class Psmf(nmf_std.Nmf_std):
"""Compute M-step and update psi."""
t_p1 = np.array(multiply(self.V, self.V).sum(axis=1))
self.psi = - \
- (np.tile(list(xrange(1, self.N)), (self.V.shape[0], 1)) * self.rho[:, 1:self.N]).sum(
+ (np.tile(list(range(1, self.N)), (self.V.shape[0], 1)) * self.rho[:, 1:self.N]).sum(
axis=1) * t_p1[:, 0]
self.psi = self.psi.reshape((self.V.shape[0], 1))
temp = np.zeros((self.V.shape[0], self.rank))
- for t in xrange(self.V.shape[1]):
+ for t in range(self.V.shape[1]):
temp += (np.tile(self.__arr(self.V[:, t]), (1, self.rank)) - self.lamb * np.tile(
self.zeta[:, t].T, (self.V.shape[0], 1))) ** 2 + self.lamb ** 2 * np.tile(self.phi.T, (self.V.shape[0], 1))
- for n in xrange(self.N):
+ for n in range(self.N):
self.psi += (self.rho[:, n:self.N].sum(axis = 1) * (self.sigma[:, :, n] * temp).sum(axis = 1)).reshape(self.psi.shape)
- for n in xrange(self.N):
- for nn in xrange(n + 1, self.N):
+ for n in range(self.N):
+ for nn in range(n + 1, self.N):
self.psi += (2 * self.rho[:, nn:self.N].sum(
axis=1) * self.cross_terms[n, nn].T[0]).reshape((self.V.shape[0], 1))
self.psi /= self.V.shape[1]
@@ -237,29 +237,29 @@ class Psmf(nmf_std.Nmf_std):
"""Compute M-step and update lambda."""
D = np.zeros((self.rank, 1))
V = np.zeros((self.V.shape[0], self.rank))
- for t in xrange(self.V.shape[1]):
+ for t in range(self.V.shape[1]):
D += self.zeta[:, t].reshape(
(self.zeta.shape[0], 1)) ** 2 + self.phi
V += dot(self.V[:, t], self.zeta[:, t].T)
temp = np.zeros((self.V.shape[0], self.rank))
- for n in xrange(self.N):
+ for n in range(self.N):
temp += np.tile((self.rho[:, n:self.N]).sum(axis = 1).reshape((self.rho.shape[0], 1)), (1, self.rank)) * self.sigma[:, :, n]
V *= temp
D = np.tile(D.T, (self.V.shape[0], 1)) * temp
# heuristic: weak Gaussian prior on lambda for ill-conditioning
# prevention
D += self.eps
- for g in xrange(self.V.shape[0]):
+ for g in range(self.V.shape[0]):
M = np.zeros((self.rank, self.rank))
- for n in xrange(self.N):
- for nn in xrange(n + 1, self.N):
+ for n in range(self.N):
+ for nn in range(n + 1, self.N):
M += np.dot(self.rho[g, nn:self.N].sum(axis = 0), np.dot(self.sigma[g, :, n].T, self.sigma[g,:, nn]))
M = (M + M.T) * np.dot(self.zeta, self.zeta.T)
self.lamb[g, :] = np.dot(V[g,:], np.linalg.inv(M + np.diag(D[g,:])))
# heuristic: negative mixing proportions not allowed
self.lamb[self.lamb < 0] = 0
self.W = sp.lil_matrix((self.V.shape[0], self.rank))
- for n in xrange(self.N):
+ for n in range(self.N):
locs = (self.r >= n).ravel().nonzero()[0]
if len(locs):
locs = sub2ind(
@@ -273,14 +273,14 @@ class Psmf(nmf_std.Nmf_std):
def _update_sigma(self):
"""Compute E-step and update sigma."""
self.cross_terms = np.zeros((self.V.shape[0], self.rank, self.N))
- for cc in xrange(self.rank):
+ for cc in range(self.rank):
t_c1 = np.tile(self.sigma[:, cc, :].reshape((self.sigma.shape[0], 1, self.sigma.shape[2])), (1, self.rank, 1))
t_c2 = np.tile(np.dot(self.zeta[cc, :], self.zeta.T), (self.V.shape[0], 1))
t_c3 = np.tile((self.lamb * np.tile(self.lamb[:, cc].reshape((self.lamb.shape[0], 1)), (1, self.rank)) * t_c2).reshape(
t_c2.shape[0], t_c2.shape[1], 1), (1, 1, self.N))
self.cross_terms += t_c1 * t_c3
self.sigma = np.zeros(self.sigma.shape)
- for t in xrange(self.V.shape[1]):
+ for t in range(self.V.shape[1]):
t_s1 = np.tile(self.__arr(self.V[:, t]), (1, self.rank)) - self.lamb * np.tile(
self.zeta[:, t].T, (self.V.shape[0], 1))
t_s2 = t_s1 ** 2 + self.lamb ** 2 * \
@@ -288,8 +288,8 @@ class Psmf(nmf_std.Nmf_std):
self.sigma -= 0.5 * \
np.tile((t_s2 / np.tile(self.psi, (1, self.rank))).reshape(
t_s2.shape[0], t_s2.shape[1], 1), (1, 1, self.N))
- for n in xrange(self.N):
- for nn in xrange(self.N):
+ for n in range(self.N):
+ for nn in range(self.N):
if nn != n:
t_s1 = (1e-50 + self.rho[:, max(n, nn):self.N]).sum(
axis=1) / (1e-50 + self.rho[:, n:self.N]).sum(axis=1)
@@ -306,9 +306,9 @@ class Psmf(nmf_std.Nmf_std):
"""Compute E-step and update zeta."""
M = np.zeros((self.rank, self.rank))
V = np.zeros((self.rank, self.V.shape[1]))
- for cc in xrange(self.rank):
- for n in xrange(self.N):
- for nn in xrange(n + 1, self.N):
+ for cc in range(self.rank):
+ for n in range(self.N):
+ for nn in range(n + 1, self.N):
t_m1 = np.tile(self.rho[:, nn:self.N].sum(axis=1).reshape(
(self.psi.shape[0], 1)) / self.psi, (1, self.rank))
t_m2 = np.tile((self.lamb[:, cc] * self.sigma[:, cc, nn]).reshape(
@@ -317,11 +317,11 @@ class Psmf(nmf_std.Nmf_std):
M[cc, :] += t_m.sum(axis = 0)
M += M.T
temp = np.zeros((self.V.shape[0], self.rank))
- for n in xrange(self.N):
+ for n in range(self.N):
temp += np.tile(self.rho[:, n:self.N].sum(axis = 1).reshape((self.rho.shape[0], 1)), (1, self.rank)) * self.sigma[:, :, n]
M += np.diag(
(self.lamb ** 2 / np.tile(self.psi, (1, self.rank)) * temp).sum(axis=0))
- for t in xrange(self.V.shape[1]):
+ for t in range(self.V.shape[1]):
t_v = np.tile(
self.__arr(self.V[:, t]) / self.psi, (1, self.rank)) * self.lamb * temp
V[:, t] = t_v.sum(axis=0)
@@ -333,7 +333,7 @@ class Psmf(nmf_std.Nmf_std):
def _update_phi(self):
"""Compute E-step and update phi."""
self.phi = np.ones(self.phi.shape)
- for n in xrange(self.N):
+ for n in range(self.N):
rho_tmp = np.tile(self.rho[:, n:self.N].sum(axis = 1).reshape(self.rho.shape[0], 1), (1, self.rank))
t_phi = np.tile(self.psi, (1, self.rank)) * self.sigma[:, :, n] * rho_tmp
self.phi += (self.lamb ** 2 / (t_phi + np.finfo(t_phi.dtype).eps)).sum(
@@ -348,22 +348,22 @@ class Psmf(nmf_std.Nmf_std):
self.sigma.shape[0], 1, self.sigma.shape[2]).cumsum(axis=2).transpose([0, 2, 1])
self.rho = self.rho.reshape((self.rho.shape[0], self.rho.shape[1]))
temp = np.zeros((self.V.shape[0], self.rank))
- for t in xrange(self.V.shape[1]):
+ for t in range(self.V.shape[1]):
t_dot = np.array(
np.dot(self.__arr(self.V[:, t]).reshape((self.V.shape[0], 1)), self.zeta[:, t].T.reshape((1, self.zeta.shape[0]))))
temp -= 2 * self.lamb * t_dot + self.lamb ** 2 * \
np.tile(
self.zeta[:, t].T ** 2 + self.phi.T, (self.V.shape[0], 1))
- for n in xrange(1, self.N):
+ for n in range(1, self.N):
self.rho[:, n] -= 0.5 / self.psi[:, 0] * (self.sigma[:, :, 1:n].sum(axis = 2) * temp).sum(axis = 1)
- for n1 in xrange(n + 1):
- for n2 in xrange(n1 + 1, n + 1):
+ for n1 in range(n + 1):
+ for n2 in range(n1 + 1, n + 1):
self.rho[:, n] -= (
1. / self.psi * self.cross_terms[n1, n2])[:, 0]
t_rho = np.exp(
self.rho - np.tile(np.amax(self.rho, 1).reshape((self.rho.shape[0], 1)), (1, self.N)))
self.rho = np.tile((self.prior / self.rank) ** list(
- xrange(1, self.N + 1)), (self.V.shape[0], 1)) * t_rho
+ range(1, self.N + 1)), (self.V.shape[0], 1)) * t_rho
self.rho = self.rho / \
np.tile(self.rho.sum(axis=1).reshape(
(self.rho.shape[0], 1)), (1, self.N))
diff --git a/nimfa/methods/factorization/snmf.py b/nimfa/methods/factorization/snmf.py
index 1624796..f229016 100644
--- a/nimfa/methods/factorization/snmf.py
+++ b/nimfa/methods/factorization/snmf.py
@@ -85,7 +85,7 @@ class Snmf(nmf_std.Nmf_std):
if self.version == 'l':
self.V = self.V.T
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
if sp.isspmatrix(self.W):
@@ -298,8 +298,8 @@ class Snmf(nmf_std.Nmf_std):
# obtain the initial feasible solution and corresponding passive set
K = self.__spcssls(CtC, CtA)
p_set = sop(K, 0, ge).tolil()
- for i in xrange(K.shape[0]):
- for j in xrange(K.shape[1]):
+ for i in range(K.shape[0]):
+ for j in range(K.shape[1]):
if not p_set[i, j]:
K[i, j] = 0.
D = K.copy()
@@ -321,8 +321,8 @@ class Snmf(nmf_std.Nmf_std):
# find indices of negative variables in passive set
tmp = sop(K[:, h_set], 0, le).tolil()
tmp_f = sp.lil_matrix(K.shape, dtype='bool')
- for i in xrange(K.shape[0]):
- for j in xrange(len(h_set)):
+ for i in range(K.shape[0]):
+ for j in range(len(h_set)):
if p_set[i, h_set[j]] and tmp[i, h_set[j]]:
tmp_f[i, h_set[j]] = True
idx_f = find(tmp_f[:, h_set])
@@ -338,7 +338,7 @@ class Snmf(nmf_std.Nmf_std):
l_1n = i_f
l_2n = [h_set[e] for e in j_f]
t_d = D[l_1n, l_2n] / (D[l_1n, l_2n] - K[l_1n, l_2n])
- for i in xrange(len(i_f)):
+ for i in range(len(i_f)):
alpha[i_f[i], j_f[i]] = t_d.todense().flatten()[0, i]
alpha_min, min_idx = argmin(alpha[:, :n_h_set], axis=0)
min_idx = min_idx.tolist()[0]
@@ -355,7 +355,7 @@ class Snmf(nmf_std.Nmf_std):
# optimality
W[:, f_set] = CtA[:, f_set] - dot(CtC, K[:, f_set])
tmp = sp.lil_matrix(p_set.shape, dtype='bool')
- for i in xrange(p_set.shape[0]):
+ for i in range(p_set.shape[0]):
for j in f_set:
if not p_set[i, j]:
tmp[i, j] = True
@@ -367,7 +367,7 @@ class Snmf(nmf_std.Nmf_std):
# for non-optimal solutions, add the appropriate variable to Pset
if len(f_set) > 0:
tmp = sp.lil_matrix(p_set.shape, dtype='bool')
- for i in xrange(p_set.shape[0]):
+ for i in range(p_set.shape[0]):
for j in f_set:
if not p_set[i, j]:
tmp[i, j] = True
@@ -389,25 +389,25 @@ class Snmf(nmf_std.Nmf_std):
K = sp.lil_matrix(CtA.shape)
if p_set == None or p_set.size == 0 or all(p_set):
# equivalent if CtC is square matrix
- for k in xrange(CtA.shape[1]):
+ for k in range(CtA.shape[1]):
ls = sp.linalg.gmres(CtC, CtA[:, k].toarray())[0]
K[:, k] = sp.lil_matrix(np.mat(ls).T)
# K = dot(np.linalg.pinv(CtC), CtA)
else:
l_var, p_rhs = p_set.shape
coded_p_set = dot(
- sp.lil_matrix(np.mat(2 ** np.array(range(l_var - 1, -1, -1)))), p_set)
+ sp.lil_matrix(np.mat(2 ** np.array(list(range(l_var - 1, -1, -1))))), p_set)
sorted_p_set, sorted_idx_set = sort(coded_p_set.todense())
breaks = diff(np.mat(sorted_p_set))
break_idx = [-1] + find(np.mat(breaks)) + [p_rhs]
- for k in xrange(len(break_idx) - 1):
+ for k in range(len(break_idx) - 1):
cols2solve = sorted_idx_set[
break_idx[k] + 1: break_idx[k + 1] + 1]
vars = p_set[:, sorted_idx_set[break_idx[k] + 1]]
- vars = [i for i in xrange(vars.shape[0]) if vars[i, 0]]
+ vars = [i for i in range(vars.shape[0]) if vars[i, 0]]
tmp_ls = CtA[:, cols2solve][vars, :]
sol = sp.lil_matrix(K.shape)
- for k in xrange(tmp_ls.shape[1]):
+ for k in range(tmp_ls.shape[1]):
ls = sp.linalg.gmres(CtC[:, vars][vars, :], tmp_ls[:, k].toarray())[0]
sol[:, k] = sp.lil_matrix(np.mat(ls).T)
i = 0
@@ -487,7 +487,7 @@ class Snmf(nmf_std.Nmf_std):
l_1n = i_f
l_2n = [h_set[e] for e in j_f]
t_d = D[l_1n, l_2n] / (D[l_1n, l_2n] - K[l_1n, l_2n])
- for i in xrange(len(i_f)):
+ for i in range(len(i_f)):
alpha[i_f[i], j_f[i]] = t_d.flatten()[0, i]
alpha_min, min_idx = argmin(alpha[:, :n_h_set], axis=0)
min_idx = min_idx.tolist()[0]
@@ -531,15 +531,15 @@ class Snmf(nmf_std.Nmf_std):
else:
l_var, p_rhs = p_set.shape
coded_p_set = dot(
- np.mat(2 ** np.array(range(l_var - 1, -1, -1))), p_set)
+ np.mat(2 ** np.array(list(range(l_var - 1, -1, -1)))), p_set)
sorted_p_set, sorted_idx_set = sort(coded_p_set)
breaks = diff(np.mat(sorted_p_set))
break_idx = [-1] + find(np.mat(breaks)) + [p_rhs]
- for k in xrange(len(break_idx) - 1):
+ for k in range(len(break_idx) - 1):
cols2solve = sorted_idx_set[
break_idx[k] + 1: break_idx[k + 1] + 1]
vars = p_set[:, sorted_idx_set[break_idx[k] + 1]]
- vars = [i for i in xrange(vars.shape[0]) if vars[i, 0]]
+ vars = [i for i in range(vars.shape[0]) if vars[i, 0]]
if vars != [] and cols2solve != []:
A = CtC[:, vars][vars, :]
B = CtA[:, cols2solve][vars,:]
diff --git a/nimfa/methods/factorization/snmnmf.py b/nimfa/methods/factorization/snmnmf.py
index 724fed3..5fd21e0 100644
--- a/nimfa/methods/factorization/snmnmf.py
+++ b/nimfa/methods/factorization/snmnmf.py
@@ -108,7 +108,7 @@ class Snmnmf(nmf_mm.Nmf_mm):
raise utils.MFError(
"Input matrices should have the same number of rows.")
- for run in xrange(self.n_run):
+ for run in range(self.n_run):
self.options.update({'idx': 0})
self.W, self.H = self.seed.initialize(
self.V, self.rank, self.options)
diff --git a/nimfa/methods/seeding/__init__.py b/nimfa/methods/seeding/__init__.py
index 7d51ff8..a119296 100644
--- a/nimfa/methods/seeding/__init__.py
+++ b/nimfa/methods/seeding/__init__.py
@@ -4,11 +4,11 @@
"""
-import nndsvd
-import random
-import fixed
-import random_c
-import random_vcol
+from . import nndsvd
+from . import random
+from . import fixed
+from . import random_c
+from . import random_vcol
methods = {"random": random.Random,
"fixed": fixed.Fixed,
diff --git a/nimfa/methods/seeding/fixed.py b/nimfa/methods/seeding/fixed.py
index a5decf0..44bb095 100644
--- a/nimfa/methods/seeding/fixed.py
+++ b/nimfa/methods/seeding/fixed.py
@@ -18,7 +18,7 @@ class Fixed(object):
def _set_fixed(self, **factors):
"""Set initial factorization."""
- for k in factors.keys():
+ for k in list(factors.keys()):
if factors[k] != None:
factors[k] = np.matrix(factors[k]) if not sp.isspmatrix(
factors[k]) else factors[k].copy()
diff --git a/nimfa/methods/seeding/nndsvd.py b/nimfa/methods/seeding/nndsvd.py
index 9ce8647..bce0205 100644
--- a/nimfa/methods/seeding/nndsvd.py
+++ b/nimfa/methods/seeding/nndsvd.py
@@ -83,7 +83,7 @@ class Nndsvd(object):
self.W[:, 0] = sqrt(S[0]) * abs(U[:, 0])
self.H[0, :] = sqrt(S[0]) * abs(E[:, 0].T)
# second svd for the other factors
- for i in xrange(1, self.rank):
+ for i in range(1, self.rank):
uu = U[:, i]
vv = E[:, i]
uup = self._pos(uu)
@@ -143,8 +143,8 @@ class Nndsvd(object):
# scipy.sparse.linalg ARPACK does not allow computation of rank(V) eigenvectors
# fill the missing columns/rows with random values
prng = np.random.RandomState()
- S = [S[i, i] for i in xrange(np.min([S.shape[0], S.shape[1]]))]
- S += [prng.rand() for _ in xrange(self.rank - len(S))]
+ S = [S[i, i] for i in range(np.min([S.shape[0], S.shape[1]]))]
+ S += [prng.rand() for _ in range(self.rank - len(S))]
U = U.tolil()
E = E.tolil()
temp_U = sp.lil_matrix((V.shape[0], min(V.shape[0], V.shape[1])))
@@ -163,7 +163,7 @@ class Nndsvd(object):
eps = np.finfo(V.data.dtype).eps if not 'int' in str(
V.data.dtype) else 0
# second svd for the other factors
- for i in xrange(1, self.rank):
+ for i in range(1, self.rank):
uu = U[:, i]
vv = E[:, i]
uup = self._pos(uu)
diff --git a/nimfa/methods/seeding/random_c.py b/nimfa/methods/seeding/random_c.py
index c6bee9d..d39e912 100644
--- a/nimfa/methods/seeding/random_c.py
+++ b/nimfa/methods/seeding/random_c.py
@@ -69,19 +69,19 @@ class Random_c(object):
self.W = sp.lil_matrix((V.shape[0], self.rank))
self.H = sp.lil_matrix((self.rank, V.shape[1]))
top_c = sorted(enumerate([norm(V[:, i], 2)
- for i in xrange(V.shape[1])]), key=itemgetter(1), reverse=True)[:self.l_c]
+ for i in range(V.shape[1])]), key=itemgetter(1), reverse=True)[:self.l_c]
top_r = sorted(
- enumerate([norm(V[i, :], 2) for i in xrange(V.shape[0])]), key=itemgetter(1), reverse=True)[:self.l_r]
+ enumerate([norm(V[i, :], 2) for i in range(V.shape[0])]), key=itemgetter(1), reverse=True)[:self.l_r]
else:
self.W = np.mat(np.zeros((V.shape[0], self.rank)))
self.H = np.mat(np.zeros((self.rank, V.shape[1])))
top_c = sorted(enumerate([norm(V[:, i], 2)
- for i in xrange(V.shape[1])]), key=itemgetter(1), reverse=True)[:self.l_c]
+ for i in range(V.shape[1])]), key=itemgetter(1), reverse=True)[:self.l_c]
top_r = sorted(
- enumerate([norm(V[i, :], 2) for i in xrange(V.shape[0])]), key=itemgetter(1), reverse=True)[:self.l_r]
+ enumerate([norm(V[i, :], 2) for i in range(V.shape[0])]), key=itemgetter(1), reverse=True)[:self.l_r]
top_c = np.mat(zip(*top_c)[0])
top_r = np.mat(zip(*top_r)[0])
- for i in xrange(self.rank):
+ for i in range(self.rank):
self.W[:, i] = V[
:, top_c[0, self.prng.randint(low=0, high=self.l_c, size=self.p_c)].tolist()[0]].mean(axis=1)
self.H[i, :] = V[
diff --git a/nimfa/methods/seeding/random_vcol.py b/nimfa/methods/seeding/random_vcol.py
index 18b6daf..3687100 100644
--- a/nimfa/methods/seeding/random_vcol.py
+++ b/nimfa/methods/seeding/random_vcol.py
@@ -62,7 +62,7 @@ class Random_vcol(object):
else:
self.W = np.mat(np.zeros((V.shape[0], self.rank)))
self.H = np.mat(np.zeros((self.rank, V.shape[1])))
- for i in xrange(self.rank):
+ for i in range(self.rank):
self.W[:, i] = V[:, self.prng.randint(
low=0, high=V.shape[1], size=self.p_c)].mean(axis=1)
self.H[i, :] = V[
diff --git a/nimfa/mf_run.py b/nimfa/mf_run.py
index b0049d1..8506aac 100644
--- a/nimfa/mf_run.py
+++ b/nimfa/mf_run.py
@@ -26,10 +26,10 @@
"""
-from utils import *
+from .utils import *
-import examples
-import methods
+from . import examples
+from . import methods
l_factorization = methods.list_mf_methods()
l_seed = methods.list_seeding_methods()
diff --git a/nimfa/models/__init__.py b/nimfa/models/__init__.py
index 64bdcca..22da91c 100644
--- a/nimfa/models/__init__.py
+++ b/nimfa/models/__init__.py
@@ -16,10 +16,10 @@
"""
-import nmf
-import nmf_std
-import nmf_ns
-import nmf_mm
-import smf
-import mf_track
-import mf_fit
+from . import nmf
+from . import nmf_std
+from . import nmf_ns
+from . import nmf_mm
+from . import smf
+from . import mf_track
+from . import mf_fit
diff --git a/nimfa/models/nmf.py b/nimfa/models/nmf.py
index d738f12..7642e81 100644
--- a/nimfa/models/nmf.py
+++ b/nimfa/models/nmf.py
@@ -219,7 +219,7 @@ class Nmf(object):
cons = V.__class__((V.shape[1], V.shape[1]), dtype=V.dtype)
else:
cons = np.mat(np.zeros((V.shape[1], V.shape[1])))
- for i in xrange(self.n_run):
+ for i in range(self.n_run):
cons += self.connectivity(
H=self.tracker.get_factor(i).H, idx=idx)
return sop(cons, self.n_run, div)
@@ -259,9 +259,9 @@ class Nmf(object):
n = V.shape[1]
mbs = self.predict(what="samples", prob=False, idx=idx)
dmbs, dmembership = {}, {}
- [dmbs.setdefault(mbs[i], set()).add(i) for i in xrange(len(mbs))]
+ [dmbs.setdefault(mbs[i], set()).add(i) for i in range(len(mbs))]
[dmembership.setdefault(membership[i], set()).add(i)
- for i in xrange(len(membership))]
+ for i in range(len(membership))]
return -1. / (n * log(len(dmembership), 2)) * sum(sum(len(dmbs[k].intersection(dmembership[j])) *
log(len(dmbs[k].intersection(dmembership[j])) / float(len(dmbs[k])), 2) for j in dmembership) for k in dmbs)
@@ -294,7 +294,7 @@ class Nmf(object):
if not prob:
return idxX
sums = X.sum(axis=0)
- prob = [e / sums[0, s] for e, s in zip(eX, list(xrange(X.shape[1])))]
+ prob = [e / sums[0, s] for e, s in zip(eX, list(range(X.shape[1])))]
return idxX, prob
def evar(self, idx=None):
@@ -333,9 +333,9 @@ class Nmf(object):
"""Return probability that the i-th feature contributes to the basis q."""
return W[i, q] / (W[i, :].sum() + np.finfo(W.dtype).eps)
res = []
- for f in xrange(W.shape[0]):
+ for f in range(W.shape[0]):
res.append(1. + 1. / log(W.shape[1], 2) * sum(
- prob(f, q) * log(prob(f, q) + np.finfo(W.dtype).eps, 2) for q in xrange(W.shape[1])))
+ prob(f, q) * log(prob(f, q) + np.finfo(W.dtype).eps, 2) for q in range(W.shape[1])))
return res
def select_features(self, idx=None):
@@ -357,7 +357,7 @@ class Nmf(object):
scores = self.score_features(idx=idx)
u = np.median(scores)
s = np.median(abs(scores - u))
- res = [i for i in xrange(len(scores)) if scores[i] > u + 3. * s]
+ res = [i for i in range(len(scores)) if scores[i] > u + 3. * s]
W = self.basis()
m = np.median(W.toarray() if sp.isspmatrix(W) else W.tolist())
return [i for i in res if np.max(W[i, :].toarray() if sp.isspmatrix(W) else W[i,:]) > m]
@@ -384,9 +384,9 @@ class Nmf(object):
n = V.shape[1]
mbs = self.predict(what="samples", prob=False, idx=idx)
dmbs, dmembership = {}, {}
- [dmbs.setdefault(mbs[i], set()).add(i) for i in xrange(len(mbs))]
+ [dmbs.setdefault(mbs[i], set()).add(i) for i in range(len(mbs))]
[dmembership.setdefault(membership[i], set()).add(i)
- for i in xrange(len(membership))]
+ for i in range(len(membership))]
return 1. / n * sum(max(len(dmbs[k].intersection(dmembership[j])) for j in dmembership) for k in dmbs)
def rss(self, idx=None):
@@ -431,7 +431,7 @@ class Nmf(object):
return x1 / x2
W = self.basis()
H = self.coef(idx)
- return np.mean([sparseness(W[:, i]) for i in xrange(W.shape[1])]), np.mean([sparseness(H[:, i]) for i in xrange(H.shape[1])])
+ return np.mean([sparseness(W[:, i]) for i in range(W.shape[1])]), np.mean([sparseness(H[:, i]) for i in range(H.shape[1])])
def coph_cor(self, idx=None):
"""
@@ -453,8 +453,8 @@ class Nmf(object):
"""
A = self.consensus(idx=idx)
# upper diagonal elements of consensus
- avec = np.array([A[i, j] for i in xrange(A.shape[0] - 1)
- for j in xrange(i + 1, A.shape[1])])
+ avec = np.array([A[i, j] for i in range(A.shape[0] - 1)
+ for j in range(i + 1, A.shape[1])])
# consensus entries are similarities, conversion to distances
Y = 1 - avec
Z = linkage(Y, method='average')
@@ -480,9 +480,9 @@ class Nmf(object):
:type idx: None or `str` with values 'coef' or 'coef1' (`int` value of 0 or 1, respectively)
"""
C = self.consensus(idx=idx)
- return sum(sum(4 * (C[i, j] - 0.5) ** 2 for j in xrange(C.shape[1])) for i in xrange(C.shape[0]))
+ return sum(sum(4 * (C[i, j] - 0.5) ** 2 for j in range(C.shape[1])) for i in range(C.shape[0]))
- def estimate_rank(self, range=xrange(30, 51), n_run=10, idx=0, what='all'):
+ def estimate_rank(self, range=list(range(30, 51)), n_run=10, idx=0, what='all'):
"""
Choosing factorization parameters carefully is vital for success of a factorization. However, the most critical parameter
is factorization rank. This method tries different values for ranks, performs factorizations, computes some quality
diff --git a/nimfa/models/nmf_mm.py b/nimfa/models/nmf_mm.py
index 8fc0aef..fede0b7 100644
--- a/nimfa/models/nmf_mm.py
+++ b/nimfa/models/nmf_mm.py
@@ -5,7 +5,7 @@
##########################
"""
-from nmf import *
+from .nmf import *
class Nmf_mm(Nmf):
diff --git a/nimfa/models/nmf_ns.py b/nimfa/models/nmf_ns.py
index 3094961..8e34a63 100644
--- a/nimfa/models/nmf_ns.py
+++ b/nimfa/models/nmf_ns.py
@@ -5,7 +5,7 @@
##########################
"""
-from nmf import *
+from .nmf import *
class Nmf_ns(Nmf):
diff --git a/nimfa/models/nmf_std.py b/nimfa/models/nmf_std.py
index 033b94d..49df4f4 100644
--- a/nimfa/models/nmf_std.py
+++ b/nimfa/models/nmf_std.py
@@ -5,7 +5,7 @@
############################
"""
-from nmf import *
+from .nmf import *
class Nmf_std(Nmf):
diff --git a/nimfa/utils/__init__.py b/nimfa/utils/__init__.py
index 43a4f8b..c5e9dec 100644
--- a/nimfa/utils/__init__.py
+++ b/nimfa/utils/__init__.py
@@ -5,5 +5,5 @@
"""
-import linalg
-import utils
+from . import linalg
+from . import utils
diff --git a/nimfa/utils/linalg.py b/nimfa/utils/linalg.py
index 51ca7b8..bbe98f7 100644
--- a/nimfa/utils/linalg.py
+++ b/nimfa/utils/linalg.py
@@ -14,8 +14,8 @@ import scipy
import scipy.sparse as sp
import scipy.sparse.linalg as sla
import numpy.linalg as nla
-from operator import mul, div, eq, ne, add, ge, le, itemgetter
-from itertools import izip
+from operator import mul, floordiv, eq, ne, add, ge, le, itemgetter
+
from math import sqrt, log, isnan, ceil
from scipy.cluster.hierarchy import linkage, cophenet
from scipy.special import erfc, erfcinv
@@ -37,7 +37,7 @@ def diff(X):
assert 1 in X.shape, "sX should be a vector."
assert not sp.isspmatrix(X), "X is sparse matrix."
X = X.flatten()
- return [X[0, j + 1] - X[0, j] for j in xrange(X.shape[1] - 1)]
+ return [X[0, j + 1] - X[0, j] for j in range(X.shape[1] - 1)]
def sub2ind(shape, row_sub, col_sub):
@@ -68,7 +68,7 @@ def trace(X):
"""
assert X.shape[0] == X.shape[1], "X should be square matrix."
if sp.isspmatrix(X):
- return sum(X[i, i] for i in xrange(X.shape[0]))
+ return sum(X[i, i] for i in range(X.shape[0]))
else:
return np.trace(np.mat(X))
@@ -89,7 +89,7 @@ def any(X, axis=None):
assert axis == 0 or axis == 1 or axis == None, "Incorrect axis number."
if axis is None:
return len(X.data) != X.shape[0] * X.shape[1]
- res = [0 for _ in xrange(X.shape[1 - axis])]
+ res = [0 for _ in range(X.shape[1 - axis])]
def _caxis(now, row, col):
res[col] += 1
@@ -127,7 +127,7 @@ def all(X, axis=None):
assert axis == 0 or axis == 1 or axis == None, "Incorrect axis number."
if axis is None:
return len(X.data) == X.shape[0] * X.shape[1]
- res = [0 for _ in xrange(X.shape[1 - axis])]
+ res = [0 for _ in range(X.shape[1 - axis])]
def _caxis(now, row, col):
res[col] += 1
@@ -170,7 +170,7 @@ def find(X):
now += 1
return res
else:
- return [j * X.shape[0] + i for i in xrange(X.shape[0]) for j in xrange(X.shape[1]) if X[i, j]]
+ return [j * X.shape[0] + i for i in range(X.shape[0]) for j in range(X.shape[1]) if X[i, j]]
def negative(X):
@@ -200,7 +200,7 @@ def sort(X):
"""
assert 1 in X.shape, "X should be vector."
X = X.flatten().tolist()[0]
- return sorted(X), sorted(range(len(X)), key=X.__getitem__)
+ return sorted(X), sorted(list(range(len(X))), key=X.__getitem__)
def std(X, axis=None, ddof=0):
@@ -227,9 +227,9 @@ def std(X, axis=None, ddof=0):
no = X.shape[0] * X.shape[1]
return sqrt(1. / (no - ddof) * sum((x - mean) ** 2 for x in X.data) + (no - len(X.data) * mean ** 2))
if axis == 0:
- return np.mat([np.std(X[:, i].toarray(), axis, ddof) for i in xrange(X.shape[1])])
+ return np.mat([np.std(X[:, i].toarray(), axis, ddof) for i in range(X.shape[1])])
if axis == 1:
- return np.mat([np.std(X[i, :].toarray(), axis, ddof) for i in xrange(X.shape[0])]).T
+ return np.mat([np.std(X[i, :].toarray(), axis, ddof) for i in range(X.shape[0])]).T
else:
return np.std(X, axis=axis, ddof=ddof)
@@ -250,7 +250,7 @@ def argmax(X, axis=None):
X = X.tocsr()
assert axis == 0 or axis == 1 or axis == None, "Incorrect axis number."
res = [[float('-inf'), 0]
- for _ in xrange(X.shape[1 - axis])] if axis is not None else [float('-inf'), 0]
+ for _ in range(X.shape[1 - axis])] if axis is not None else [float('-inf'), 0]
def _caxis(row, col):
if X[row, col] > res[col][0]:
@@ -265,15 +265,15 @@ def argmax(X, axis=None):
res[0] = X[row, col]
res[1] = row * X.shape[0] + col
check = _caxis if axis == 0 else _raxis if axis == 1 else _naxis
- [check(row, col) for row in xrange(X.shape[0])
- for col in xrange(X.shape[1])]
+ [check(row, col) for row in range(X.shape[0])
+ for col in range(X.shape[1])]
if axis == None:
return res
elif axis == 0:
- t = zip(*res)
+ t = list(zip(*res))
return list(t[0]), np.mat(t[1])
else:
- t = zip(*res)
+ t = list(zip(*res))
return list(t[0]), np.mat(t[1]).T
else:
idxX = np.asmatrix(X).argmax(axis)
@@ -281,10 +281,10 @@ def argmax(X, axis=None):
eX = X[idxX / X.shape[1], idxX % X.shape[1]]
elif axis == 0:
eX = [X[idxX[0, idx], col]
- for idx, col in izip(xrange(X.shape[1]), xrange(X.shape[1]))]
+ for idx, col in zip(list(range(X.shape[1])), list(range(X.shape[1])))]
else:
eX = [X[row, idxX[idx, 0]]
- for row, idx in izip(xrange(X.shape[0]), xrange(X.shape[0]))]
+ for row, idx in zip(list(range(X.shape[0])), list(range(X.shape[0])))]
return eX, idxX
@@ -304,7 +304,7 @@ def argmin(X, axis=None):
X = X.tocsr()
assert axis == 0 or axis == 1 or axis == None, "Incorrect axis number."
res = [[float('inf'), 0]
- for _ in xrange(X.shape[1 - axis])] if axis is not None else [float('inf'), 0]
+ for _ in range(X.shape[1 - axis])] if axis is not None else [float('inf'), 0]
def _caxis(row, col):
if X[row, col] < res[col][0]:
@@ -319,15 +319,15 @@ def argmin(X, axis=None):
res[0] = X[row, col]
res[1] = row * X.shape[0] + col
check = _caxis if axis == 0 else _raxis if axis == 1 else _naxis
- [check(row, col) for row in xrange(X.shape[0])
- for col in xrange(X.shape[1])]
+ [check(row, col) for row in range(X.shape[0])
+ for col in range(X.shape[1])]
if axis == None:
return res
elif axis == 0:
- t = zip(*res)
+ t = list(zip(*res))
return list(t[0]), np.mat(t[1])
else:
- t = zip(*res)
+ t = list(zip(*res))
return list(t[0]), np.mat(t[1]).T
else:
idxX = np.asmatrix(X).argmin(axis)
@@ -335,10 +335,10 @@ def argmin(X, axis=None):
eX = X[idxX / X.shape[1], idxX % X.shape[1]]
elif axis == 0:
eX = [X[idxX[0, idx], col]
- for idx, col in izip(xrange(X.shape[1]), xrange(X.shape[1]))]
+ for idx, col in zip(list(range(X.shape[1])), list(range(X.shape[1])))]
else:
eX = [X[row, idxX[idx, 0]]
- for row, idx in izip(xrange(X.shape[0]), xrange(X.shape[0]))]
+ for row, idx in zip(list(range(X.shape[0])), list(range(X.shape[0])))]
return eX, idxX
@@ -353,7 +353,7 @@ def repmat(X, m, n):
:type m,n: `int`
"""
if sp.isspmatrix(X):
- return sp.hstack([sp.vstack([X for _ in xrange(m)], format=X.format) for _ in xrange(n)], format=X.format)
+ return sp.hstack([sp.vstack([X for _ in range(m)], format=X.format) for _ in range(n)], format=X.format)
else:
return np.tile(np.asmatrix(X), (m, n))
@@ -411,7 +411,7 @@ def _svd_right(X):
# http://docs.scipy.org/doc/scipy/reference/release.0.9.0.html#scipy-sparse
try:
val, u_vec = sla.eigsh(XXt, k=X.shape[0] - 1)
- except sla.ArpackNoConvergence, err:
+ except sla.ArpackNoConvergence as err:
# If eigenvalue iteration fails to converge, partially
# converged results can be accessed
val = err.eigenvalues
@@ -454,7 +454,7 @@ def _svd_left(X):
# http://docs.scipy.org/doc/scipy/reference/release.0.9.0.html#scipy-sparse
try:
val, v_vec = sla.eigsh(XtX, k=X.shape[1] - 1)
- except sla.ArpackNoConvergence, err:
+ except sla.ArpackNoConvergence as err:
# If eigenvalue iteration fails to converge, partially
# converged results can be accessed
val = err.eigenvalues
@@ -689,7 +689,7 @@ def _op_matrix(X, Y, op):
# operation is not necessarily commutative
assert X.shape == Y.shape, "Matrices are not aligned."
eps = np.finfo(Y.dtype).eps if not 'int' in str(Y.dtype) else 0
- return np.mat([[op(X[i, j], Y[i, j] + eps) for j in xrange(X.shape[1])] for i in xrange(X.shape[0])])
+ return np.mat([[op(X[i, j], Y[i, j] + eps) for j in range(X.shape[1])] for i in range(X.shape[0])])
def inf_norm(X):
@@ -860,7 +860,7 @@ def choose(n, k):
if 0 <= k <= n:
ntok = 1
ktok = 1
- for t in xrange(1, min(k, n - k) + 1):
+ for t in range(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
--
2.0.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment