我正在探索scikit-learn提供的不同功能提取类。阅读documentation我对DictVectorizer可以使用的内容并不是很了解?其他问题浮现在脑海中,例如DictVectorizer如何用于文本分类?,即该类如何帮助处理标记的文本数据?除了我在文档网页上已经准备好阅读的example之外,还能有人提供一些小例子吗?
答案 0 :(得分:8)
说你的特征空间是长度,宽度和高度,你有3个观察结果;即你测量长度,宽度和宽度。 3个物体的高度:
length width height
obs.1 1 0 2
obs.2 0 1 1
obs.3 3 2 1
另一种显示此方法的方法是使用词典列表:
[{'height': 1, 'length': 0, 'width': 1}, # obs.2
{'height': 2, 'length': 1, 'width': 0}, # obs.1
{'height': 1, 'length': 3, 'width': 2}] # obs.3
DictVectorizer
反其道而行之;即给定词典列表构建顶部框架:
>>> from sklearn.feature_extraction import DictVectorizer
>>> v = DictVectorizer(sparse=False)
>>> d = [{'height': 1, 'length': 0, 'width': 1},
... {'height': 2, 'length': 1, 'width': 0},
... {'height': 1, 'length': 3, 'width': 2}]
>>> v.fit_transform(d)
array([[ 1., 0., 1.], # obs.2
[ 2., 1., 0.], # obs.1
[ 1., 3., 2.]]) # obs.3
# height, len., width