% % Plot the documents and terms on the same set of 2D axes after % using a rank-2 approximation of the term-document matrix, to % see where "clusters" might happen. A = [... 0 1 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 1 0 0 1 0 0 0 ]; [m,n] = size(A); for k = 1:n A(:,k) = A(:,k)/norm(A(:,k)); end % ------------------------- % Compute the full SVD of A % ------------------------- [U,S,V] = svd(A); % ------------------------------------------- % Get the rank-2 approximation to A with SVD: % ------------------------------------------- A2 = U(:,1:2)*S(1:2,1:2)*[V(:,1:2)]' ; % ----------------------------------------------------------- % See what the terms and documents look like in reduced space % ----------------------------------------------------------- docs = -U(:,1:2)'*A; terms = -A*V(:,1:2); plot(docs(1,:), docs(2,:), 'b+', ... terms(:,1), terms(:,2), 'g*') terms = terms'; % ------------------------------------- % Fudge factor to slightly shift points % ------------------------------------- e = 0.05; text(terms(1,1), terms(2,1), 'baby') text(terms(1,2), terms(2,2)+e, 'child') text(terms(1,3), terms(2,3), 'guide') text(terms(1,4), terms(2,4), 'health') text(terms(1,5), terms(2,5), 'home') text(terms(1,6), terms(2,6), 'infant') text(terms(1,7), terms(2,7)+e, 'proofing') text(terms(1,8), terms(2,8), 'safety') text(terms(1,9)-e, terms(2,9)+e, 'toddler') text(docs(1,1), docs(2,1), 'D1') text(docs(1,2), docs(2,2), 'D2') text(docs(1,3), docs(2,3), 'D3') text(docs(1,4), docs(2,4), 'D4') text(docs(1,5), docs(2,5), 'D5') text(docs(1,6), docs(2,6), 'D6') text(docs(1,7), docs(2,7), 'D7')