我在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)