Package: numba-plsa
Numba PLSAPLSA for sparse matrices implemented with Numba. Wicked fast.InstallationClone the repo:git clone https://github.com/TnTo/numba-plsa.gitInstall:pip install .Run the example:python example.pyUsageThe numba-plsa package provides two implementations: a basic NumPy method and a numba method. Theplsamethod wraps the basic algorithmic functionality, and the algorithm is chosen by using themethodargument (method='numba'ormethod='basic', the default). The basic method works for any NumPy document-term matrix, whereas the numba method is optimized for sparse matrices. Theplsamethod automatically converts the input document-term matrix to a COO sparse matrix. Theplsa_directmethod is also available, which assumes the input is already in COO form and skips some precomputation for faster performance on large matrices.Two very basic classes are included to assist with topic modeling tasks for text corpora. TheCorpusclass takes on text documents and can build a document-term matrix. ThePLSAModelclass has atrainmethod which provides an interface toplsa.For an example, see the example (conveniently namedexample.py). The numba method runs in under a second on a standard laptop with 4 GB of RAM available. The20 newsgroupsdata set, which contains 2,000 documents, is used for evaluation. Assuming NumPy seeds play nice cross-OS, the results should beTop topic terms
================
Topic 1: boswell, yalcin, onur, wright, mbytes
Topic 2: premiums, yeast, vitamin, sinus, candida
Topic 3: ports, pci, stereo, ankara, istanbul
Topic 4: icons, atari, lsd, cyprus, apps
Topic 5: wires, neutral, circuit, wiring, wire
Topic 6: gifs, simtel, jfif, gif, jpeg
Topic 7: nhl, sleeve, gant, players, league
Topic 8: mormon, gaza, xxxx, israeli, arabs
Topic 9: chi, det, suck, cubs, pit
Topic 10: cramer, theism, odwyer, bds, claytonWe can assign coherent labels to most topics, such as "pharmaceuticals" for Topic 2, "middle east" for Topic 8, and "baseball" for Topic 9. Adjusting corpus construction parameters, running for more iterations, or changing the number of topics can yield even better results.Performance comparisonsWe compare the two implementations on artificial problems of different sizes, all with document-term matrix sparsity around 95% (which is fairly dense for a text-based corpus). These results were obtained on a standard laptop with 4 GB of RAM available. The scriptspeed_test.pycan be used to recreate the figures.Corpus sizeVocab sizeNumber of topicsBasic method avg. time / iteration (best of 3)Numba method avg. time  / iteration (best of 3)100500100.0047 s0.00058 s2501000100.024 s0.0028 s1002500100.026 s0.0028 s10005000101.16 s0.042 s20006000102.59 s0.12 s30005000103.26 s0.13 sThe filelarge_speed_test.pycarries out a test for a large matrix: 10,000 documents and 100,000 terms in the vocabulary with 99% sparsity (10 million non-zero entries). Running a 5 topic model for 10 iterations takes around 30 seconds, or 3 seconds per iteration.We can also compare numba-plsa to a popular Python package on GitHub:PLSA. We used the example data from the PLSA repo. The two methods resulted the same distributions when using the same initializations.ImplementationCorpus sizeVocab sizeNumber of topicsNumber of iterationsTime (best of 3)PLSA package13212653044.89 snumba-plsa, basic1321265300.082 snumba-plsa, numba1321265300.006 s