Efficient Text Data Representation with Sparse Matrices
Introduction to Sparse Matrices
Hello and welcome to this lesson on "Efficient Text Data Representation with Sparse Matrices". As you recall, in our previous lessons, we transformed raw text data into numerical features, for example, using the Bag-of-Words (BoW) or Term Frequency-Inverse Document Frequency (TF-IDF) techniques. These transformation methods often create what we call "Sparse Matrices," an incredibly memory-efficient way of storing high-dimensional data.
Let's break this down a bit. In the context of text data, each unique word across all documents could be treated as a distinct feature. However, each document will only include a small subset of these available features or unique words. Meaning, most entries in our feature matrix end up being 0s, hence resulting in a sparse matrix.
We'll begin with a simple non-text matrix to illustrate sparse matrices and later connect this knowledge to our journey on text data transformation.
Sparse Matrix Formats: CSR
In this section, we'll investigate how we can handle sparse matrices in different formats including: Compressed Sparse Row (CSR), Compressed Sparse Column (CSC), and the Coordinate (COO) formats.
We'll start with the CSR format, a common format for sparse matrices that is excellent for quick arithmetic operations and matrix vector calculations.
The output of the above code will be:
Observe that in the output of the Compressed Sparse Row representation, it records the values in the matrix row-wise, starting from the top. Each entry (0, 2), for example, tells us that the element in the 0th row and 2nd column is 2.
Sparse Matrix Formats: CSC
Next, let's convert our vectors matrix to the CSC format. This format, like the CSR format, also forms the backbone of many operations we perform on sparse matrices. But it stores the non-zero entries column-wise, and is especially efficient for column slicing operations.
The output of the above code will be:
In this Compressed Sparse Column output, the non-zero entries are stored column-wise. Essentially, CSC format is a transpose of the CSR format.
