Skip to content

Commit 28fef84

Browse files
author
ddbourgin
committed
add first sketch of the factorization module
1 parent 5ffd87b commit 28fef84

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

Diff for: docs/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ that in mind, don't just read the docs -- read the source!
3838

3939
numpy_ml.nonparametric
4040

41+
numpy_ml.factorization
42+
4143
numpy_ml.trees
4244

4345
numpy_ml.neural_nets

Diff for: docs/numpy_ml.factorization.factors.rst

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
``VanillaALS``
2+
--------------
3+
.. autoclass:: numpy_ml.factorization.VanillaALS
4+
:members:
5+
:undoc-members:
6+
7+
``NMF``
8+
--------
9+
.. autoclass:: numpy_ml.factorization.NMF
10+
:members:
11+
:undoc-members:

Diff for: docs/numpy_ml.factorization.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Matrix factorization
2+
####################
3+
4+
.. toctree::
5+
:maxdepth: 3
6+
7+
numpy_ml.factorization.factors

Diff for: numpy_ml/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# noqa
2+
"""Common ML and ML-adjacent algorithms implemented in NumPy"""
3+
14
from . import utils
25
from . import preprocessing
36

@@ -11,3 +14,4 @@
1114
from . import rl_models
1215
from . import trees
1316
from . import bandits
17+
from . import factorization

Diff for: numpy_ml/factorization/factors.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, K, alpha=1, max_iter=200, tol=1e-4):
2525
where :math:`||\cdot||` denotes the Frobenius norm, **X** is the
2626
:math:`N \times M` data matrix, :math:`\mathbf{W}` and
2727
:math:`\mathbf{H}` are learned factor matrices with dimensions :math:`N
28-
\times K` and :math:`K \times M`, respectively. :math:`\alpha` is a
28+
\times K` and :math:`K \times M`, respectively, and :math:`\alpha` is a
2929
user-defined regularization weight.
3030
3131
ALS proceeds by alternating between fixing **W** and optimizing for
@@ -36,8 +36,8 @@ def __init__(self, K, alpha=1, max_iter=200, tol=1e-4):
3636
References
3737
----------
3838
.. [1] Gillis, N. (2014). The why and how of nonnegative matrix
39-
factorization. *Regularization, optimization, kernels, and support
40-
vector machines*, 12(257), 257-291.
39+
factorization. *Regularization, optimization, kernels, and support
40+
vector machines, 12(257)*, 257-291.
4141
4242
Parameters
4343
----------
@@ -294,14 +294,15 @@ def fit(self, X, W=None, H=None, n_initializations=10, verbose=False):
294294
\mathbf{X}^{(j)} :=
295295
\mathbf{X} - \mathbf{WH}^\top + \mathbf{w}_j \mathbf{h}_j^\top
296296
297-
where :math:`\mathbf{X}^{(j)}` is the `j`th residue, **X** is the input
298-
data matrix, and :math:`\mathbf{w}_j` and :math:`\mathbf{h}_j` are the
299-
`j`th columns of the current factor matrices **W** and **H**. HALS
300-
proceeds by minimizing the cost for each residue, first with respect to
301-
:math:`\mathbf{w}_j` holding :math:`\mathbf{h}_j` fixed, and then with
302-
respect to :math:`\mathbf{h}_j`, holding the newly updated
303-
:math:`\mathbf{w}_j` fixed. The residue cost :math:`\mathcal{L}^{(j)}`
304-
for :math:`\mathbf{X}^{j}` is simply:
297+
where :math:`\mathbf{X}^{(j)}` is the :math:`j^{th}` residue, **X** is
298+
the input data matrix, and :math:`\mathbf{w}_j` and
299+
:math:`\mathbf{h}_j` are the :math:`j^{th}` columns of the current
300+
factor matrices **W** and **H**. HALS proceeds by minimizing the cost
301+
for each residue, first with respect to :math:`\mathbf{w}_j` holding
302+
:math:`\mathbf{h}_j` fixed, and then with respect to
303+
:math:`\mathbf{h}_j`, holding the newly updated :math:`\mathbf{w}_j`
304+
fixed. The residue cost :math:`\mathcal{L}^{(j)}` for
305+
:math:`\mathbf{X}^{j}` is simply:
305306
306307
.. math::
307308

0 commit comments

Comments
 (0)