How to Transform Categorical Features to Numerical Features?

This Python code example will help you understand the process of transforming categorical features to numerical features. | ProjectPro

Many machine learning algorithms, such as linear regression, logistic regression, and support vector machines, require numerical input features. Transforming categorical features into numerical features allows these algorithms to process the data effectively. Most machine learning models perform better when trained on numerical features rather than categorical ones. Numeric representations allow the models to learn more complex patterns and relationships in the data, improving performance and predictive accuracy. So, this guide is here to help you understand the essential techniques to seamlessly transform categorical features into numeric form and understand data preprocessing with confidence and precision.

How to Convert Categorical Data to Numerical Data in Python? 

The conversion of categorical data to numerical form is a crucial preprocessing step in many machine learning tasks, enabling algorithms to process non-numeric information. Check out the steps below to understand the complete process using an example - 

Step 1 - Import the library

 

    import pandas as pd 

We have only imported pandas; this is required for the dataset.

Step 2 - Setting up the Data

We have created a dictionary and passed it through the pd.DataFrame to create a dataframe with columns "name," "episodes," and "gender."   

    data = {"name": ["Sheldon", "Penny", "Amy", "Penny", "Raj", "Sheldon"],

                "episodes": [42, 24, 31, 29, 37, 40],

                "gender": ["male", "female", "female", "female", "male", "male"]}

 

    df = pd.DataFrame(data, columns = ["name","episodes", "gender"])

    print(df)

Step 3 - Converting the Values

We can clearly observe that in the column "gender" there are two categories male and female, so we can assign numbers to each category like 1 to male and 2 to female. Now, we are using LabelEncoder. We have first fitted the feature and transformed it.

    le = preprocessing.LabelEncoder()

    le.fit(df["gender"])

    print(); print(list(le.classes_))

    print(); print(le.transform(df["gender"])) 

So the output comes as:

Feature Matrix:

   Feature 1  Feature 2  Feature 3  Feature 4  Feature 5  Feature 6  

0  -1.867524   1.745983   2.952435  -0.177492  -3.088648   1.762311

1   0.450144  -2.106431  -1.065847  -1.958231  -0.451780  -1.990662

2  -4.647836  -4.214226  -1.830341  -1.714825  -6.590249  -0.315993

3   1.958901  -1.313546   1.409145  -2.069271   1.508912   3.774923

4   2.001750   0.879350  -2.041154   1.917629  -0.760137   1.310228

 

   Feature 7  Feature 8  Feature 9  Feature 10

0  -0.195266   1.029769   2.814171    0.071059

1  -2.530104  -1.377802  -0.013353   -2.849859

2   2.780038  -3.325841  -4.008319    2.001941

3   5.012315  -5.772415  -0.818187   -0.392333

4   0.671990   1.444606  -1.731576   -0.378597

 

Target Class:

   TargetClass

0            1

1            2

2            1

3            0

4            0

 

Write a Python Program to Make Categorical Values in Numeric Format for a given dataset?

Let’s write a python program that uses the LabelEncoder from the scikit-learn library to convert categorical values in a dataset into numeric format:- 

 

Python program to make categorical values in Numeric format



Python program output

 

This program inputs a sample dataset, converts categorical columns ('Name', 'Gender', and 'City') into numeric format using the LabelEncoder, and then displays the modified DataFrame. You can replace the sample dataset with your own by reading it from a file or any other source.

How to Convert Numerical Data to Categorical Data in Python? 

Converting numerical data to categorical data in Python can be done using various methods, such as binning, one-hot encoding, label encoding, or custom functions. Here's a detailed explanation of each method:- 

 

  1. Binning 

Binning involves dividing numerical data into bins or intervals and assigning labels to each bin. This method is proper when converting continuous data into categorical data. 

 

Binning Example

 

  1. One-hot encoding

One-hot encoding creates binary columns for each category. It is commonly used when the categorical data is nominal (no inherent order).

 

One-hot Encoding Example

 

  1. Label Encoding 

Label encoding assigns a unique integer to each category. It is suitable when the categorical data has an ordinal relationship.

 

Label Encoding Example

Learn to Transform Categorical Features into Numerical Features with ProjectPro! 

These practical examples have helped you understand the importance of selecting the most suitable transformation method based on the nature of our data. While one-hot encoding preserves all categories but can lead to high dimensionality, label encoding simplifies the process, potentially introducing unintended ordinal relationships. Target encoding, conversely, showcases promise in capturing categorical-variable relationships with the target variable, though caution is warranted to prevent overfitting and data leakage. ProjectPro facilitates hands-on learning experiences crucial for mastering such concepts. Through ProjectPro, enthusiasts delve into real-world scenarios, honing their skills by tackling diverse datasets and employing various techniques. 


Download Materials


What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Learn to Build an End-to-End Machine Learning Pipeline - Part 3
This machine learning project integrates model monitoring, CI/CD practices and Amazon Sagemaker pipelines into the logistics-oriented machine learning pipeline to streamline workflow orchestration for scalable and reliable deployment of ML models in logistics.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.