sklearn.feature_extraction.TfidfTransformer?
class sklearn.feature_extraction.text.TfidfTransformer(*, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False)
將計數矩陣轉換為標準化的tf
或tf-idf
表示
Tf
表示詞頻,Tf -idf
表示詞頻乘以反文檔頻率。這是信息檢索中常用的術語加權方案,在文檔分類中也有很好的應用。
使用tf-idf
代替原始的目標頻率發生的令牌在給定文檔縮減的影響令牌經常發生在一個給定的語料庫,因此經驗不如特性發生在信息的一小部分訓練語料庫。
公式用于計算文檔的tf-idf
任期t d在文檔集 tf-idf(t, d) = tf(t, d) * idf(t)和計算 idf(t) = log [ n / df(t) ] + 1 (如果smooth_idf = False
),其中n是文檔的文檔集的總數和df的文檔頻率t (t);文檔頻率是文檔集中包含t項的文檔數量。在上式中對idf加“1”的效果是idf為零的項,即不會完全忽略在訓練集中所有文檔中出現的術語。(請注意,上面的idf公式不同于標準教科書中定義的idf(t) = log [ n / (df(t) + 1) ]。
如果smooth_idf = True
(默認),不斷“1”添加到分子和分母idf的如果一個額外的文檔被認為包含集合中的每一項完全一次,防止零分歧:idf(t) = log [ (1 + n) / (1 + df(t)) ] + 1
。
此外,計算tf和idf的公式取決于參數設置,對應于IR中使用的智能標記如下:
Tf默認為“n”(自然),當sublinear_tf=True
時為“l”(對數)。給定use_idf時,Idf為“t”,否則為“n”(none)。當norm='l2'
時歸一化為" c " (cos)
,當norm=' none '
時為" n " (none)
。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
norm | {‘l1’, ‘l2’}, default=’l2’ 每個輸出行都有單位范數,可以是:* ' l2 ':向量元素的平方和為1。當應用l2范數時,兩個向量之間的余弦相似度是它們的點積。*‘l1’:向量元素的絕對值之和為1。看到 preprocessing.normalize |
use_idf | bool, default=True 使inverse-document-frequency權重。 |
smooth_idf | bool, default=True 通過在文檔頻率上增加1來平滑idf權重,就好像在一個額外的文檔中只包含集合中的每一個詞一樣。防止零分歧。 |
sublinear_tf | bool, default=False 應用次線性tf縮放,即將tf替換為1 + log(tf)。 |
屬性 | 說明 |
---|---|
idf_ | array of shape (n_features) 反文檔頻率(IDF)向量;只有在 use_idf 為真時才定義。新版本0.20。 |
參考文獻
Yates2011
R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 68-74.
MRS2008
C.D. Manning, P. Raghavan and H. Schütze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 118-120.
示例
>>> from sklearn.feature_extraction.text import TfidfTransformer
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> from sklearn.pipeline import Pipeline
>>> import numpy as np
>>> corpus = ['this is the first document',
... 'this document is the second document',
... 'and this is the third one',
... 'is this the first document']
>>> vocabulary = ['this', 'document', 'first', 'is', 'second', 'the',
... 'and', 'one']
>>> pipe = Pipeline([('count', CountVectorizer(vocabulary=vocabulary)),
... ('tfid', TfidfTransformer())]).fit(corpus)
>>> pipe['count'].transform(corpus).toarray()
array([[1, 1, 1, 1, 0, 1, 0, 0],
[1, 2, 0, 1, 1, 1, 0, 0],
[1, 0, 0, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 0, 0]])
>>> pipe['tfid'].idf_
array([1. , 1.22314355, 1.51082562, 1. , 1.91629073,
1. , 1.91629073, 1.91629073])
>>> pipe.transform(corpus).shape
(4, 8)
方法
方法 | 說明 |
---|---|
fit (X[, y]) |
學習idf向量(全局項權重)。 |
fit_transform (X[, y]) |
適合數據,然后轉換它。 |
get_params ([deep]) |
獲取這個估計器的參數。 |
set_params (**params) |
設置的參數估計量。 |
transform (X[, copy]) |
將計數矩陣轉換為tf或tf-idf表示 |
__init__(*, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False)
初始化self. See 請參閱help(type(self))以獲得準確的說明。
fit(X, y=None)
學習idf向量(全局項權重)。
fit_transform(X, y=None, **fit_params)
擬合數據,然后轉換它。
使用可選參數fit_params將transformer與X和y匹配,并返回X的轉換版本。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix, dataframe} of shape (n_samples, n_features) |
y | ndarray of shape (n_samples,), default=None 目標的價值。 |
**fit_params | dict 其他合適的參數。 |
返回值 | 說明 |
---|---|
X_new | ndarray array of shape (n_samples, n_features_new) 改變數組。 |
get_params(deep=True)
獲取這個估計器的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為真,將返回此估計器的參數以及包含的作為估計器的子對象。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到它們的值。 |
set_params(**params)
設置這個估計器的參數。
該方法適用于簡單估計量和嵌套對象。后者具有形式為<component>_<parameter>
的參數,這樣就讓更新嵌套對象的每個樣本成為了可能。
參數 | 說明 |
---|---|
**params | dict 估計器參數。 |
返回值 | 說明 |
---|---|
self | object 估計器實例。 |
transform(X, copy=True)
將計數矩陣轉換為tf或tf-idf表示
參數 | 說明 |
---|---|
X | sparse matrix of (n_samples, n_features) 術語/標記計數的矩陣 |
返回值 | 說明 |
---|---|
vectors | sparse matrix of shape (n_samples, n_features) |