How to convert a dictionary to a matrix or nArray in Python?
DATA MUNGING

How to convert a dictionary to a matrix or nArray in Python?

How to convert a dictionary to a matrix or nArray in Python?

This recipe helps you convert a dictionary to a matrix or nArray in Python

0
This python source code does the following: 1. Creates custom dictionary in python 2. Creates dictvectorizer object and converts dictionary into array 3. Extracts names of feature columns
In [1]:
## How to Convert a Dictionary into a Matrix or nArray
def Kickstarter_Example_5():
    print()
    print(format('How to Convert a Dictionary into a Matrix or ndArray',
                 '*^72'))

    # Load library
    from sklearn.feature_extraction import DictVectorizer

    # Our dictionary of data
    data_dict = [{'Apple': 2, 'Orange': 4},
                 {'Apple': 4, 'Orange': 3},
                 {'Apple': 1, 'Banana': 2},
                 {'Apple': 2, 'Banana': 2}]
    print()
    print(data_dict)

    # Create DictVectorizer object
    dictvectorizer = DictVectorizer(sparse=False)

    # Convert dictionary into feature matrix
    features = dictvectorizer.fit_transform(data_dict)

    # View feature matrix
    print()
    print(features)

    # View feature matrix column names
    dictvectorizer.get_feature_names()

Kickstarter_Example_5()
**********How to Convert a Dictionary into a Matrix or ndArray**********

[{'Apple': 2, 'Orange': 4}, {'Apple': 4, 'Orange': 3}, {'Apple': 1, 'Banana': 2}, {'Apple': 2, 'Banana': 2}]

[[2. 0. 4.]
 [4. 0. 3.]
 [1. 2. 0.]
 [2. 2. 0.]]

Relevant Projects

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Data Science Project-All State Insurance Claims Severity Prediction
Data science project in R to develop automated methods for predicting the cost and severity of insurance claims.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

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.

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.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.