Text tfidfvectorizer save - CyrilB1531/lodestar GitHub Wiki
Development build. This page describes
main, not a released package. The latest published Lodestar.Text is 0.6.0 — read its documentation.
Home › Text › Vectorization
TfidfVectorizer.Save
Write a fitted vectorizer out, vocabulary and document frequencies together.
public void Save(Stream destination)
public void Save(string path)
Parameters — destination is a writable stream, left open for the caller to dispose; path
is a file to create or overwrite.
Exceptions — InvalidOperationException when nothing has been fitted yet.
ArgumentNullException for a null stream or path. IOException from the stream or file system.
Example — saving and restoring, with the weights intact.
using Lodestar.Text.Vectorization;
var tv = new TfidfVectorizer();
tv.Fit(["the cat eats", "the dog eats"]);
using var buffer = new MemoryStream();
tv.Save(buffer);
buffer.Position = 0;
TfidfVectorizer restored = TfidfVectorizer.Load(buffer);
string first = restored.GetFeatureNames()[0]; // => cat
Remarks — what is written is both halves of the fit: the vocabulary and the document
frequencies. That second half is the reason saving matters more here than for
CountVectorizer — frequencies are a property of the training corpus, and
a corpus that is gone cannot be measured again.
The stream overload leaves destination open, so a vectorizer can be one entry in a larger
archive.
The write is not bounded the way a load is. A vocabulary past a default of
ArtifactLoadOptions — a token over 1024 characters, more
than a million features — saves, and loads only with that bound raised.
Applies to — net10.0, netstandard2.0.
See also — TfidfVectorizer.Load,
TfidfVectorizer.SaveAsync.