Have to tried to load features? We think yes. But have to tried to do it from a dictionary in python?
So this is the recipe on how we can load features from a Dictionary in python.
from sklearn.feature_extraction import DictVectorizer
We have only imported DictVectorizer which is needed.
We have created a dictionary on which we will perform the operation.
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"}]
We are creating an object for DictVectorizer() then we are using this to fit and transform the feature employee to array and finally printing the feature.
vec = DictVectorizer()
print("Feature Matrix: "); print(vec.fit_transform(employee).toarray())
print("Feature Name: "); print(vec.get_feature_names())
So the output comes as
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"]