阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。
1. Text Preprocessing
1.1 Tools
NLTK (Natural Language Toolkit):
http://www.nltk.org/index.html
1.2 Tokenizing/Tokenization
Tokenizing is a process of splitting a text into individual words or sequences of words (N-grams).
nltk.word_tokenize(sent) # Split a sentence into words based on delimitersAfer doing document tokenization, we usually remove “stop words” from the result set. Stop words are words that don’t convey significant meaning, e.g. “a, the, an, in”.
It includes 153 stop words in
nltk.corpus.stopwords.words('english')分词、词表与n-gram: 对I like this product,unigram为I/like/this/product,bigram为I like/like this/this product,trigram为I like this/like this product。语料中的去重token组成词表;计数向量的列必须与同一词表对齐,不能按每篇句子的词位置对齐。
ngrams
知乎解释:
tokenization,也叫word segmentation,是一种操作,它按照特定需求,把文本切分成一个字符串序列(其元素一般称为token,或者叫词语)。一般来说,我们要求序列的元素有一定的意义,比如“text mining is time-consuming”需要处理成”text mining/ is/ time-consuming”,其中”text mining”表示”文本挖掘”。
如果我们把语料中所有的token做一个去重,就得到了一个词汇表,其中的每一个词语被称为type。
整理注: 停用词数量与语料包版本有关,不是固定153个;是否去停用词应由任务决定。
1.3 Stemming(advanced: lemmatization)
Stemming is a process of counting inflected forms of a word together. It helps us to reduce the number of unique vocabulary items that we need to track.
词干化处理就是把一些名词的复数去掉,动词的不同时态去掉等等类似的处理。
2. Term Frequency-Inverse Document Frequency (TF-IDF)
2.1 Why TF-IDF?
We need a weighting factor to reflect the importance of a word in a collection of documents (corpus).
2.2 Definition of TF-IDF
TF-IDF is the product of two statistics, term frequency and inverse document frequency.
TF-IDF(wordA_in_docB) = TF(wordA_in_docB) * IDF(wordA)- TF(wordA_in_docB) = count of wordA in docB / total words count in docB
- IDF(wordA) = log(total number of documents in the corpus / (number of documents where wordA appears + 1))
Example:
独立TF-IDF小例: 两篇文档red car与blue car,词表red,blue,car。计数为[1,0,1]与[0,1,1];非平滑IDF为[log2,log2,0],所以共现于每篇的car权重为0。若采用平滑+1约定,它不会为0;需声明公式。
整理注: TF-IDF有多种约定。scikit-learn默认平滑IDF为log((1+n)/(1+df))+1,还会作L2归一化;应把本节简化公式和库默认实现区分开。
3. K-means
K-means is an unsupervised learning algorithm. It aims to partition n observations into k (<= n) sets so as to minimize the within-cluster sum of squares.
3.1 Details of K-means Algorithm
Suppose we want to get K clusters from a set of observations (X1, X2, …, Xn)
K-means循环: 初始化K个中心 → 分配各样本到最近中心 → 用簇内均值更新中心 → 直到停止条件。多次初始化选择较小inertia;算法通常只保证收敛到局部解,不能保证全局最优。
for k-means multiple times (n_init=10):
reset centroids
while run times < max_iter and cur_tol > tol:
execute steps in feature above
...
calculate inertia value
Chose the k-means result with the best inertia value|a(k) - a(k-1)| < tolerance
inertia value = 计算cluster中所有的点到centroid的欧氏距离平方的和
4. Latent Dirichlet Allocation
4.1 What is LDA?
- LDA is a generative statistical model
- LDA is an example of a topic model
In LDA, each document is assumed to be characterized by a particular set of topics. - If observations words are collected into documents, LDA result means that:
- Each document is a mixture of a small number of topics
- Each word’s creation is attributable to one of the document’s topics
LDA生成视角: 每篇文档有主题比例theta,每个主题有词分布phi;先为词位抽主题,再从该主题抽词。主题名称是人根据高权重词解释的,不是模型自动理解出的真实标签。
4.2 Intuition of LDA
主题混合:
例如文档一半主题A、一半主题B,则词概率是两种主题词分布的加权和;这与把文档硬分到单一K-means簇不同。