Abstractions csrmatrix todense - CyrilB1531/lodestar GitHub Wiki

HomeAbstractionsThe shared sparse primitive

CsrMatrix.ToDense

The same matrix with its zeros written out, for inspection.

public double[,] ToDense()

Returnsdouble[,] of RowCount × ColumnCount, every cell present, the stored values in their places and zeros everywhere else.

Example — the cell that says the appears twice in the third document.

using Lodestar.Abstractions;
using Lodestar.Text.Vectorization;

string[] docs = ["the cat eats", "the dog eats", "the cat and the dog"];
var cv = new CountVectorizer();
CsrMatrix counts = cv.FitTransform(docs);

// Features are sorted: and, cat, dog, eats, the — so "the" is column 4.
double[,] dense = counts.ToDense();
double theInThird = dense[2, 4];  // => 2

Remarksallocates RowCount × ColumnCount doubles, which is exactly the array the sparse layout exists to avoid. On the three-document corpus above that is fifteen cells; on a real corpus it is the number that made sparsity necessary in the first place. Reach for it to look at a small matrix, to hand a small one to something that wants a rectangular array, or in a test — not as a step in a pipeline.

To read one cell without materialising the rest, walk RowPointers and ColumnIndices directly; to reduce the whole matrix against a vector, Multiply does it without densifying.

A column stored twice in one row is written out as the sum of its entries, which is what Multiply computes from the same row and what scipy's toarray() returns.

Applies to — net10.0, netstandard2.0.

See alsoCsrMatrix, CsrMatrix.Multiply.