<jupyter_start><jupyter_text>Count Based Vectorizer<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import (
    TfidfVectorizer, CountVectorizer, TfidfTransformer
)<jupyter_output><empty_output><jupyter_text>References https://en.wikipedia.org/wiki/Tf%E2%80%93idf  Count Vectorizerfrequency based on the set of words<jupyter_code>corpus = [
    'this is the first document',
    'this document is the second document',
    'and this is the third one'
]
count_vec = CountVectorizer()
count_vec.fit(corpus)
count_vec.get_feature_names()
count_data = count_vec.transform(corpus).toarray()
count_data
pd.DataFrame(
    data=count_data,
    columns=count_vec.get_feature_names()
)<jupyter_output><empty_output><jupyter_text>So the first line is `this is the first document`. and then the first vector is reflecting as document is 1, first is 1, is is 1 etc.  Term Frequency TransformerNumber of times term t, appear in the document d.\begin{align}    tf_{t,d} &= \frac{n_{t,d}}{\sum_k{n_{k,d}}} \end{align}<jupyter_code>tf_transformer = TfidfTransformer(use_idf=False)
tf_transformer.fit(count_data)
tf_data = tf_transformer.transform(count_data).toarray()
tf_data
pd.DataFrame(
    data=tf_data,
    columns=count_vec.get_feature_names()
)<jupyter_output><empty_output><jupyter_text>Inverse Document Frequency Transformer> `Document Frequency` : number of ducuments that the term appears / number of ducuments\begin{align}    probability = df_{t,d,D} &= \frac{d \in D : t \in d}{D} \end{align}The log of the number of documents D divided by the number of documents that contain the word t. Inverse data frequency determines the weight of rare words across all documents in the corpus.\begin{align}    idf &= -log(p)\\    idf &= log(\frac{1}{p})\\    idf_{t,d,D} &= log(\frac{D}{d \in D : t \in d})\end{align}> document d is in all the documents D, and term t is in the document d.<jupyter_code>tfidf_transformer = TfidfTransformer()
tfidf_transformer.fit(count_data)
tfidf_data = tfidf_transformer.transform(count_data).toarray()
tfidf_data
pd.DataFrame(
    data=tfidf_data,
    columns=count_vec.get_feature_names()
)<jupyter_output><empty_output><jupyter_text>TFIDF VectorizerCount Vectorizer --> Tfidf Transformer = Tfidf Vectorizer$tfidf_{t,d,D} = tf_{t,d}.idf_{t,d,D}$<jupyter_code>tfidf_vec = TfidfVectorizer()
tfidf_vec.fit(corpus)
tfidf_data = tfidf_vec.transform(corpus).toarray()
tfidf_vec.get_feature_names()
pd.DataFrame(
    data=tfidf_data,
    columns=tfidf_vec.get_feature_names()
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11081185071181224, 'fraction_numerical': 0.0034628703347441324, 'mean_word_length': 6.046070460704607, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '549113', 'license': 'MIT', 'n_tokens_mistral': 938, 'n_tokens_neox': 921, 'n_words': 218, 'path': 'Concepts/tfidf.ipynb', 'repo_name': 'NishantBaheti/NatualLanguageProcessing', 'size': '2599'}