Loading...

vastorbit.machine_learning.vast.feature_extraction.text.TfidfVectorizer

class vastorbit.machine_learning.vast.feature_extraction.text.TfidfVectorizer(name: str = 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.

Variables:
  • created (Many attributes are)

  • phase. (during the fitting)

  • 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_ (VastFrame) – The IDF table which is computed based on the relation used for the fitting process.

  • tf_ (VastFrame) – 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 vastorbit:

import vastorbit as vo

Hint

By assigning an alias to vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit 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 vastorbit 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 VastFrame:

data = vo.VastFrame(
    {
        "id": (list(range(1,len(documents)+1))),
        "docs": documents,
    }
)

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

from vastorbit.machine_learning.vast.feature_extraction.text import TfidfVectorizer

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

We apply the transform function to obtain the idf representation.

model.transform(
    vdf = data,
    index = "id",
    x = "docs",
)
123
row_id
Integer
Abc
word
Varchar(4049)
123
tf
Double
123
idf
Double
123
tfidf
Double
16python1.02.60943791243410050.3313965871579751
26is1.01.22314355131420970.1553382805462904
36a1.01.35667494393873240.17229666364609753
46popular1.02.60943791243410050.3313965871579751
56programming1.02.60943791243410050.3313965871579751
66language2.02.2039728043259360.559805667007647
76for1.01.51082562376599070.19187368019798118
86natural1.02.2039728043259360.2799028335038235
96processing1.02.2039728043259360.2799028335038235
106tasks1.02.60943791243410050.3313965871579751
111natural1.02.2039728043259360.30420571107707617
121language1.02.2039728043259360.30420571107707617
131processing1.02.2039728043259360.30420571107707617
141is1.01.22314355131420970.16882570104610647
151a1.01.35667494393873240.18725651478606037
161field1.02.60943791243410050.3601704676688489
171of1.01.9162907318741550.2644980843574777
181study1.02.60943791243410050.3601704676688489
191in1.01.9162907318741550.2644980843574777
201artificial1.02.60943791243410050.3601704676688489
211intelligence1.02.60943791243410050.3601704676688489
227tfidf1.02.2039728043259360.3449460916969199
237is1.01.22314355131420970.19143547814291995
247a1.01.35667494393873240.21233461624220487
257technique1.02.2039728043259360.3449460916969199
267widely1.02.60943791243410050.4084058603868319
277used1.01.9162907318741550.2999206692648759
287in1.01.9162907318741550.2999206692648759
297information1.02.60943791243410050.4084058603868319
307retrieval1.02.60943791243410050.4084058603868319
318an1.02.2039728043259360.2658587251660463
328algorithm1.02.60943791243410050.31476878273543557
338is1.01.22314355131420970.14754419138439442
348a2.01.35667494393873240.3273033772034955
358set1.02.60943791243410050.31476878273543557
368of1.01.9162907318741550.231156487059915
378instructions1.02.60943791243410050.31476878273543557
388designed1.02.60943791243410050.31476878273543557
398to1.01.9162907318741550.231156487059915
408perform1.02.60943791243410050.31476878273543557
418specific1.02.60943791243410050.31476878273543557
428task1.02.60943791243410050.31476878273543557
439data2.02.2039728043259360.5318504429864452
449preprocessing1.02.60943791243410050.31484751239890096
459is1.01.22314355131420970.1475810949948256
469an1.02.2039728043259360.2659252214932226
479important1.02.60943791243410050.31484751239890096
489step1.02.60943791243410050.31484751239890096
499in1.01.9162907318741550.2312143036968633
509preparing1.02.60943791243410050.31484751239890096
519for1.01.51082562376599070.18229201279118498
529machine1.02.2039728043259360.2659252214932226
539learning1.02.2039728043259360.2659252214932226
543machine1.02.2039728043259360.2907046445125331
553learning1.02.2039728043259360.2907046445125331
563algorithms1.02.60943791243410050.344185608471555
573can1.02.60943791243410050.344185608471555
583be1.02.60943791243410050.344185608471555
593applied1.02.60943791243410050.344185608471555
603to1.01.9162907318741550.25275929671124686
613text1.02.2039728043259360.2907046445125331
623data1.02.2039728043259360.2907046445125331
633for1.01.51082562376599070.19927833275222503
643classification1.02.2039728043259360.2907046445125331
652tfidf1.02.2039728043259360.3434249460827697
662stands1.02.60943791243410050.40660486945440955
672for1.01.51082562376599070.2354181536922318
682term1.02.60943791243410050.40660486945440955
692frequencyinverse1.02.60943791243410050.40660486945440955
702document1.02.60943791243410050.40660486945440955
712frequency1.02.60943791243410050.40660486945440955
724the1.02.60943791243410050.2990194911597364
734201.02.60943791243410050.2990194911597364
744newsgroups1.02.60943791243410050.2990194911597364
754dataset1.02.60943791243410050.2990194911597364
764is1.01.22314355131420970.14016189486115033
774a1.01.35667494393873240.1554634618714917
784collection1.02.60943791243410050.2990194911597364
794of1.01.9162907318741550.21959069301044337
804text2.02.2039728043259360.5051132455301
814documents1.02.2039728043259360.25255662276505
824used1.01.9162907318741550.21959069301044337
834for1.01.51082562376599070.17312782461575696
844classification1.02.2039728043259360.25255662276505
855clustering1.02.60943791243410050.37808950386861345
865is1.01.22314355131420970.17722504002599546
875a1.01.35667494393873240.19657281517240482
885technique1.02.2039728043259360.31934041433091737
895used1.01.9162907318741550.27765727194730444
905to1.01.9162907318741550.27765727194730444
915group1.02.60943791243410050.37808950386861345
925similar1.02.60943791243410050.37808950386861345
935documents1.02.2039728043259360.31934041433091737
945together1.02.60943791243410050.37808950386861345
Rows: 1-94 | Columns: 5

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 = "docs",
)
model.transform(
    vdf = data,
    index = "id",
    x = "docs",
)
123
row_id
Integer
Abc
word
Varchar(4049)
123
tf
Double
123
idf
Double
123
tfidf
Double
17tfidf1.02.2039728043259360.3599704622538046
27technique1.02.2039728043259360.3599704622538046
37widely1.02.60943791243410050.4261942659718019
47used1.01.9162907318741550.31298392575964307
57in1.01.9162907318741550.31298392575964307
67information1.02.60943791243410050.4261942659718019
77retrieval1.02.60943791243410050.4261942659718019
86python1.02.60943791243410050.3475186445132981
96popular1.02.60943791243410050.3475186445132981
106programming1.02.60943791243410050.3475186445132981
116language2.02.2039728043259360.5870395596337948
126natural1.02.2039728043259360.2935197798168974
136processing1.02.2039728043259360.2935197798168974
146tasks1.02.60943791243410050.3475186445132981
154newsgroups1.02.60943791243410050.3107020913110948
164dataset1.02.60943791243410050.3107020913110948
174collection1.02.60943791243410050.3107020913110948
184of1.01.9162907318741550.22817003428833435
194text2.02.2039728043259360.5248478656907996
204documents1.02.2039728043259360.2624239328453998
214used1.01.9162907318741550.22817003428833435
224classification1.02.2039728043259360.2624239328453998
234the1.02.60943791243410050.3107020913110948
244201.02.60943791243410050.3107020913110948
255clustering1.02.60943791243410050.39207100431193614
265technique1.02.2039728043259360.33114941219743876
275used1.01.9162907318741550.28792485470509516
285to1.01.9162907318741550.28792485470509516
295group1.02.60943791243410050.39207100431193614
305similar1.02.60943791243410050.39207100431193614
315documents1.02.2039728043259360.33114941219743876
325together1.02.60943791243410050.39207100431193614
333machine1.02.2039728043259360.29665466794755513
343learning1.02.2039728043259360.29665466794755513
353algorithms1.02.60943791243410050.35123025834234434
363can1.02.60943791243410050.35123025834234434
373be1.02.60943791243410050.35123025834234434
383applied1.02.60943791243410050.35123025834234434
393to1.01.9162907318741550.2579326703302803
403text1.02.2039728043259360.29665466794755513
413data1.02.2039728043259360.29665466794755513
423classification1.02.2039728043259360.29665466794755513
431natural1.02.2039728043259360.31436127094398053
441language1.02.2039728043259360.31436127094398053
451processing1.02.2039728043259360.31436127094398053
461field1.02.60943791243410050.3721943469502448
471of1.01.9162907318741550.27332805050394926
481study1.02.60943791243410050.3721943469502448
491in1.01.9162907318741550.27332805050394926
501artificial1.02.60943791243410050.3721943469502448
511intelligence1.02.60943791243410050.3721943469502448
529data2.02.2039728043259360.5471117563820143
539preprocessing1.02.60943791243410050.3238819818101776
549an1.02.2039728043259360.27355587819100713
559important1.02.60943791243410050.3238819818101776
569step1.02.60943791243410050.3238819818101776
579in1.01.9162907318741550.23784893942348248
589preparing1.02.60943791243410050.3238819818101776
599machine1.02.2039728043259360.27355587819100713
609learning1.02.2039728043259360.27355587819100713
612tfidf1.02.2039728043259360.35335631585664906
622stands1.02.60943791243410050.4183633143678389
632term1.02.60943791243410050.4183633143678389
642frequencyinverse1.02.60943791243410050.4183633143678389
652document1.02.60943791243410050.4183633143678389
662frequency1.02.60943791243410050.4183633143678389
678an1.02.2039728043259360.2848499257850261
688algorithm1.02.60943791243410050.337253796525324
698set1.02.60943791243410050.337253796525324
708of1.01.9162907318741550.24766878778426263
718instructions1.02.60943791243410050.337253796525324
728designed1.02.60943791243410050.337253796525324
738to1.01.9162907318741550.24766878778426263
748perform1.02.60943791243410050.337253796525324
758specific1.02.60943791243410050.337253796525324
768task1.02.60943791243410050.337253796525324
Rows: 1-76 | Columns: 5

Notice how we have removed the unnecessary words.

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

model.stop_words_

See also

VastColumn.pivot() : pivot VastFrame.
__init__(name: str = 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

Methods

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

contour([nbins, chart])

Draws the model's contour plot.

deploySQL([X])

Returns the SQL code needed to deploy the model.

drop()

Drops the model from the VAST 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) to draw a specific graphic.

import_models(path[, schema, kind])

Imports machine learning models.

set_params([parameters])

Sets the parameters of the model.

summarize()

Summarizes the model.

to_binary(path)

Exports the model to the VAST Binary format.

to_python([return_proba, ...])

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

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

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

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

Transforms input data to tf-idf representation.

Attributes