Loading...

verticapy.machine_learning.vertica.feature_extraction.text.TfidfVectorizer

class verticapy.machine_learning.vertica.feature_extraction.text.TfidfVectorizer(name: str | None = None, overwrite_model: bool = False, lowercase: bool = True, vocabulary: Annotated[list | ndarray, 'Array Like Structure'] | None = None, max_df: Annotated[int | float | Decimal, 'Python Numbers'] | None = None, min_df: Annotated[int | float | Decimal, 'Python Numbers'] | None = None, norm: Literal['l1', 'l2', None] = 'l2', smooth_idf: bool = True, compute_vocabulary: bool = True)

[Beta Version] Create tfidf representation of documents.

The formula that is used to compute the tf-idf for a term t of a document d in a document set is

\[tf-idf(t, d) = tf(t, d) * idf(t),\]

and if smooth_idf = False, the idf is computed as

\[idf(t) = log [ n / df(t) ] + 1,\]

where n is the total number of documents in the document set and df(t) is the document frequency of t; the document frequency is the number of documents in the document set that contain the term t. The effect of adding “1” to the idf in the equation above is that terms with zero idf, i.e., terms that occur in all documents in a training set, will not be entirely ignored.

If smooth_idf=True (the default), the constant “1” is added to the numerator and denominator of the idf as if an extra document was seen containing every term in the collection exactly once, which prevents zero divisions:

\[idf(t) = log [ (1 + n) / (1 + df(t)) ] + 1.\]

Parameters

name: str, optional

Name of the model.

overwrite_model: bool, optional

If set to True, training a model with the same name as an existing model overwrites the existing model.

lowercase: bool, optional

Converts all the elements to lowercase before processing.

vocabulary: list, optional

A list of string elements to be regarded as the primary vocabulary.

max_df: PythonNumber, optional

While constructing the vocabulary, exclude terms with a document frequency surpassing the specified threshold, essentially treating them as corpus-specific stop words. If the value is a float within the range [0.0, 1.0], it denotes a proportion of documents; if an integer, it signifies absolute counts. Note that this parameter is disregarded if a custom vocabulary is provided.

min_df: PythonNumber, optional

When constructing the vocabulary, omit terms with a document frequency below the specified threshold, often referred to as the cut-off in literature. If the value is a float within the range [0.0, 1.0], it denotes a proportion of documents; if an integer, it signifies absolute counts. It’s important to note that this parameter is disregarded if a custom vocabulary is provided.

norm: str, optional

The tfidf values of each document will have unit norm, either:

  • l2:

    Sum of squares of vector elements is 1.

  • l1:

    Sum of absolute values of vector elements is 1.

  • None:

    No normalization.

smooth_idf: bool, optional

Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions.

compute_vocabulary: bool, optional

If set to true, the vocabulary is computed, making the operation more resource-intensive.

Attributes

Many attributes are created during the fitting phase.

vocabulary_: ArrayLike

The ultimate vocabulary. If empty, it implies that all words are utilized, and the user opted not to compute a specific vocabulary.

fixed_vocabulary_: bool

Boolean indicating whether a vocabulary was supplied by the user.

idf_: vDataFrame

The IDF table which is computed based on the relation used for the fitting process.

tf_: vDataFrame

The TF table which is computed based on the relation used for the fitting process.

stop_words_: ArrayLike

Terms are excluded under the following conditions:

  • They appear in an excessive number of documents

(controlled by max_df).

  • They appear in an insufficient number of documents

(controlled by min_df).

This functionality is only applicable when no specific vocabulary is provided and compute_vocabulary is set to True.

n_document_: int

Total number of document. This functionality is only applicable when no specific vocabulary is provided and compute_vocabulary is set to True.

Note

All attributes can be accessed using the get_attributes() method.

Examples

We import verticapy:

import verticapy as vp

Hint

By assigning an alias to verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions from verticapy are used as intended without interfering with functions from other libraries.

For this example, let’s generate some text.

documents = [
    "Natural language processing is a field of study in artificial intelligence.",
    "TF-IDF stands for Term Frequency-Inverse Document Frequency.",
    "Machine learning algorithms can be applied to text data for classification.",
    "The 20 Newsgroups dataset is a collection of text documents used for text classification.",
    "Clustering is a technique used to group similar documents together.",
    "Python is a popular programming language for natural language processing tasks.",
    "TF-IDF is a technique widely used in information retrieval.",
    "An algorithm is a set of instructions designed to perform a specific task.",
    "Data preprocessing is an important step in preparing data for machine learning.",
]

Next, we can insert this text into a vDataFrame:

data = vp.vDataFrame(
    {
        "id": (list(range(1,len(documents)+1))),
        "values": documents,
    }
)

Then we can initialize the object and fit the model, to learn the idf weigths.

from verticapy.machine_learning.vertica.feature_extraction.text import TfidfVectorizer

model = TfidfVectorizer(name = "test_idf")
model.fit(
    input_relation = data,
    index = "id",
    x = "values",
)

We apply the transform function to obtain the idf representation.

model.transform(
    vdf = data,
    index = "id",
    x = "values",
)
123
row_id
Integer
Abc
word
Varchar(18320)
123
tfidf
Float(22)
12term0.40660486945441
22document0.40660486945441
32for0.235418153692232
42frequency0.40660486945441
52stands0.40660486945441
62tfidf0.34342494608277
72frequencyinverse0.40660486945441
85to0.277657271947304
95used0.277657271947304
105together0.378089503868613
115similar0.378089503868613
125documents0.319340414330917
135technique0.319340414330917
145is0.177225040025995
155group0.378089503868613
165clustering0.378089503868613
175a0.196572815172405
188to0.231156487059915
198task0.314768782735436
208set0.314768782735436
218of0.231156487059915
228specific0.314768782735436
238an0.265858725166046
248is0.147544191384394
258instructions0.314768782735436
268perform0.314768782735436
278designed0.314768782735436
288algorithm0.314768782735436
298a0.327303377203496
303to0.252759296711247
313be0.344185608471555
323for0.199278332752225
333data0.290704644512533
343text0.290704644512533
353machine0.290704644512533
363learning0.290704644512533
373classification0.290704644512533
383can0.344185608471555
393algorithms0.344185608471555
403applied0.344185608471555
416tasks0.331396587157975
426python0.331396587157975
436processing0.279902833503824
446popular0.331396587157975
456natural0.279902833503824
466language0.559805667007647
476for0.191873680197981
486programming0.331396587157975
496is0.15533828054629
506a0.172296663646098
517widely0.408405860386832
527used0.299920669264876
537tfidf0.34494609169692
547retrieval0.408405860386832
557information0.408405860386832
567technique0.34494609169692
577is0.19143547814292
587in0.299920669264876
597a0.212334616242205
601processing0.304205711077076
611of0.264498084357478
621natural0.304205711077076
631language0.304205711077076
641field0.360170467668849
651study0.360170467668849
661is0.168825701046106
671in0.264498084357478
681intelligence0.360170467668849
691artificial0.360170467668849
701a0.18725651478606
714of0.219590693010443
724200.299019491159736
734used0.219590693010443
744newsgroups0.299019491159736
754for0.173127824615757
764text0.5051132455301
774documents0.25255662276505
784dataset0.299019491159736
794is0.14016189486115
804the0.299019491159736
814classification0.25255662276505
824collection0.299019491159736
834a0.155463461871492
849preprocessing0.314847512398901
859preparing0.314847512398901
869for0.182292012791185
879an0.265925221493223
889step0.314847512398901
899machine0.265925221493223
909learning0.265925221493223
919is0.147581094994826
929in0.231214303696863
939data0.531850442986445
949important0.314847512398901
Rows: 1-94 | Columns: 3

Notice how we can get the idf weight/score of each word in each row. We can also get the results in a more convient form by switching the pivot parameter to True. But for large datasets this is not ideal.

Advanced Analysis

In the above result, we can observe some less informative words such as “is” and “a”, which may not provide meaningful insights.

To address this issue, we can make use of the max_df parameter to exclude words that occur too frequently and might be irrelevant. Similarly, we can leverage the min_df parameter to eliminate words with low frequency that may not contribute significantly.

Let’s apply these parameters to remove common words like “is” and “a.”

model = TfidfVectorizer(max_df = 4, min_df = 1,)
model.fit(
    input_relation = data,
    index = "id",
    x = "values",
)
model.transform(
    vdf = data,
    index = "id",
    x = "values",
)
123
row_id
Integer
Abc
word
Varchar(18320)
123
tfidf
Float(22)
12tfidf0.353356315856649
22stands0.418363314367839
32frequencyinverse0.418363314367839
42term0.418363314367839
52document0.418363314367839
62frequency0.418363314367839
75group0.392071004311936
85similar0.392071004311936
95together0.392071004311936
105clustering0.392071004311936
115technique0.331149412197439
125documents0.331149412197439
135to0.287924854705095
145used0.287924854705095
158set0.337253796525324
168algorithm0.337253796525324
178perform0.337253796525324
188specific0.337253796525324
198designed0.337253796525324
208instructions0.337253796525324
218task0.337253796525324
228an0.284849925785026
238to0.247668787784263
248of0.247668787784263
253be0.351230258342344
263algorithms0.351230258342344
273applied0.351230258342344
283can0.351230258342344
293data0.296654667947555
303learning0.296654667947555
313text0.296654667947555
323machine0.296654667947555
333classification0.296654667947555
343to0.25793267033028
356python0.347518644513298
366programming0.347518644513298
376popular0.347518644513298
386tasks0.347518644513298
396language0.587039559633795
406processing0.293519779816897
416natural0.293519779816897
427retrieval0.426194265971802
437widely0.426194265971802
447information0.426194265971802
457tfidf0.359970462253805
467technique0.359970462253805
477in0.312983925759643
487used0.312983925759643
491field0.372194346950245
501study0.372194346950245
511intelligence0.372194346950245
521artificial0.372194346950245
531language0.314361270943981
541processing0.314361270943981
551natural0.314361270943981
561of0.273328050503949
571in0.273328050503949
584200.310702091311095
594the0.310702091311095
604dataset0.310702091311095
614collection0.310702091311095
624newsgroups0.310702091311095
634text0.5248478656908
644classification0.2624239328454
654documents0.2624239328454
664of0.228170034288334
674used0.228170034288334
689important0.323881981810178
699step0.323881981810178
709preprocessing0.323881981810178
719preparing0.323881981810178
729data0.547111756382014
739learning0.273555878191007
749machine0.273555878191007
759an0.273555878191007
769in0.237848939423482
Rows: 1-76 | Columns: 3

Notice how we have removed the unnecessary words.

We can also see which words were omitted using the stop_words_ attribute:

model.stop_words_
Out[4]: array(['a', 'for', 'is'], dtype='<U3')

See also

vDataColumn.pivot() : pivot vDataFrame.
__init__(name: str | None = None, overwrite_model: bool = False, lowercase: bool = True, vocabulary: Annotated[list | ndarray, 'Array Like Structure'] | None = None, max_df: Annotated[int | float | Decimal, 'Python Numbers'] | None = None, min_df: Annotated[int | float | Decimal, 'Python Numbers'] | None = None, norm: Literal['l1', 'l2', None] = 'l2', smooth_idf: bool = True, compute_vocabulary: bool = True) None

Must be overridden in the child class

Methods

__init__([name, overwrite_model, lowercase, ...])

Must be overridden in the child class

contour([nbins, chart])

Draws the model's contour plot.

deploySQL([X])

Returns the SQL code needed to deploy the model.

does_model_exists(name[, raise_error, ...])

Checks whether the model is stored in the Vertica database.

drop()

Drops the model from the Vertica database.

export_models(name, path[, kind])

Exports machine learning models.

fit(input_relation, index, x[, return_report])

Applies basic pre-processing.

get_attributes([attr_name])

Returns the model attributes.

get_match_index(x, col_list[, str_check])

Returns the matching index.

get_params()

Returns the parameters of the model.

get_plotting_lib([class_name, chart, ...])

Returns the first available library (Plotly, Matplotlib, or Highcharts) to draw a specific graphic.

get_vertica_attributes([attr_name])

Returns the model Vertica attributes.

import_models(path[, schema, kind])

Imports machine learning models.

register(registered_name[, raise_error])

Registers the model and adds it to in-DB Model versioning environment with a status of 'under_review'.

set_params([parameters])

Sets the parameters of the model.

summarize()

Summarizes the model.

to_binary(path)

Exports the model to the Vertica Binary format.

to_pmml(path)

Exports the model to PMML.

to_python([return_proba, ...])

Returns the Python function needed for in-memory scoring without using built-in Vertica functions.

to_sql([X, return_proba, ...])

Returns the SQL code needed to deploy the model without using built-in Vertica functions.

to_tf(path)

Exports the model to the Frozen Graph format (TensorFlow).

transform(vdf, index, x[, pivot])

Transforms input data to tf-idf representation.

Attributes