How to load features from a Dictionary in python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to load features from a Dictionary in python?

How to load features from a Dictionary in python?

This recipe helps you load features from a Dictionary in python

0

Recipe Objective

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.

Step 1 - Import the library

from sklearn.feature_extraction import DictVectorizer

We have only imported DictVectorizer which is needed.

Step 2 - Creating a Dictionary

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"}]

Step 3 - Extracting Features

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"]

Relevant Projects

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.