├── README.md └── nmf.js /README.md: -------------------------------------------------------------------------------- 1 | nmf.js 2 | ====== 3 | 4 | Implementation of Non-negative Matrix Factorization in Javascript. This is a rather ambitious project and relies on the numeric.js library for linear algebra support. 5 | 6 | Simple usage: 7 | 8 | var maxiterations = 20; 9 | 10 | var k = 2; 11 | 12 | var tolerance = 0.001; 13 | 14 | var A; 15 | 16 | 17 | 18 | A = [[1,1,0,0], 19 | [1,1,0,0], 20 | [0,0,1,1], 21 | [0,0,1,1]]; 22 | 23 | 24 | nmf_factorized = nmf.mu(A, k, maxiterations, tolerance); 25 | 26 | alert(numeric.prettyPrint(nmf_factorized.W)); 27 | 28 | alert(numeric.prettyPrint(nmf_factorized.H)); 29 | -------------------------------------------------------------------------------- /nmf.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | // Javascript implementation of Non-negative Matrix Factorisation Algorithms 4 | 5 | var nmf = (typeof exports === "undefined")?(function nmf() {}):(exports); 6 | if(typeof global !== "undefined") { global.nmf = nmf; } 7 | 8 | nmf.version = "0.8"; 9 | nmf.epsilon = 0.0001; 10 | 11 | nmf.mu = function mu(A, k, maxiterations, tolerance) 12 | { 13 | // Implements Lee and Seungs Multiplicative Update Algorithm 14 | var W,H, A_dim; 15 | var m,n,i; 16 | 17 | A_dim = numeric.dim(A); 18 | m = A_dim[0]; 19 | n = A_dim[1]; 20 | 21 | W = numeric.random([m,k]); // initialize W as random matrix 22 | H = numeric.random([k,n]); // initialize H as random matrix 23 | 24 | for (i=0; i