Skip to content

Text 0.4.0 countvectorizer fittransform

github-actions[bot] edited this page Aug 21, 2026 · 1 revision

Lodestar.Text 0.4.0. This page is frozen at that release. Read the current documentation for what main says now. A link to a decision or a migration page follows main, and leaves the archive.

CountVectorizer.FitTransform

Learn the vocabulary and count the same corpus, in one pass.

public CsrMatrix FitTransform(IEnumerable<string> documents)

Parametersdocuments is the corpus, both learned from and counted.

ReturnsCsrMatrix, one row per document and one column per learned term.

ExceptionsArgumentNullException when documents is null. A corpus that leaves no terms does not throw: it yields a model of zero columns, which every later transform will produce empty rows against.

Example — the whole corpus at once.

using Lodestar.Text.Vectorization;

string[] docs = ["the cat eats", "the dog eats", "the cat and the dog"];

CsrMatrix counts = new CountVectorizer().FitTransform(docs);

int rows = counts.RowCount;         // => 3
int stored = counts.NonZeroCount;   // => 10

Remarks — equivalent to Fit then Transform on the same corpus, and not equivalent to fitting one corpus and transforming another. It exists because that is the common case and because doing it in one pass avoids enumerating the corpus twice — which matters when the corpus is a lazy sequence read from disk.

The fit is kept, so the vectorizer can go on to transform further corpora afterwards.

Applies to — net10.0, netstandard2.0.

See alsoCountVectorizer.Fit, CountVectorizer.Transform, CsrMatrix.

Lodestar

Project

Clone this wiki locally