小编fi1*_*1er的帖子

如何使用sklearn DictVectorizer对列表进行矢量化

我在sklearn docs网站上找到了下一个例子:

>>> measurements = [
...     {'city': 'Dubai', 'temperature': 33.},
...     {'city': 'London', 'temperature': 12.},
...     {'city': 'San Fransisco', 'temperature': 18.},
... ]

>>> from sklearn.feature_extraction import DictVectorizer
>>> vec = DictVectorizer()

>>> vec.fit_transform(measurements).toarray()
array([[  1.,   0.,   0.,  33.],
       [  0.,   1.,   0.,  12.],
       [  0.,   0.,   1.,  18.]])

>>> vec.get_feature_names()
['city=Dubai', 'city=London', 'city=San Fransisco', 'temperature']
Run Code Online (Sandbox Code Playgroud)

我需要矢量化dict,看起来像:

>>> measurements = [
...     {'city': ['Dubai','London'], 'temperature': 33.},
...     {'city': ['London','San Fransisco'], 'temperature': 12.},
...     {'city': ['San Fransisco'], 'temperature': 18.},
... …
Run Code Online (Sandbox Code Playgroud)

python scikit-learn

7
推荐指数
1
解决办法
7515
查看次数

标签 统计

python ×1

scikit-learn ×1