title | author | date |
---|---|---|
Loading Features From Dictionaries |
Damian Mingle |
04/30/2018 |
from sklearn.feature_extraction import DictVectorizer
staff = [{'name': 'John Oxboro', 'age': 23.},
{'name': 'Regina Smith', 'age': 10.},
{'name': 'Ollie Dyson', 'age': 28.},
{'name': 'Ian McGrath', 'age': 48}]
# Create an object for our dictionary vectorizer
vec = DictVectorizer()
# Fit then transform the staff dictionary with vec, then output an array
vec.fit_transform(staff).toarray()
array([[23., 0., 1., 0., 0.],
[10., 0., 0., 0., 1.],
[28., 0., 0., 1., 0.],
[48., 1., 0., 0., 0.]])
# Get Feature Names
vec.get_feature_names()
['age',
'naem=Ian McGrath',
'name=John Oxboro',
'name=Ollie Dyson',
'name=Regina Smith']