How to convert string categorical variables into numerical variables using Label Encoder?
DATA MUNGING

How to convert string categorical variables into numerical variables using Label Encoder?

How to convert string categorical variables into numerical variables using Label Encoder?

This recipe helps you convert string categorical variables into numerical variables using Label Encoder

4
This data science python source code does the following: 1. Convert categorical features into numerical. 2. Implementation of Label Encoding function.
In [1]:
## How to convert string categorical variables into numerical variables using Label Encoder
def Kickstarter_Example_78():
    print()
    print(format('How to convert strings into numerical variables using Label Encoder','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd
    from sklearn.preprocessing import LabelEncoder

    # 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'])
    print(); print(df)

    # Create a function that converts all values of df['score'] into numbers
    def dummyEncode(df):
          columnsToEncode = list(df.select_dtypes(include=['category','object']))
          le = LabelEncoder()
          for feature in columnsToEncode:
              try:
                  df[feature] = le.fit_transform(df[feature])
              except:
                  print('Error encoding '+feature)
          return df
    df = dummyEncode(df)

    print(); print(df)

Kickstarter_Example_78()
*******How to convert strings into numerical variables using Label Encoder********

   patient  obs  treatment   score
0        1    1          0  strong
1        1    2          1    weak
2        1    3          0  normal
3        2    1          1    weak
4        2    2          0  strong

   patient  obs  treatment  score
0        1    1          0      1
1        1    2          1      2
2        1    3          0      0
3        2    1          1      2
4        2    2          0      1

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.

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.

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

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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.

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.

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.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

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.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.