pub struct TfidfVectorizer { /* private fields */ }Expand description
TF-IDF vectorizer for text preprocessing.
Wraps CountVectorizer and applies Inverse Document Frequency (IDF) weighting
with L2 normalization per document. Computes IDF as log((n_docs + 1) / (df + 1)) + 1
to match sklearn’s smooth_idf=True behavior.
§Usage
- Use
fit_transformwhen training (more efficient) - Use
fit+transformwhen you need the vectorizer separately - Serialize with
to_bytes()(bincode) orto_json()(serde feature)
Implementations§
Source§impl TfidfVectorizer
impl TfidfVectorizer
Sourcepub fn fit<T: AsRef<str> + Sync>(
texts: &[T],
count_vectorizer_params: VectorizerParams,
) -> Self
pub fn fit<T: AsRef<str> + Sync>( texts: &[T], count_vectorizer_params: VectorizerParams, ) -> Self
Fit vectorizer on training texts.
Tokenizes texts, builds vocabulary (filtering by min_df/max_df), computes IDF weights.
§Arguments
texts- Training documentscount_vectorizer_params- Configuration for n-gram extraction and vocabulary filtering
Sourcepub fn transform<T: AsRef<str> + Sync>(&self, texts: &[T]) -> CsMat<f32>
pub fn transform<T: AsRef<str> + Sync>(&self, texts: &[T]) -> CsMat<f32>
Transform texts to TF-IDF sparse matrix using fitted vocabulary.
§Returns
Sparse CSR matrix of shape (n_texts, n_features) with L2-normalized TF-IDF values
Sourcepub fn fit_transform<T: AsRef<str> + Sync>(
texts: &[T],
count_vectorizer_params: VectorizerParams,
) -> (Self, CsMat<f32>)
pub fn fit_transform<T: AsRef<str> + Sync>( texts: &[T], count_vectorizer_params: VectorizerParams, ) -> (Self, CsMat<f32>)
Fit vectorizer and transform texts in a single pass.
More efficient than calling fit() + transform() separately: tokenizes and
computes n-grams only once.
§Returns
Tuple of (fitted vectorizer, TF-IDF matrix)
Sourcepub fn num_features(&self) -> usize
pub fn num_features(&self) -> usize
Number of features (vocabulary size) in the fitted vectorizer.
Sourcepub fn vocabulary(&self) -> HashMap<String, usize>
pub fn vocabulary(&self) -> HashMap<String, usize>
Get vocabulary as a mapping of text n-grams to feature indices.
Note: Requires reverse tokenization (tiktoken decoding), which can be slow for large vocabularies.
Sourcepub fn params(&self) -> &VectorizerParams
pub fn params(&self) -> &VectorizerParams
Get the vectorizer parameters.
Source§impl TfidfVectorizer
impl TfidfVectorizer
Sourcepub fn to_bytes(&self) -> Result<Vec<u8>, EncodeError>
pub fn to_bytes(&self) -> Result<Vec<u8>, EncodeError>
Serialize vectorizer to bytes using bincode format.
Used for fast binary serialization. Preferred format for Rust-to-Rust communication.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError>
Deserialize vectorizer from bincode bytes.
Trait Implementations§
Source§impl<'__de, __Context> BorrowDecode<'__de, __Context> for TfidfVectorizer
impl<'__de, __Context> BorrowDecode<'__de, __Context> for TfidfVectorizer
Source§fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>(
decoder: &mut __D,
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>
Source§impl Clone for TfidfVectorizer
impl Clone for TfidfVectorizer
Source§fn clone(&self) -> TfidfVectorizer
fn clone(&self) -> TfidfVectorizer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TfidfVectorizer
impl Debug for TfidfVectorizer
Source§impl<__Context> Decode<__Context> for TfidfVectorizer
impl<__Context> Decode<__Context> for TfidfVectorizer
Auto Trait Implementations§
impl Freeze for TfidfVectorizer
impl RefUnwindSafe for TfidfVectorizer
impl Send for TfidfVectorizer
impl Sync for TfidfVectorizer
impl Unpin for TfidfVectorizer
impl UnwindSafe for TfidfVectorizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.