The Wayback Machine - https://web.archive.org/web/20240708031756/https://www.geeksforgeeks.org/exploring-categorical-data/
Open In App

Exploring Categorical Data

Last Updated : 25 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Categorical Variable/Data (or Nominal variable): Such variables take on a fixed and limited number of possible values. For example – grades, gender, blood group type, etc. Also, in the case of categorical variables, the logical order is not the same as categorical data e.g. “one”, “two”, “three”. But the sorting of these variables uses logical order. For example, gender is a categorical variable and has categories – male and female and there is no intrinsic ordering to the categories. A purely categorical variable is one that simply allows you to assign categories, but you cannot clearly order the variables. Terms related to Variability Metrics : 

  • Mode : Most frequently occurring value in the given data Example- 
Data = ["Car", "Bat", "Bat", "Car", "Bat", "Bat", "Bat", "Bike"]
Mode = "Bat"
  • Expected Value : When working in machine learning, categories have to be associated with a numeric value, so as to give understanding to the machine. This gives an average value based on a category’s probability of occurrence i.e. Expected Value. It is calculated by –
-> Multiply each outcome by its probability of occurring.
-> Sum these values
  • So, it is the sum of values times their probability of occurrence often used to sum up factor variable levels.
  • Bar Charts : Frequency of each category plotted as bars. Loading Libraries – 

Python3




import matplotlib.pyplot as plt
import numpy as np


  • Data – 

Python3




label = ['Car', 'Bike', 'Truck', 'Cycle', 'Jeeps', 'Ambulance']
no_vehicle = [941, 854, 4595, 2125, 942, 509]


  • Indexing Data – 

Python3




index = np.arange(len(label))
 
print ("Total Labels : ", len(label))
print ("Indexing : ", index)


  • Output:
Total Labels :  6
Indexing :  [0 1 2 3 4 5]
  • Bar Graph – 

Python3




plt.bar(index, no_vehicle)
plt.xlabel('Type', fontsize = 15)
plt.ylabel('No of Vehicles', fontsize = 15)
plt.xticks(index, label, fontsize = 10, rotation = 30)
plt.title('Market Share for Each Genre 1995-2017')
 
plt.show()


  • Output:
  • Pie Charts : Frequency of each category plotted as pie or wedges. It is a circular graph, where the arc length of each slice is proportional to the quantity it represents. 

Python3




plt.figure(figsize =(8, 8))
plt.pie(no_vehicle, labels = label,
        startangle = 90, autopct ='%.1f %%')
plt.show()


  • Output:


Similar Reads

Exploring Data Distribution | Set 1
Whenever we work in data science and machine learning, our approach of handling the data and finding something useful out of it is based on the distribution of the data. Distribution means that how data can be present in different possible ways, the percentage of specific data, identifying the outliers. So, data distribution is the way of using gra
2 min read
Exploring Data Distribution | Set 2
Prerequisite: Exploring Data Distribution | Set 1Terms related to Exploration of Data Distribution -> Boxplot -> Frequency Table -> Histogram -> Density Plot Loading Libraries C/C++ Code import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt Loading Data C/C++ Code data = pd.read_csv("../data/s
2 min read
Exploring 3D Data Visualization with Mayavi in Python
Mayavi is an interactive 3D plotting library that can be used to visualize complex spatial datasets. Although matplotlib can also be used for simple 3D plotting, Mayavi uses a more complex engine (VTK), making Mayavi more suitable for displaying large and complex data. The Mayavi library is like a magic tool that helps us do lots of cool things in
7 min read
Exploring Geospatial Data with Pydeck: Advanced Visualizations
Geospatial data visualization is the key to unlocking insights from location-based information. Pydeck is an advanced Python library for building interactive geospatial data visualizations. It leverages [deck.gl](http://deck.gl/), a framework for exploring large datasets, to enable users to visualize complex patterns in geospatial data with customi
9 min read
Handling Categorical Data with Bokeh - Python
As a data scientist, you will often come across datasets with categorical data. Categorical data is a type of data that can be divided into distinct categories or groups. For example, a dataset might have a column with the categories "red", "green", and "blue". Handling categorical data can be challenging because it cannot be processed in the same
5 min read
Handling Categorical Data in Python
Categorical data is a set of predefined categories or groups an observation can fall into. Categorical data can be found everywhere. For instance, survey responses like marital status, profession, educational qualifications, etc. However, certain problems can arise with categorical data that must be dealt with before proceeding with any other task.
6 min read
Passing categorical data to Sklearn Decision Tree
Theoretically, decision trees are capable of handling numerical as well as categorical data, but, while implementing, we need to prepare the data for classification. There are two methods to handle the categorical data before training: one-hot encoding and label encoding. In this article, we understand how each method helps in converting categorica
5 min read
How to fit categorical data types for random forest classification?
Categorical variables are an essential component of many datasets, representing qualitative characteristics rather than numerical values. While random forest classification is a powerful machine-learning technique, it typically requires numerical input data. Therefore, encoding categorical variables into a suitable format is a crucial step in prepa
9 min read
Encoding Categorical Data in Sklearn
Categorical data is a common occurrence in many datasets, especially in fields like marketing, finance, and social sciences. Unlike numerical data, categorical data represents discrete values or categories, such as gender, country, or product type. Machine learning algorithms, however, require numerical input, making it essential to convert categor
6 min read
Sagemaker - Exploring Ground truth labeling | ML
Have you ever thought of doing machine learning completely from the scratch and don't know where to start? If yes, there is a place where you can enter by holding just the dataset in your hands and leaves the place with fully trained machine learning model which is ready to be deployed in the real-life scenario's. Amazon Sagemaker provides various
3 min read