liveBook · Manning

Dense matrix

As explained in Natural Language Processing in Action, Second Edition

A dense matrix is a matrix representation that explicitly stores every element, including zero-valued elements.

A dense matrix explicitly stores every element, including zeros. In text processing, it makes all document–token counts visible, but wastes space when most entries are zero. section 3.2.1

Use a dense representation when:

  • Inspecting a small matrix is more important than conserving memory. section 3.2.1
  • The matrix contains many meaningful nonzero values rather than mostly empty placeholders. section 3.2.1
  • Avoid converting large sparse data without checking scale, because the resulting array can exhaust available memory. section 3.2.1

Viewing count vectors as a dense NumPy array

After scikit-learn creates a 16-by-240 sparse count matrix, call `count_vectors.toarray()` to display every document–token count, including its many zeros. section 3.2.1

Listing 3.4 Using sklearn to compute word count vectors
>>> count_vectors.toarray()                                    #1
array([[0, 0, 0, 0, 0, 0, 0, 1, ..., 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 2, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0],
       ...,
       [0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 0, ..., 1, 0, 0, 0, 0, 1, 0, 0]])

Inspect the complete NumPy array produced from document word-count vectors. section 3.2.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines explicit zero storage and demonstrates conversion, stopping short of recommending it for large datasets.
Examples
Uses 16 sentences, 240 tokens, CountVectorizer output, and a displayed NumPy array.
What it assumes
Builds on familiarity with vectors, arrays, documents, and token counts.
What you can do after
Apply via NumPy to inspect a small sparse matrix as a complete array.
How it connects
Connects sparse matrices, count vectors, NumPy arrays, similarity matrices, and graphs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.