How to convert Categorical features to Numerical Features in Python?
DATA MUNGING

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

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

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.

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

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

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.

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.

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.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.