Skip to content

Text tfidftransformer fittransform

github-actions[bot] edited this page Aug 27, 2026 · 26 revisions

Development build. This page describes main, not a released package. The latest published Lodestar.Text is 0.4.0 — read its documentation.

TfidfTransformer.FitTransform

Learn the document frequencies and weight the same matrix.

public CsrMatrix FitTransform(CsrMatrix counts)

Parameterscounts is the count matrix, both learned from and weighted.

ReturnsCsrMatrix, the same shape as counts, weighted and normalized by TfidfOptions.Norm.

ExceptionsArgumentNullException when counts is null.

Example — the common case, where the counts and the corpus to weight are the same.

using Lodestar.Text.Vectorization;

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

CsrMatrix weighted = new TfidfTransformer().FitTransform(counts);

int rows = weighted.RowCount;      // => 3
int columns = weighted.ColumnCount; // => 5

Remarks — the shape is unchanged: weighting replaces values, it does not add or remove columns. A count of zero stays absent, because a term the document does not hold has no weight however rare it is.

The returned matrix is a new one; counts is left as it was, unlike CsrMatrix.NormalizeRows, which works in place.

Applies to — net10.0, netstandard2.0.

See alsoTfidfTransformer.Fit, TfidfVectorizer.FitTransform.

Lodestar

Project

Clone this wiki locally