Types of variable, it's graphical representation
By: Zubair Hossian
Tags: Machine Learning, Data Science, Statistics, Data Analysis, Data Engineering

1. Types of Variable

The data source is vast. We may find different types of data from different data sources. But it is important to know about the characteristics of the data. And variables define the characteristics of data. There are some parameters by which we can easily divide or categorize the variables. Basically there are two types of variables.

Types of Variables (photo by author)

Mainly two variable types are i) categorical and ii) numerical

i. Categorical: Categorical variables represent types of data which may be divided into groups. It is also known as qualitative variable.

Examples: Car Brand is a categorical variable that holds categorical data like Audi, Toyota, BMW, etc. Answer is a categorical variable that holds categorical data yes/no.

ii.Numerical: All the variables representing in number is known as the numerical variable. It is also known as quantitative variable. It can be a)discrete, or b)continuous.

a) Discrete: In a simple way, all the variables which contain countable data is known as the discrete variable.

Example: Variable -No of Children, SAT Score, Population etc., all of these variables contain discrete data.

b)Continuous: The variable which is uncountable is known as the continuous variable. It takes forever to count and the counting process will never end.

Example: Age is a continuous variable. But why? Suppose, your age is 25 years, 2 months, 10 days, 7 hours, 40 seconds, 44 milliseconds, 10 nanoseconds, 99 picoseconds…and so on. It will never end. Another example could be “Average ” variable. Take an average number of 1.232343542543245………. never ends.

A quick comparison of the variables.

Quantitative vs Qualitative (Photo by author)

2. Levels of Measurement

According to Wikipedia, “Level of measurement or scale of measure is a classification that describes the nature of the information within the values assigned to variables.”

Psychologist Stanley Smith Stevens developed the best-known classification with four levels, or scales, of measurement: i. nominal, ii.ordinal, iii. interval, iv. ratio

The nominal and ordinal scales are used to measure the qualitative/categorical variable. On the other hand, the interval and ratio scales are used to measure the quantitative/ numerical variables.

Level of measurement (photo by author)

i. Nominal: It is a Latin word that means name only. The nominal level represents the categories that can not be put in any order. This level represents only the individual category or name.

It only represents quality.

With a nominal scale, we may identify the difference between two individuals within the variable.

It doesn’t provide any idea about the size of the difference.

Example: Suppose, a variable represents Car Brand→ Audi, Toyota, BMW, etc. We can not order these names in any manner. So, such types of variables are in the level of nominal measurement.

ii. Ordinal: It is an ordered level or rank. It indicates the categorical variables that can be put in order. With this scale, we can determine the direction of the difference of a variable, but we can not determine the size of the difference.

Example: We have a variable named Height represents the height of the people with short, medium, and tall. We can easily order these values tall → medium → short. This order provides a good intuition about the direction of difference but it doesn’t provide any clue to identify how much the height differs from each other.

Ordinal Scales intuition (photo by Statistics How To)

iii. Interval: A variable that represents an equal interval among the levels and can be represented as a real number is known as an interval scale. It not only classifies and orders the measurements, but it also specifies that the distances between each interval on the scale are equivalent along the scale from low interval to high interval.

Example: Temperature is a variable where the interval between 10-degree centigrade, and 20-degree, 70-degree centigrade, and 80-degree are the same. Marks of the examination, height, time, etc. can be a good example of an interval scale.

iv. Ratio: It has all the properties like the interval scale. Additionally, it must satisfy a meaningful zero in the scale. Let’s make it clear.

Suppose, we are considering the body temperature in °C and °F scale. We found that two persons’ body temperature is 10°C and 20°C or 10°F and 20°F respectively. We can not say that the 2nd person’s body temperature is 2 times higher than the 1st one. Because 0°C and 0°F are not the true zero that means it doesn’t mean the absence of temperature. If you want to represent temperature as a ratio scale then we must consider the Kelvin scale as 0 degree Kelvin indicates the absence of temperature.

3. Data Representation with Python

Here, we will discuss the Bar chart, Pie Chart, and Pareto Diagram to represent the variables.

Categorical Variable Representation:

Suppose, we have a dataset of a car selling market and we are going to analyze the Car Brand variable. At first, we calculate the frequency of different Car Brand. It seems something like below.

Demo Car Brand frequency

Let’s convert it to a dataframe. As we are going to analyze with this demo data for graphical representation. The frequency represents the count of sold cars of each brand. You can do the same with any dataset. Let's convert the image with a pandas dataframe.

import pandas as pd

import matplotlib.pyplot as plt

df=pd.DataFrame({"Brand":['Audi',"BMW","Mercedes"],"Frequency":[124,98,113]})

Pandas dataframe

Compute relative frequency

Relative frequency indicates the distribution of individual frequency in percentage.

Relative frequency(%)=(Individual Frequency/total frequency)*100

df['Relative Frequency(%)']=

round((df.Frequency/sum(df.Frequency))*100,2)

We have added a relative frequency column in our main dataset.

dataframe with relative frequency column

Let’s draw a bar chart with the frequency.

Bar Chart:

import matplotlib.pyplot as plt fig,ax=plt.subplots()

var=plt.bar(df.Brand, df.Frequency, align='center', alpha=0.5) #this loop is used to represent frequency on each bar

for idx,rect in enumerate(var):

height = rect.get_height()

ax.text(rect.get_x() + rect.get_width()/2., 1.01*height,

df.Frequency[idx],

ha='center', va='bottom', rotation=0) #adding labels and title

plt.ylabel('Frequency')

plt.xlabel('Brand Name')

plt.title('Car Sales Statistics') plt.show()

This represents a bar chart of the frequency.

Bar chart represents the frequency

We can also represent the distribution of the frequency among different categorical data with Pie Chart.

Pie Chart:

fig = plt.figure()

ax = fig.add_axes([0,0,1,1])

ax.axis('equal') ax.pie(df.Frequency, labels = df.Brand,autopct='%1.2f%%')

plt.show()

Pie chart with relative frequency.

Pie chart with relative frequency

Pareto Diagram

The Pareto diagram is a special type of bar chart where the categories are shown in descending order of frequency, and a separate curve shows the cumulative frequency.

Dataframe with cumulative frequency.

Let’s draw the diagram with python.

from matplotlib.ticker import PercentFormatter df = df.sort_values(by='Frequency',ascending=False)

df["Cumulative Frequency"] = round(df["Frequency"].cumsum()/df["Frequency"].sum()*100,2)

fig, ax = plt.subplots(figsize=(10,5))

ax.bar(df.Brand, df["Frequency"], color="blue")

ax2 = ax.twinx()

ax2.plot(df.Brand, df["Cumulative Frequency"], color="red", marker="D", ms=7)

ax2.yaxis.set_major_formatter(PercentFormatter()) ax.tick_params(axis="y", colors="black")

ax2.tick_params(axis="y", colors="black")

ax2.set_ylim(0,105)

ax.set_ylim(0,130)

plt.show()

Pareto Diagram for the frequency table.

Left side of the diagram represents the frequency and the right side represents the cumulative frequency

So far so good. We are at the very end of the article.

Numerical Variable Representation:

We can also represent the numerical data with a bar chart, pie chart, and Pareto diagram. If we want to find the frequency of the numerical data, we may found a frequency of 1 for each data. But it is not feasible to represent the data. So, what can I do?

We can divide the numerical data into different intervals and then count the frequency of the data within the interval. Let’s try to have better intuition with a python dataframe.

df=pd.DataFrame({"data":[10,40,20,5,30,100,110,70,80,63,55,32,45,85,87,77,65,44,33,4,56,90,95,96]})

First 5 data of the dataframe.

If we want to count the frequency, most of the frequency of the data will be 1.

df['data'].value_counts()

This gives the frequency..

Frequency of the numerical data.

Now, if we want to represent the above frequency with bar chart, pie chart it doesn’t provide any information. So, we need to count the frequency within intervals. Suppose, we divide the data into 5 equal intervals.

d=df['data'].value_counts(bins = 5).sort_index()

It shows the distribution of the frequency below.

The left column indicates the interval and the right column indicates the frequency.

Well, we are able to apply the bar chart, pie chart, and Pareto diagram to represent the numerical variable’s frequency. Try to follow the above techniques to draw the chart and diagram for the numerical variable. It’s your task.