> For the complete documentation index, see [llms.txt](https://digitalgarden.batamladen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://digitalgarden.batamladen.com/notes/machine-learning/feature-engineering/feature-extraction/tf-idf.md).

# TF-IDF

TF-IDF is a [**feature extraction**](/notes/machine-learning/feature-engineering/feature-extraction.md) method. TF-IDF is used on textual data to transform it into numbers, so that the model can understand it.\
TF-IDF stands for Term Frequency - Inverse Document Frequency

**Term frequency** represents the amount of times a word appears in a document. This can be represented like this:

```
tf(t,d) = count of t in d / number of words in d
```

**Document Frequency:** This tests the meaning of the text, which is very similar to TF, in the whole corpus collection. The only difference is that in document d, TF is the frequency counter for a term t, while df is the number of occurrences in the document set N of the term t.

```
df(t) = occurrence of t in documents
```

**Inverse Document Frequency:** Mainly, it tests how relevant the word is. The key aim of the search is to locate the appropriate records that fit the demand.

First, find the document frequency of a term t by counting the number of documents containing the term:

```
df(t) = N(t)
where
df(t) = Document frequency of a term t
N(t) = Number of documents containing the term t
```

Now let’s look at the definition of the frequency of the inverse paper. The IDF of the word is the number of documents in the corpus separated by the frequency of the text.

```
idf(t) = N/ df(t) = N/N(t)

```

We then take the logarithm (with base 2) of the inverse frequency of the paper. So the if of the term t becomes:

```
idf(t) = log(N/ df(t))
```

Usually, the tf-idf weight consists of two terms-

1. **Normalized Term Frequency (tf)**
2. **Inverse Document Frequency (idf)**

```
tf-idf(t, d) = tf(t, d) * idf(t)
```

***

## Syntax

{% tabs %}
{% tab title="Import" %}

```python
from sklearn.feature_extraction.text import TfidfVectorizer
```

{% endtab %}

{% tab title="Create obj" %}

```
tfidf = TfidfVectorizer()
```

{% endtab %}

{% tab title="Fit" %}

```python
# get tf-df values
X_new = tfidf.fit_transform(string)
```

{% endtab %}
{% endtabs %}

***

## Example


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://digitalgarden.batamladen.com/notes/machine-learning/feature-engineering/feature-extraction/tf-idf.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
