Title: Loading Features From Dictionaries
Slug: loading_features_from_dictionaries
Summary: Loading Features From Dictionaries
Date: 2016-11-01 12:00
Category: Machine Learning
Tags: Preprocessing Structured Data
Authors: Chris Albon

Preliminaries


In [1]:
from sklearn.feature_extraction import DictVectorizer

Create A Dictionary


In [2]:
staff = [{'name': 'Steve Miller', 'age': 33.},
         {'name': 'Lyndon Jones', 'age': 12.},
         {'name': 'Baxter Morth', 'age': 18.}]

Convert Dictionary To Feature Matrix


In [3]:
# Create an object for our dictionary vectorizer
vec = DictVectorizer()

In [4]:
# Fit then transform the staff dictionary with vec, then output an array
vec.fit_transform(staff).toarray()


Out[4]:
array([[ 33.,   0.,   0.,   1.],
       [ 12.,   0.,   1.,   0.],
       [ 18.,   1.,   0.,   0.]])

View Feature Names


In [5]:
# Get Feature Names
vec.get_feature_names()


Out[5]:
['age', 'name=Baxter Morth', 'name=Lyndon Jones', 'name=Steve Miller']