From 1ff60d03200764dc7ad652d3eb86d98e510a1729 Mon Sep 17 00:00:00 2001 From: Irene RF Date: Fri, 16 Jan 2026 11:48:16 +0000 Subject: [PATCH] Replaced missing deprecated "np.mat()" with "np.asmatrix()" I noticed several 'np.mat()' instances were still present in the package and limited fix #66 . As such, issue #65 was still present. With this update, I've ensured all instances of 'np.mat()' have been replaced to 'np.asmatrix()'. To do so, I've the following shell command: find * -type f -exec sed -i 's/np.mat(/np.asmatrix(/g' {} \; This should fix several broken functions, hopefully making Nimfa compatible with numpy v.2.0.0+. Resolves issue #65 , building on #66 . --- nimfa/examples/gene_func_prediction.py | 8 +++--- nimfa/examples/orl_images.py | 6 ++--- nimfa/examples/synthetic.py | 2 +- nimfa/methods/factorization/bd.py | 4 +-- nimfa/methods/factorization/icm.py | 4 +-- nimfa/methods/factorization/lfnmf.py | 8 +++--- nimfa/methods/factorization/nmf.py | 2 +- nimfa/methods/factorization/pmfcc.py | 2 +- nimfa/methods/factorization/psmf.py | 4 +-- nimfa/methods/factorization/sepnmf.py | 2 +- nimfa/methods/factorization/snmf.py | 36 +++++++++++++------------- nimfa/methods/factorization/snmnmf.py | 4 +-- nimfa/methods/seeding/nndsvd.py | 4 +-- nimfa/methods/seeding/random.py | 2 +- nimfa/methods/seeding/random_c.py | 8 +++--- nimfa/methods/seeding/random_vcol.py | 4 +-- nimfa/models/nmf.py | 4 +-- nimfa/utils/linalg.py | 32 +++++++++++------------ 18 files changed, 68 insertions(+), 68 deletions(-) diff --git a/nimfa/examples/gene_func_prediction.py b/nimfa/examples/gene_func_prediction.py index 3666588..2721234 100644 --- a/nimfa/examples/gene_func_prediction.py +++ b/nimfa/examples/gene_func_prediction.py @@ -162,8 +162,8 @@ class information exploiting properties of Functional Catalogue hierarchy. if line_type == "@DATA": section = 'd' 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)))) + attr_data = np.asmatrix(np.zeros((1e4, len(attr2idx)))) + class_data = np.asmatrix(np.zeros((1e4, len(class2idx)))) elif section == 'd': d, _, comment = line.strip().partition("%") values = d.split(",") @@ -287,10 +287,10 @@ def compute_correlations(train, test): # alternative, it is time consuming - can be used for partial evaluation """corrs = {} for i in xrange(test['W'].shape[0]): - corrs.setdefault(i, np.mat(np.zeros((train['W'].shape[0], 1)))) + corrs.setdefault(i, np.asmatrix(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, :])""" - return np.mat(corrs) + return np.asmatrix(corrs) def _corr(x, y): diff --git a/nimfa/examples/orl_images.py b/nimfa/examples/orl_images.py index b116eea..b10c267 100644 --- a/nimfa/examples/orl_images.py +++ b/nimfa/examples/orl_images.py @@ -159,7 +159,7 @@ def read(): im = open(join(dir + str(subject + 1), str(image + 1) + ".pgm")) # reduce the size of the image im = im.resize((46, 56)) - V[:, 10 * subject + image] = np.mat(np.asarray(im).flatten()).T + V[:, 10 * subject + image] = np.asmatrix(np.asarray(im).flatten()).T return V @@ -174,9 +174,9 @@ def preprocess(V): """ print("Data preprocessing") min_val = V.min(axis=0) - V = V - np.mat(np.ones((V.shape[0], 1))) * min_val + V = V - np.asmatrix(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) / 100. + V = (255. * V) / (np.asmatrix(np.ones((V.shape[0], 1))) * max_val) / 100. return V diff --git a/nimfa/examples/synthetic.py b/nimfa/examples/synthetic.py index fe11f61..6a96c64 100644 --- a/nimfa/examples/synthetic.py +++ b/nimfa/examples/synthetic.py @@ -115,7 +115,7 @@ def run_bd(V): rank = 10 bd = nimfa.Bd(V, seed="random_c", rank=rank, max_iter=12, alpha=np.zeros((V.shape[0], rank)), beta=np.zeros((rank, V.shape[1])), theta=.0, k=.0, sigma=1., skip=100, - stride=1, n_w=np.mat(np.zeros((rank, 1))), n_h=np.mat(np.zeros((rank, 1))), + stride=1, n_w=np.asmatrix(np.zeros((rank, 1))), n_h=np.asmatrix(np.zeros((rank, 1))), n_sigma=False) fit = bd() print_info(fit) diff --git a/nimfa/methods/factorization/bd.py b/nimfa/methods/factorization/bd.py index b8a1e61..f3caea1 100644 --- a/nimfa/methods/factorization/bd.py +++ b/nimfa/methods/factorization/bd.py @@ -187,10 +187,10 @@ def __init__(self, V, seed=None, W=None, H=None, rank=30, max_iter=30, nmf_std.Nmf_std.__init__(self, vars()) if self.alpha is None: self.alpha = sp.csr_matrix((self.V.shape[0], self.rank)) - self.alpha = self.alpha.tocsr() if sp.isspmatrix(self.alpha) else np.mat(self.alpha) + self.alpha = self.alpha.tocsr() if sp.isspmatrix(self.alpha) else np.asmatrix(self.alpha) if self.beta is None: self.beta = sp.csr_matrix((self.rank, self.V.shape[1])) - self.beta = self.beta.tocsr() if sp.isspmatrix(self.beta) else np.mat(self.beta) + self.beta = self.beta.tocsr() if sp.isspmatrix(self.beta) else np.asmatrix(self.beta) if self.n_w is None: self.n_w = np.zeros((self.rank, 1)) if self.n_h is None: diff --git a/nimfa/methods/factorization/icm.py b/nimfa/methods/factorization/icm.py index 89e6ab7..8abf6e4 100644 --- a/nimfa/methods/factorization/icm.py +++ b/nimfa/methods/factorization/icm.py @@ -162,10 +162,10 @@ def __init__(self, V, seed=None, W=None, H=None, H1=None, nmf_std.Nmf_std.__init__(self, vars()) if self.alpha is None: self.alpha = sp.rand(self.V.shape[0], self.rank, density=0.8, format='csr') - self.alpha= self.alpha.tocsr() if sp.isspmatrix(self.alpha) else np.mat(self.alpha) + self.alpha= self.alpha.tocsr() if sp.isspmatrix(self.alpha) else np.asmatrix(self.alpha) if self.beta is None: self.beta = sp.rand(self.rank, self.V.shape[1], density=0.8, format='csr') - self.beta = self.beta.tocsr() if sp.isspmatrix(self.beta) else np.mat(self.beta) + self.beta = self.beta.tocsr() if sp.isspmatrix(self.beta) else np.asmatrix(self.beta) self.tracker = mf_track.Mf_track() if self.track_factor and self.n_run > 1 \ or self.track_error else None diff --git a/nimfa/methods/factorization/lfnmf.py b/nimfa/methods/factorization/lfnmf.py index fb4a2a7..4530118 100644 --- a/nimfa/methods/factorization/lfnmf.py +++ b/nimfa/methods/factorization/lfnmf.py @@ -157,8 +157,8 @@ def factorize(self): 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( - np.zeros((1, 1))), np.mat(np.zeros((1, 1))) + self.Sw, self.Sb = np.asmatrix( + np.zeros((1, 1))), np.asmatrix(np.zeros((1, 1))) p_obj = c_obj = sys.float_info.max best_obj = c_obj if run == 0 else best_obj iter = 0 @@ -244,7 +244,7 @@ def update(self): # 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 range(len(c2m[i]))) for i in c2m) - avgs_t = np.mat(np.zeros((self.rank, 1))) + avgs_t = np.asmatrix(np.zeros((self.rank, 1))) for k in avgs: avgs_t += avgs[k] avgs_t /= len(avgs) @@ -259,7 +259,7 @@ def _encoding(self, idxH): c2m.setdefault(idxH[0, i], []) c2m[idxH[0, i]].append(i) # compute mean value of class idx in encoding matrix H - avgs.setdefault(idxH[0, i], np.mat(np.zeros((self.rank, 1)))) + avgs.setdefault(idxH[0, i], np.asmatrix(np.zeros((self.rank, 1)))) avgs[idxH[0, i]] += self.H[:, i] for k in avgs: avgs[k] /= len(c2m[k]) diff --git a/nimfa/methods/factorization/nmf.py b/nimfa/methods/factorization/nmf.py index 8c7d6cb..ca1ec98 100644 --- a/nimfa/methods/factorization/nmf.py +++ b/nimfa/methods/factorization/nmf.py @@ -303,7 +303,7 @@ def conn(self): cons = elop(mat1, mat2, eq) if not hasattr(self, 'consold'): self.cons = cons - self.consold = np.mat(np.logical_not(cons)) + self.consold = np.asmatrix(np.logical_not(cons)) else: self.consold = self.cons self.cons = cons diff --git a/nimfa/methods/factorization/pmfcc.py b/nimfa/methods/factorization/pmfcc.py index c5a7a07..e8aae10 100644 --- a/nimfa/methods/factorization/pmfcc.py +++ b/nimfa/methods/factorization/pmfcc.py @@ -133,7 +133,7 @@ def __init__(self, V, seed=None, W=None, H=None, rank=30, max_iter=30, self.aseeds = ["random", "fixed", "nndsvd", "random_c", "random_vcol"] smf.Smf.__init__(self, vars()) if not self.Theta: - self.Theta = np.mat(np.zeros((self.V.shape[1], self.V.shape[1]))) + self.Theta = np.asmatrix(np.zeros((self.V.shape[1], self.V.shape[1]))) self.tracker = mf_track.Mf_track() if self.track_factor and self.n_run > 1 \ or self.track_error else None diff --git a/nimfa/methods/factorization/psmf.py b/nimfa/methods/factorization/psmf.py index ef872eb..1fe7129 100644 --- a/nimfa/methods/factorization/psmf.py +++ b/nimfa/methods/factorization/psmf.py @@ -188,8 +188,8 @@ def factorize(self): self.H = self.V.__class__( (self.rank, self.V.shape[1]), dtype='d') else: - self.W = np.mat(np.zeros((self.V.shape[0], self.rank))) - self.H = np.mat(np.zeros((self.rank, self.V.shape[1]))) + self.W = np.asmatrix(np.zeros((self.V.shape[0], self.rank))) + self.H = np.asmatrix(np.zeros((self.rank, self.V.shape[1]))) self.s = np.zeros((self.V.shape[0], self.N), int) self.r = np.zeros((self.V.shape[0], 1), int) self.psi = np.array(std(self.V, axis=1, ddof=0)) diff --git a/nimfa/methods/factorization/sepnmf.py b/nimfa/methods/factorization/sepnmf.py index 4b8db0e..7274e91 100644 --- a/nimfa/methods/factorization/sepnmf.py +++ b/nimfa/methods/factorization/sepnmf.py @@ -318,7 +318,7 @@ def norm_axis(X, p=None, axis=None): if sp.isspmatrix(X): return sla.norm(X, ord=p, axis=axis) else: - n = nla.norm(np.mat(X), ord=p, axis=axis) + n = nla.norm(np.asmatrix(X), ord=p, axis=axis) if axis is None: return n else: diff --git a/nimfa/methods/factorization/snmf.py b/nimfa/methods/factorization/snmf.py index c30d29b..215a8f3 100644 --- a/nimfa/methods/factorization/snmf.py +++ b/nimfa/methods/factorization/snmf.py @@ -183,8 +183,8 @@ def factorize(self): if sp.isspmatrix(self.H): self.H = self.H.tolil() iter = 0 - self.idx_w_old = np.mat(np.zeros((self.V.shape[0], 1))) - self.idx_h_old = np.mat(np.zeros((1, self.V.shape[1]))) + self.idx_w_old = np.asmatrix(np.zeros((self.V.shape[0], 1))) + self.idx_h_old = np.asmatrix(np.zeros((1, self.V.shape[1]))) c_obj = sys.float_info.max best_obj = c_obj if run == 0 else best_obj # count the number of convergence checks that column clusters and @@ -200,7 +200,7 @@ def factorize(self): sp.eye(self.rank, self.rank, format='lil') else: self.beta_vec = sqrt(self.beta) * np.ones((1, self.rank)) - self.I_k = self.eta * np.mat(np.eye(self.rank)) + self.I_k = self.eta * np.asmatrix(np.eye(self.rank)) self.n_restart = 0 if self.callback_init: self.final_obj = c_obj @@ -287,8 +287,8 @@ def update(self): if self.n_restart >= 100: raise utils.MFError( "Too many restarts due to too large beta parameter.") - self.idx_w_old = np.mat(np.zeros((self.V.shape[0], 1))) - self.idx_h_old = np.mat(np.zeros((1, self.V.shape[1]))) + self.idx_w_old = np.asmatrix(np.zeros((self.V.shape[0], 1))) + self.idx_h_old = np.asmatrix(np.zeros((1, self.V.shape[1]))) self.inc = 0 self.W, _ = self.seed.initialize(self.V, self.rank, self.options) # normalize W and convert to lil @@ -333,7 +333,7 @@ def objective(self): resmat1 = elop(self.W, WHHt - VHt + self.eta ** 2 * self.W, min) res_vec = nz_data(resmat) + nz_data(resmat1) # L1 norm - self.conv = norm(np.mat(res_vec), 1) + self.conv = norm(np.asmatrix(res_vec), 1) err_avg = self.conv / len(res_vec) self.idx_w_old = idx_w self.idx_h_old = idx_h @@ -388,7 +388,7 @@ def _spfcnnls(self, C, A): # make infeasible solutions feasible (standard NNLS inner loop) if len(h_set) > 0: n_h_set = len(h_set) - alpha = np.mat(np.zeros((l_var, n_h_set))) + alpha = np.asmatrix(np.zeros((l_var, n_h_set))) while len(h_set) > 0 and iter < max_iter: iter += 1 alpha[:, :n_h_set] = np.Inf @@ -465,15 +465,15 @@ def __spcssls(self, CtC, CtA, p_set=None): # equivalent if CtC is square matrix 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[:, k] = sp.lil_matrix(np.asmatrix(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(list(range(l_var - 1, -1, -1))))), p_set) + sp.lil_matrix(np.asmatrix(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] + breaks = diff(np.asmatrix(sorted_p_set)) + break_idx = [-1] + find(np.asmatrix(breaks)) + [p_rhs] for k in range(len(break_idx) - 1): cols2solve = sorted_idx_set[ break_idx[k] + 1: break_idx[k + 1] + 1] @@ -483,7 +483,7 @@ def __spcssls(self, CtC, CtA, p_set=None): sol = sp.lil_matrix(K.shape) 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) + sol[:, k] = sp.lil_matrix(np.asmatrix(ls).T) i = 0 for c in cols2solve: j = 0 @@ -519,7 +519,7 @@ def _fcnnls(self, C, A): A = A.todense() if sp.isspmatrix(A) else A _, l_var = C.shape p_rhs = A.shape[1] - W = np.mat(np.zeros((l_var, p_rhs))) + W = np.asmatrix(np.zeros((l_var, p_rhs))) iter = 0 max_iter = 3 * l_var # precompute parts of pseudoinverse @@ -542,7 +542,7 @@ def _fcnnls(self, C, A): # make infeasible solutions feasible (standard NNLS inner loop) if len(h_set) > 0: n_h_set = len(h_set) - alpha = np.mat(np.zeros((l_var, n_h_set))) + alpha = np.asmatrix(np.zeros((l_var, n_h_set))) while len(h_set) > 0 and iter < max_iter: iter += 1 alpha[:, :n_h_set] = np.Inf @@ -597,7 +597,7 @@ def __cssls(self, CtC, CtA, p_set=None): Solve the set of equations CtA = CtC * K for variables defined in set p_set using the fast combinatorial approach (van Benthem and Keenan, 2004). """ - K = np.mat(np.zeros(CtA.shape)) + K = np.asmatrix(np.zeros(CtA.shape)) if p_set is None or p_set.size == 0 or all(p_set): # equivalent if CtC is square matrix K = np.linalg.lstsq(CtC, CtA, rcond=-1)[0] @@ -605,10 +605,10 @@ def __cssls(self, CtC, CtA, p_set=None): else: l_var, p_rhs = p_set.shape coded_p_set = dot( - np.mat(2 ** np.array(list(range(l_var - 1, -1, -1)))), p_set) + np.asmatrix(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] + breaks = diff(np.asmatrix(sorted_p_set)) + break_idx = [-1] + find(np.asmatrix(breaks)) + [p_rhs] for k in range(len(break_idx) - 1): cols2solve = sorted_idx_set[ break_idx[k] + 1: break_idx[k + 1] + 1] diff --git a/nimfa/methods/factorization/snmnmf.py b/nimfa/methods/factorization/snmnmf.py index c528511..e314c6b 100644 --- a/nimfa/methods/factorization/snmnmf.py +++ b/nimfa/methods/factorization/snmnmf.py @@ -192,10 +192,10 @@ def __init__(self, V, V1, seed=None, W=None, H=None, H1=None, nmf_mm.Nmf_mm.__init__(self, vars()) if self.A is None: self.A = sp.csr_matrix((self.V1.shape[1], self.V1.shape[1])) - self.A = self.A.tocsr() if sp.isspmatrix(self.A) else np.mat(self.A) + self.A = self.A.tocsr() if sp.isspmatrix(self.A) else np.asmatrix(self.A) if self.B is None: self.B = sp.csr_matrix((self.V.shape[1], self.V1.shape[1])) - self.B = self.B.tocsr() if sp.isspmatrix(self.B) else np.mat(self.B) + self.B = self.B.tocsr() if sp.isspmatrix(self.B) else np.asmatrix(self.B) self.tracker = mf_track.Mf_track() if self.track_factor and self.n_run > 1 \ or self.track_error else None diff --git a/nimfa/methods/seeding/nndsvd.py b/nimfa/methods/seeding/nndsvd.py index 03c61e8..f9cbe18 100644 --- a/nimfa/methods/seeding/nndsvd.py +++ b/nimfa/methods/seeding/nndsvd.py @@ -65,8 +65,8 @@ def initialize(self, V, rank, options): E = E.T if sp.isspmatrix(U): return self.init_sparse(V, U, S, E) - self.W = np.mat(np.zeros((V.shape[0], self.rank))) - self.H = np.mat(np.zeros((self.rank, V.shape[1]))) + self.W = np.asmatrix(np.zeros((V.shape[0], self.rank))) + self.H = np.asmatrix(np.zeros((self.rank, V.shape[1]))) # choose the first singular triplet to be nonnegative S = np.diagonal(S) self.W[:, 0] = sqrt(S[0]) * abs(U[:, 0]) diff --git a/nimfa/methods/seeding/random.py b/nimfa/methods/seeding/random.py index d8d6598..6c2414b 100644 --- a/nimfa/methods/seeding/random.py +++ b/nimfa/methods/seeding/random.py @@ -83,7 +83,7 @@ def gen_dense(self, dim1, dim2): :param dim2: Dimension along second axis. :type dim2: `int` """ - return np.mat(self.prng.uniform(0, self.max, (dim1, dim2))) + return np.asmatrix(self.prng.uniform(0, self.max, (dim1, dim2))) def __repr__(self): return "random.Random()" diff --git a/nimfa/methods/seeding/random_c.py b/nimfa/methods/seeding/random_c.py index d197f65..a7619c4 100644 --- a/nimfa/methods/seeding/random_c.py +++ b/nimfa/methods/seeding/random_c.py @@ -69,14 +69,14 @@ def initialize(self, V, rank, options): top_r = sorted( 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]))) + self.W = np.asmatrix(np.zeros((V.shape[0], self.rank))) + self.H = np.asmatrix(np.zeros((self.rank, V.shape[1]))) top_c = sorted(enumerate([norm(V[:, i], 2) 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 range(V.shape[0])]), key=itemgetter(1), reverse=True)[:self.l_r] - top_c = np.mat(list(zip(*top_c))[0]) - top_r = np.mat(list(zip(*top_r))[0]) + top_c = np.asmatrix(list(zip(*top_c))[0]) + top_r = np.asmatrix(list(zip(*top_r))[0]) 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) diff --git a/nimfa/methods/seeding/random_vcol.py b/nimfa/methods/seeding/random_vcol.py index 4bc696d..c663ca1 100644 --- a/nimfa/methods/seeding/random_vcol.py +++ b/nimfa/methods/seeding/random_vcol.py @@ -56,8 +56,8 @@ def initialize(self, V, rank, options): self.W = sp.lil_matrix((V.shape[0], self.rank)) self.H = sp.lil_matrix((self.rank, V.shape[1])) else: - self.W = np.mat(np.zeros((V.shape[0], self.rank))) - self.H = np.mat(np.zeros((self.rank, V.shape[1]))) + self.W = np.asmatrix(np.zeros((V.shape[0], self.rank))) + self.H = np.asmatrix(np.zeros((self.rank, V.shape[1]))) 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) diff --git a/nimfa/models/nmf.py b/nimfa/models/nmf.py index dafcbd7..4639c27 100644 --- a/nimfa/models/nmf.py +++ b/nimfa/models/nmf.py @@ -185,7 +185,7 @@ def connectivity(self, H=None, idx=None): if sp.isspmatrix(conn): return conn.__class__(conn, dtype='d') else: - return np.mat(conn, dtype='d') + return np.asmatrix(conn, dtype='d') def consensus(self, idx=None): """ @@ -204,7 +204,7 @@ def consensus(self, idx=None): if sp.isspmatrix(V): cons = V.__class__((V.shape[1], V.shape[1]), dtype=V.dtype) else: - cons = np.mat(np.zeros((V.shape[1], V.shape[1]))) + cons = np.asmatrix(np.zeros((V.shape[1], V.shape[1]))) for i in range(self.n_run): cons += self.connectivity( H=self.tracker.get_factor(i).H, idx=idx) diff --git a/nimfa/utils/linalg.py b/nimfa/utils/linalg.py index 9cf9404..add23ba 100644 --- a/nimfa/utils/linalg.py +++ b/nimfa/utils/linalg.py @@ -71,7 +71,7 @@ def trace(X): if sp.isspmatrix(X): return sum(X[i, i] for i in range(X.shape[0])) else: - return np.trace(np.mat(X)) + return np.trace(np.asmatrix(X)) def any(X, axis=None): @@ -106,7 +106,7 @@ def _raxis(now, row, col): check(now, row, col) now += 1 sol = [x != 0 for x in res] - return np.mat(sol) if axis == 0 else np.mat(sol).T + return np.asmatrix(sol) if axis == 0 else np.asmatrix(sol).T else: return X.any(axis) @@ -144,7 +144,7 @@ def _raxis(now, row, col): check(now, row, col) now += 1 sol = [x == X.shape[0] if axis == 0 else x == X.shape[1] for x in res] - return np.mat(sol) if axis == 0 else np.mat(sol).T + return np.asmatrix(sol) if axis == 0 else np.asmatrix(sol).T else: return X.all(axis) @@ -228,9 +228,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 range(X.shape[1])]) + return np.asmatrix([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 range(X.shape[0])]).T + return np.asmatrix([np.std(X[i, :].toarray(), axis, ddof) for i in range(X.shape[0])]).T else: return np.std(X, axis=axis, ddof=ddof) @@ -272,10 +272,10 @@ def _naxis(row, col): return res elif axis == 0: t = list(zip(*res)) - return list(t[0]), np.mat(t[1]) + return list(t[0]), np.asmatrix(t[1]) else: t = list(zip(*res)) - return list(t[0]), np.mat(t[1]).T + return list(t[0]), np.asmatrix(t[1]).T else: idxX = np.asmatrix(X).argmax(axis) if axis is None: @@ -326,10 +326,10 @@ def _naxis(row, col): return res elif axis == 0: t = list(zip(*res)) - return list(t[0]), np.mat(t[1]) + return list(t[0]), np.asmatrix(t[1]) else: t = list(zip(*res)) - return list(t[0]), np.mat(t[1]).T + return list(t[0]), np.asmatrix(t[1]).T else: idxX = np.asmatrix(X).argmin(axis) if axis is None: @@ -389,8 +389,8 @@ def svd(X): else: U, S, V = _svd_right(X) else: - U, S, V = nla.svd(np.mat(X), full_matrices=False) - S = np.mat(np.diag(S)) + U, S, V = nla.svd(np.asmatrix(X), full_matrices=False) + S = np.asmatrix(np.diag(S)) return U, S, V @@ -522,7 +522,7 @@ def multiply(X, Y): else: with warnings.catch_warnings(): warnings.simplefilter('ignore') - return np.multiply(np.mat(X), np.mat(Y)) + return np.multiply(np.asmatrix(X), np.asmatrix(Y)) def power(X, s): @@ -631,8 +631,8 @@ def elop(X, Y, op): X[X == 0] = np.finfo(X.dtype).eps Y[Y == 0] = np.finfo(Y.dtype).eps except ValueError: - return op(np.mat(X), np.mat(Y)) - return op(np.mat(X), np.mat(Y)) + return op(np.asmatrix(X), np.asmatrix(Y)) + return op(np.asmatrix(X), np.asmatrix(Y)) def _op_spmatrix(X, Y, op): @@ -690,7 +690,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 range(X.shape[1])] for i in range(X.shape[0])]) + return np.asmatrix([[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): @@ -742,7 +742,7 @@ def norm(X, p="fro"): }.get(p) return v(X) if v != None else sum(abs(x) ** p for x in X.data) ** (1. / p) else: - return nla.norm(np.mat(X), p) + return nla.norm(np.asmatrix(X), p) def vstack(X, format=None, dtype=None):