Types of variable, it's graphical representation | Towards Data Science

“You can have data without information, but you cannot have information without data.” — Daniel Keys Moran
Data is the raw material for the information age. We can not think of modern technology without data. If we have data we are able to get tons of information about any incidence or space. Today’s world has quite a hunger for data. It can be compared with oil for modern technologies.
But it does not make any sense if you don’t know the data. Firstly, we must be familiar with the data we are exploring. If we don’t know the data well, our system or analysis model building on top of the data will be ended up with a useless analysis or system. For this reason, we need to be familiar with data types and their representation techniques.
The parameter holds the data is known as a variable in statistics. One dataset may describe the stock market, others may describe the population, employee data, and so on. It varies with the system. The properties with varying values are variables.
This article will help you in this regard. All the variable types and possible graphical representations of the variables will be shown here. This would be the best article for you if you are interested in it. Let’s get started.
Types of variableLevels of MeasurementData Representation (Bar Chart, Pie Chart, Pareto Diagram)with python
Types of variable
Levels of Measurement
Data Representation (Bar Chart, Pie Chart, Pareto Diagram)with python
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.
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.
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.
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.
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.
Here, we will discuss the Bar chart, Pie Chart, and Pareto Diagram to represent the variables.
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.
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 pdimport matplotlib.pyplot as pltdf=pd.DataFrame({"Brand":['Audi',"BMW","Mercedes"],"Frequency":[124,98,113]})
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.
Let’s draw a bar chart with the frequency.
Bar Chart:
import matplotlib.pyplot as pltfig,ax=plt.subplots()var=plt.bar(df.Brand, df.Frequency, align='center', alpha=0.5)#this loop is used to represent frequency on each barfor 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 titleplt.ylabel('Frequency')plt.xlabel('Brand Name')plt.title('Car Sales Statistics')plt.show()
This represents a bar chart of the frequency.
We can also represent the distribution of the frequency among different categorical data with 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.
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.
Let’s draw the diagram with python.
from matplotlib.ticker import PercentFormatterdf = 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.
So far so good. We are at the very end of the article.
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]})
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..
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.
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.
The denominator variable is very much important for a data science task. At the very beginning of our analysis, we must have a look at it. Most often we forget to take it into account. Hopefully, this few minutes read provides you a good intuition about the variables, it’s types and so on. You will get the full jupyter notebook file in Github.
Please, let me know if you have any problems in the comment section.
[N.B: I will try my best to publish all the basic knowledge of statistics for data science. If you are interested, you may read the first part of statistical knowledge for data science~ Sampling. Statistics~ Part-1 ]