使用sklearn对文档进行向量化的程序

1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-
"""
演示内容:文档的向量化
"""
from sklearn.feature_extraction.text import CountVectorizer
corpus = [
'Jobs was the chairman of Apple Inc., and he was very famous',
'I like to use apple computer',
'And I also like to eat apple'
]
1
2
3
4
5
6
#未经停用词过滤的文档向量化
vectorizer =CountVectorizer()
print(vectorizer.fit_transform(corpus).todense()) #转化为完整特征矩阵
print(vectorizer.vocabulary_)

print(" ")
[[0 1 1 1 0 0 1 1 1 1 0 1 1 0 0 1 2]
 [0 0 1 0 1 0 0 0 0 0 1 0 0 1 1 0 0]
 [1 1 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0]]
{'jobs': 9, 'was': 16, 'the': 12, 'chairman': 3, 'of': 11, 'apple': 2, 'inc': 8, 'and': 1, 'he': 7, 'very': 15, 'famous': 6, 'like': 10, 'to': 13, 'use': 14, 'computer': 4, 'also': 0, 'eat': 5}
1
2
3
4
5
6
7
8
9
#经过停用词过滤后的文档向量化
import nltk
nltk.download('stopwords')
stopwords = nltk.corpus.stopwords.words('english')
print (stopwords)
print(" ")
vectorizer =CountVectorizer(stop_words='english')
print("after stopwords removal: ", vectorizer.fit_transform(corpus).todense())
print("after stopwords removal: ", vectorizer.vocabulary_)
[nltk_data] Downloading package stopwords to /Users/maqi/nltk_data...


['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]

after stopwords removal:   [[1 1 0 0 1 1 0 0]
 [1 0 1 0 0 0 1 1]
 [1 0 0 1 0 0 1 0]]
after stopwords removal:   {'jobs': 5, 'chairman': 1, 'apple': 0, 'famous': 4, 'like': 6, 'use': 7, 'computer': 2, 'eat': 3}


[nltk_data]   Unzipping corpora/stopwords.zip.
1
2
3
4
5
6
print(" ")
#采用ngram模式进行文档向量化
vectorizer =CountVectorizer(ngram_range=(1,2))#表示从1-2,既包括unigram,也包括bigram
print("N-gram mode: ",vectorizer.fit_transform(corpus).todense()) #转化为完整特征矩阵
print(" ")
print("N-gram mode: ",vectorizer.vocabulary_)
N-gram mode:      [[0 0 1 0 1 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 1 1 2 1 1]
 [0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0]
 [1 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0]]

N-gram mode:          {'jobs': 18, 'was': 33, 'the': 24, 'chairman': 8, 'of': 22, 'apple': 5, 'inc': 16, 'and': 2, 'he': 14, 'very': 31, 'famous': 13, 'jobs was': 19, 'was the': 34, 'the chairman': 25, 'chairman of': 9, 'of apple': 23, 'apple inc': 7, 'inc and': 17, 'and he': 4, 'he was': 15, 'was very': 35, 'very famous': 32, 'like': 20, 'to': 26, 'use': 29, 'computer': 10, 'like to': 21, 'to use': 28, 'use apple': 30, 'apple computer': 6, 'also': 0, 'eat': 11, 'and also': 3, 'also like': 1, 'to eat': 27, 'eat apple': 12}