Recipe: How to load features from a Dictionary in python?
DATA MUNGING DATA CLEANING PYTHON

How to load features from a Dictionary in python?

This recipe helps you load features from a Dictionary in python
In [1]:
## How to load features from a Dictionary in python
def Kickstarter_Example_20():
    print()
    print(format('How to load features from a Dictionary in python', '*^72'))

    # Load library
    from sklearn.feature_extraction import DictVectorizer

    # Create A Dictionary
    employee = [{'name': 'Steve Miller', 'age': 33., 'dept': 'Analytics'},
                {'name': 'Lyndon Jones', 'age': 42., 'dept': 'Finance'},
                {'name': 'Baxter Morth', 'age': 37., 'dept': 'Marketing'},
                {'name': 'Mathew Scott', 'age': 32., 'dept': 'Business'}]

    # Convert Dictionary To Feature Matrix
    vec = DictVectorizer()

    # Fit then transform the dictionary with vec, then output an array
    print();
    print("Feature Matrix: "); print(vec.fit_transform(employee).toarray())

    # View Feature Names
    print()
    print("Feature Name: "); print(vec.get_feature_names())

Kickstarter_Example_20()
************How to load features from a Dictionary in python************

Feature Matrix:
[[33.  1.  0.  0.  0.  0.  0.  0.  1.]
 [42.  0.  0.  1.  0.  0.  1.  0.  0.]
 [37.  0.  0.  0.  1.  1.  0.  0.  0.]
 [32.  0.  1.  0.  0.  0.  0.  1.  0.]]

Feature Name:
['age', 'dept=Analytics', 'dept=Business', 'dept=Finance', 'dept=Marketing', 'name=Baxter Morth', 'name=Lyndon Jones', 'name=Mathew Scott', 'name=Steve Miller']


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from HvH
1 developer from The Hindu
1 developer from Altimetrik
1 developer from KPMG
1 developer from Vodafone
1 developer from ANAC
1 developer from LTI
1 developer from YASH Technologies
1 developer from Embicon
1 developer from MudraCircle