Mercurial > hg > segmentation
comparison pymf/nmfnnls.py @ 0:26838b1f560f
initial commit of a segmenter project
author | mi tian |
---|---|
date | Thu, 02 Apr 2015 18:09:27 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:26838b1f560f |
---|---|
1 #!/usr/bin/python | |
2 # | |
3 # Copyright (C) Christian Thurau, 2010. | |
4 # Licensed under the GNU General Public License (GPL). | |
5 # http://www.gnu.org/licenses/gpl.txt | |
6 """ | |
7 PyMF Non-negative Matrix Factorization. | |
8 | |
9 NMFALS: Class for Non-negative Matrix Factorization using non negative | |
10 least squares optimization (requires scipy.optimize) | |
11 | |
12 [1] Lee, D. D. and Seung, H. S. (1999), Learning the Parts of Objects by Non-negative | |
13 Matrix Factorization, Nature 401(6755), 788-799. | |
14 """ | |
15 | |
16 | |
17 | |
18 import scipy.optimize | |
19 from nmf import NMF | |
20 | |
21 __all__ = ["NMFNNLS"] | |
22 | |
23 class NMFNNLS(NMF): | |
24 """ | |
25 NMFNNLS(data, num_bases=4) | |
26 | |
27 | |
28 Non-negative Matrix Factorization. Factorize a data matrix into two matrices | |
29 s.t. F = | data - W*H | = | is minimal. H, and W are restricted to non-negative | |
30 data. Uses the Lawsons and Hanson's algorithm for non negative constrained | |
31 least squares (-> also see scipy.optimize.nnls) | |
32 | |
33 Parameters | |
34 ---------- | |
35 data : array_like, shape (_data_dimension, _num_samples) | |
36 the input data | |
37 num_bases: int, optional | |
38 Number of bases to compute (column rank of W and row rank of H). | |
39 4 (default) | |
40 | |
41 Attributes | |
42 ---------- | |
43 W : "data_dimension x num_bases" matrix of basis vectors | |
44 H : "num bases x num_samples" matrix of coefficients | |
45 ferr : frobenius norm (after calling .factorize()) | |
46 | |
47 Example | |
48 ------- | |
49 Applying NMF to some rather stupid data set: | |
50 | |
51 >>> import numpy as np | |
52 >>> data = np.array([[1.0, 0.0, 2.0], [0.0, 1.0, 1.0]]) | |
53 >>> nmf_mdl = NMFALS(data, num_bases=2) | |
54 >>> nmf_mdl.factorize(niter=10) | |
55 | |
56 The basis vectors are now stored in nmf_mdl.W, the coefficients in nmf_mdl.H. | |
57 To compute coefficients for an existing set of basis vectors simply copy W | |
58 to nmf_mdl.W, and set compute_w to False: | |
59 | |
60 >>> data = np.array([[1.5], [1.2]]) | |
61 >>> W = np.array([[1.0, 0.0], [0.0, 1.0]]) | |
62 >>> nmf_mdl = NMFALS(data, num_bases=2) | |
63 >>> nmf_mdl.W = W | |
64 >>> nmf_mdl.factorize(niter=1, compute_w=False) | |
65 | |
66 The result is a set of coefficients nmf_mdl.H, s.t. data = W * nmf_mdl.H. | |
67 """ | |
68 | |
69 def update_h(self): | |
70 def updatesingleH(i): | |
71 self.H[:,i] = scipy.optimize.nnls(self.W, self.data[:,i])[0] | |
72 | |
73 map(updatesingleH, xrange(self._num_samples)) | |
74 | |
75 | |
76 def update_w(self): | |
77 def updatesingleW(i): | |
78 self.W[i,:] = scipy.optimize.nnls(self.H.T, self.data[i,:].T)[0] | |
79 | |
80 map(updatesingleW, xrange(self._data_dimension)) |