【发布时间】:2018-01-23 23:50:37
【问题描述】:
我正在研究一个文本分类问题,从 RSS 提要解析新闻报道,我怀疑许多 HTML 元素和乱码被计为标记。我知道 Beautiful Soup 提供了清理 HTML 的方法,但我想尝试传入字典以更好地控制计数的标记。
这在概念上似乎很简单,但我得到了我不明白的结果。
from sklearn.feature_extraction.text import TfidfVectorizer
eng_dictionary = []
with open("C:\\Data\\words_alpha.txt") as f:
eng_dictionary = f.read().splitlines()
short_dic = []
short_dic.append(("short"))
short_dic.append(("story"))
stories = []
stories.append("This is a short story about the color red red red red blue blue blue i am in a car")
stories.append("This is a novel about the color blue red red red red i am in a boot")
stories.append("I like the color green, but prefer blue blue blue blue blue red red red red i am on a bike")
vec = TfidfVectorizer(decode_error=u'ignore', stop_words='english', analyzer='word', lowercase=True)
pos_vector = vec.fit_transform(stories).toarray()
print(vec.get_feature_names())
vec = TfidfVectorizer(decode_error=u'ignore', stop_words='english', analyzer='word', lowercase=True, vocabulary=short_dic)
pos_vector = vec.fit_transform(stories).toarray()
print(vec.get_feature_names())
vec = TfidfVectorizer(decode_error=u'ignore', stop_words='english', analyzer='word', lowercase=True, vocabulary=eng_dictionary)
pos_vector = vec.fit_transform(stories).toarray()
print(vec.get_feature_names())
程序的输出如下;
['bike', 'blue', 'boot', 'car', 'color', 'green', 'like', 'novel', 'prefer', 'red', 'short', 'story']
['short', 'story']
ptic', 'skeptical', 'skeptically', 'skepticalness', 'skepticism', 'skepticize', 'skepticized', 'skepticizing'...
第三次打印的输出一直持续,所以我故意将其缩短,但奇怪的是它从单词中间开始,正如我在上面显示的那样。前两个打印语句的结果对我来说很有意义;
- 没有词汇意味着特征是直接从语料库中构建的。
- 提供词汇表意味着特征是根据语料库和词汇表中的标记构建的
但是,第三次打印中显示的特征不是我的语料库的一部分,为什么会出现?
【问题讨论】:
标签: python scikit-learn tf-idf