How to convert Categorical features to Numerical Features in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to convert Categorical features to Numerical Features in Python?

How to convert Categorical features to Numerical Features in Python?

This recipe helps you convert Categorical features to Numerical Features in Python

0
This python source code does the following : 1. Creates custom data dictionary and converts it into pandas dataframe 2. Applies label encoder to convert categorical into numerical 3. Uses "inverse_transform" function to reverse encode the numerical into categorical
In [1]:
## How to convert Categorical features to Numerical Features in Python 
def Kickstarter_Example_26():
    print()
    print(format('How to convert Categorical features to Numerical Features in Python',
                 '*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # Load libraries
    from sklearn import preprocessing
    import pandas as pd

    #Create DataFrame
    raw_data = {'patient': [1, 1, 1, 2, 2],
                'obs': [1, 2, 3, 1, 2],
                'treatment': [0, 1, 0, 1, 0],
                'score': ['strong', 'weak', 'normal', 'weak', 'strong']}
    df = pd.DataFrame(raw_data, columns = ['patient', 'obs', 'treatment', 'score'])

    # Fit The Label Encoder
    # Create a label (category) encoder object
    le = preprocessing.LabelEncoder()

    # Fit the encoder to the pandas column
    le.fit(df['score'])

    # View The Labels
    print(); print(list(le.classes_))

    # Transform Categories Into Integers
    # Apply the fitted encoder to the pandas column
    print(); print(le.transform(df['score']))

    # Transform Integers Into Categories
    # Convert some integers into their category names
    print(); print(list(le.inverse_transform([2, 2, 1, 0, 1, 2])))

Kickstarter_Example_26()
*******How to convert Categorical features to Numerical Features in Python********

['normal', 'strong', 'weak']

[1 2 0 2 1]

['weak', 'weak', 'strong', 'normal', 'strong', 'weak']

Relevant Projects

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

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.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

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.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

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