Before doing a simple Chinese text categorization system, using naive Bayesian algorithm, now review and give detailed each step. If you have source code requirements, please go to Https://github.com/chenfei0328/BayesProject
First, text preprocessing
1. Format problems, such as the deletion of space, the deletion of line-changing characters.
2.jieba participle Https://github.com/fxsjy/jieba
Building a vector-space model
1. Load training set, each document as a row of data, n documents are n-dimensional.
2. Generate dictionaries, add each occurrence of a word in the training set to the dictionary, and delete the words that exist in the inactive vocabulary (such as "").
3. Establish the TF-IDF model.
Algorithm principle:
Instance:
The dictionary = "I, yes, China, man, ah"
Text 1 = "I, yes, man"
Text 2 = "China, man, yes, man"
A. Establishing a TF matrix (representing the word frequency information for each document)
(1) Project each document into a dictionary, recording the number of occurrences of the word at the index of the word.
Text 1 = "1, 1, 0, 1, 0"
Text 2 = "0, 1, 1, 2, 0" (the "person" in the dictionary appears two times in the text)
(2) Next, the integral type counting way is normalized, can avoid the sentence length inconsistency problem.
Text 1 = "1/3, 1/3, 0, 1/3, 0"
Text 2 = "0, 1/4, 1/4, 2/4, 0"
B. Establishment of the IDF vector (representing the word frequency information in the whole bag)
(1) The document frequency of the entry.
"1/2, 2/2, 1/2, 2/2, 0/2"
To prevent "0" from appearing in ln expressions, the numerator denominator is added "1" (or treated with Laplace's principle).
"2/3, 3/3, 2/3, 3/3, 1/3"
(2) The weight of IDF.
"Ln (3/2), ln (3/3), ln (3/2), ln (3/3), ln (3/1)"
C. Establishment of the TF-IDF matrix
Each row of the TF matrix is multiplied by the IDF weight vector.
Text 1 "1/3ln (3/2), 1/3LN (3/3), 0, 1/3ln (3/3), 0"
Text 2 "0, 1/4LN (3/3), 1/4ln (3/2), 2/3ln (3/3), 0"
The word that has the largest number in each text is the most representative word in the document and becomes the keyword.
Three, Bayes classifier
1. Naive Bayesian algorithm formula derivation (pure handwriting, if wrong please forgive me)
2. Generate category labels to count the probability of a document belonging to a certain class
{0:0.1,1:0.9} (key is category label, the key value is the proportion of the number of documents in the Class)
3. Generate Conditional probability matrix
A. Add the document vectors belonging to the same class in the TFIDF matrix, form the M*n matrix, M represents the class number, and N represents the number of dictionaries.
"" "1,2,0"
"0,1,1" "
B. Dividing the weight of each word by the total weight of all the words in the class
"" "1/3,2/3,0"
"0,1/2,1/2" "
Four, the application
1. The test document after Jieba participle map to the dictionary to form a vector, if there is a test word in the dictionary does not exist, you can delete the word (in the dictionary to find, if found, the word is the index position +1, if not, then jump to the next word).
2. Calculate max{P (x1|yi) * p (x2|yi) * ... * p (xn|yi) * P (Yi)}, the output of the maximum value of Yi, that is, category.
A. Since P (xn|yi) may be equal to 0 (which can be solved in other steps of the algorithm), the output will always be equal to 0, which does not meet the requirements, so I set an ln function on the outer layer of the shell.
The equation becomes: max{ln (P (x1|yi) * p (x2|yi) * ... * p (xn|yi)) * P (Yi)}
Equivalent to: max{(ln (P (x1|yi)) + ln (P (x2|yi)) + ... + ln (P (xn|yi))) * P (Yi)}
B. Because the test data may include n a certain characteristic, therefore should multiply the conditional probability to this n weight.
The equation becomes: max{(ln (n1 * p (x1|yi)) + ln (N2 * p (x2|yi)) + ... + ln (NN * p (xn|yi))) * P (Yi)}
C. If n=0, it will exceed the LN function definition field and take +1 methods.
The equation becomes: max{(ln (n1 * p (X1|yi) + 1) + ln (N2 * p (x2|yi) + 1) + ... + ln (NN * p (xn|yi) + 1)) * P (Yi)}