pub struct TfidfVectorizer { /* private fields */ }Expand description
Applies TF-IDF weighting and L2 normalization to text features.
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()(rkyv) 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<AlignedVec, Error>
pub fn to_bytes(&self) -> Result<AlignedVec, Error>
Serialize vectorizer to bytes using rkyv format.
Provides zero-copy capable binary serialization.
Sourcepub fn access_from_bytes(
bytes: &[u8],
) -> Result<&ArchivedTfidfVectorizer, Error>
pub fn access_from_bytes( bytes: &[u8], ) -> Result<&ArchivedTfidfVectorizer, Error>
Access vectorizer from rkyv bytes without full deserialization (zero-copy).
Preferred for fast read-only access.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
Deserialize vectorizer from rkyv bytes back to the original type.
Use when you need a mutable, owned instance. Handles unaligned input by copying to an aligned buffer if needed.
Trait Implementations§
Source§impl Archive for TfidfVectorizer
impl Archive for TfidfVectorizer
Source§const COPY_OPTIMIZATION: CopyOptimization<Self>
const COPY_OPTIMIZATION: CopyOptimization<Self>
serialize. Read moreSource§impl Clone for TfidfVectorizer
impl Clone for TfidfVectorizer
Source§impl Debug for TfidfVectorizer
impl Debug for TfidfVectorizer
Source§impl<__D: Fallible + ?Sized> Deserialize<TfidfVectorizer, __D> for Archived<TfidfVectorizer>where
CountVectorizer: Archive,
<CountVectorizer as Archive>::Archived: Deserialize<CountVectorizer, __D>,
Vec<f32>: Archive,
<Vec<f32> as Archive>::Archived: Deserialize<Vec<f32>, __D>,
impl<__D: Fallible + ?Sized> Deserialize<TfidfVectorizer, __D> for Archived<TfidfVectorizer>where
CountVectorizer: Archive,
<CountVectorizer as Archive>::Archived: Deserialize<CountVectorizer, __D>,
Vec<f32>: Archive,
<Vec<f32> as Archive>::Archived: Deserialize<Vec<f32>, __D>,
Source§fn deserialize(
&self,
deserializer: &mut __D,
) -> Result<TfidfVectorizer, <__D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut __D, ) -> Result<TfidfVectorizer, <__D as Fallible>::Error>
Auto Trait Implementations§
impl Freeze for TfidfVectorizer
impl RefUnwindSafe for TfidfVectorizer
impl Send for TfidfVectorizer
impl Sync for TfidfVectorizer
impl Unpin for TfidfVectorizer
impl UnsafeUnpin for TfidfVectorizer
impl UnwindSafe for TfidfVectorizer
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> ArchiveUnsized for Twhere
T: Archive,
impl<T> ArchiveUnsized for Twhere
T: Archive,
Source§type Archived = <T as Archive>::Archived
type Archived = <T as Archive>::Archived
Archive, it may be
unsized. Read moreSource§fn archived_metadata(
&self,
) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, S> SerializeUnsized<S> for T
impl<T, S> SerializeUnsized<S> 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.