Univariate, Bivariate and Multivariate data

Univariate data

Univariate data are the ones which consist of only one variable. Univariate data analysis are pretty straightforward as we are dealing with only one variable. The analysis that we do in case of univariate data analysis doesn't have to do anything with the relationships between variables. The main purpose is to describe the data and find patterns that are present.

For example: let's consider the weights of individuals in a class. Here we have only weights as a single variable which is univariate.

Bivariate Data

Bivariate data involves two different variables where we are concerned about investigating the causes and relationship between those 2 variables.

We will understand and explore more about the relationship using covariance and correlation. Please refer this link https://ai-ml-analytics.com/covariance-and-correlation/ to understand more about it and how they relate to bivariate analysis.

If you want to understand how the visualizations are done and want a deeper dive to it then I would suggest to refer the visualization post https://ai-ml-analytics.com/data-visualization-101/

For Example: let's consider a table which has the height and weights of students of a class.

Multivariate data

Data which involves 3 or more variables are termed as Multivariate data. These are similar to bivariate but contains more than one dependent variable.

About the dataset

The dataset is about consists of Placement data of MBA students of a B-school. It includes the following features:

  1. serial number (sl_no)
  2. gender(gender)
  3. secondary school percentage(ssc_p)
  4. secondary school specialization(ssc_b)
  5. higher secondary school percentage(hsc_p)
  6. higher secondary school specialization(hsc_b)
  7. degree percentage(degree_p)
  8. degree specialization(degree_t)
  9. workex(workex)
  10. competitive exam percentage(etest_p)
  11. Specialization(specialisation)
  12. mba percentage(mba_p)
  13. status(status)
  14. salary(salary)

You can download the dataset from kaggle https://www.kaggle.com/benroshan/factors-affecting-campus-placement?select=Placement_Data_Full_Class.csv

1. Import the dataset

In [93]:
import pandas as pd
In [94]:
df = pd.read_csv("Placement.csv")
In [95]:
df.head()
Out[95]:
sl_no gender ssc_p ssc_b hsc_p hsc_b hsc_s degree_p degree_t workex etest_p specialisation mba_p status salary
0 1 M 67.00 Others 91.00 Others Commerce 58.00 Sci&Tech No 55.0 Mkt&HR 58.80 Placed 270000.0
1 2 M 79.33 Central 78.33 Others Science 77.48 Sci&Tech Yes 86.5 Mkt&Fin 66.28 Placed 200000.0
2 3 M 65.00 Central 68.00 Central Arts 64.00 Comm&Mgmt No 75.0 Mkt&Fin 57.80 Placed 250000.0
3 4 M 56.00 Central 52.00 Central Science 52.00 Sci&Tech No 66.0 Mkt&HR 59.43 Not Placed NaN
4 5 M 85.80 Central 73.60 Central Commerce 73.30 Comm&Mgmt No 96.8 Mkt&Fin 55.50 Placed 425000.0

2. check for categorical values in the 'status' column

In [96]:
df['status'].unique()
Out[96]:
array(['Placed', 'Not Placed'], dtype=object)

Univariate data analysis

3. Extract the data where status is 'placed' and 'Not Placed' and store it in df_placed and df_Notplaced respectively

In [97]:
df_placed = df.loc[df['status']=='Placed']
df_Notplaced = df.loc[df['status']=='Not Placed']
In [98]:
df_placed.head()
Out[98]:
sl_no gender ssc_p ssc_b hsc_p hsc_b hsc_s degree_p degree_t workex etest_p specialisation mba_p status salary
0 1 M 67.00 Others 91.00 Others Commerce 58.00 Sci&Tech No 55.0 Mkt&HR 58.80 Placed 270000.0
1 2 M 79.33 Central 78.33 Others Science 77.48 Sci&Tech Yes 86.5 Mkt&Fin 66.28 Placed 200000.0
2 3 M 65.00 Central 68.00 Central Arts 64.00 Comm&Mgmt No 75.0 Mkt&Fin 57.80 Placed 250000.0
4 5 M 85.80 Central 73.60 Central Commerce 73.30 Comm&Mgmt No 96.8 Mkt&Fin 55.50 Placed 425000.0
7 8 M 82.00 Central 64.00 Central Science 66.00 Sci&Tech Yes 67.0 Mkt&Fin 62.14 Placed 252000.0
In [99]:
df_Notplaced.head()
Out[99]:
sl_no gender ssc_p ssc_b hsc_p hsc_b hsc_s degree_p degree_t workex etest_p specialisation mba_p status salary
3 4 M 56.0 Central 52.0 Central Science 52.00 Sci&Tech No 66.00 Mkt&HR 59.43 Not Placed NaN
5 6 M 55.0 Others 49.8 Others Science 67.25 Sci&Tech Yes 55.00 Mkt&Fin 51.58 Not Placed NaN
6 7 F 46.0 Others 49.2 Others Commerce 79.00 Comm&Mgmt No 74.28 Mkt&Fin 53.29 Not Placed NaN
9 10 M 58.0 Central 70.0 Central Commerce 61.00 Comm&Mgmt No 54.00 Mkt&Fin 52.21 Not Placed NaN
12 13 F 47.0 Central 55.0 Others Science 65.00 Comm&Mgmt No 62.00 Mkt&HR 65.04 Not Placed NaN

4. Plot the data using one column from the df_placed and df_Notplaced dataframes

In [100]:
import matplotlib.pyplot as plt
import numpy as np
In [101]:
plt.plot(df_placed['mba_p'],np.zeros_like(df_placed['mba_p']),'*')
#plt.plot(df_Notplaced['mba_p'],np.zeros_like(df_Notplaced['mba_p']),'o')
Out[101]:
[<matplotlib.lines.Line2D at 0x1e98a293e88>]

Bivariate Data Analysis

1.Using Scatter Plot

1. Analysis between x="mba_p",y="salary", grouping (hue) by 'specialisation'

In [ ]:
import seaborn as sns
In [82]:
df.isna().sum()
Out[82]:
sl_no              0
gender             0
ssc_p              0
ssc_b              0
hsc_p              0
hsc_b              0
hsc_s              0
degree_p           0
degree_t           0
workex             0
etest_p            0
specialisation     0
mba_p              0
status             0
salary            67
dtype: int64
In [83]:
drop = df.dropna()
In [84]:
drop.isna().sum()
Out[84]:
sl_no             0
gender            0
ssc_p             0
ssc_b             0
hsc_p             0
hsc_b             0
hsc_s             0
degree_p          0
degree_t          0
workex            0
etest_p           0
specialisation    0
mba_p             0
status            0
salary            0
dtype: int64
In [85]:
import matplotlib.pyplot as plt
from scipy.stats import pearsonr
plt.title('salary vs mba_p(r = {0:0.2f})'.format(pearsonr(drop['salary'],drop['mba_p'])[0]))
sns.scatterplot(x="salary",y="mba_p",data = df, hue='specialisation')
Out[85]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e98a27eb48>

2. Analysis between x="mba_p",y="salary", grouping(hue) by 'workex'

In [86]:
plt.title('mba_p vs hsc_p(r = {0:0.2f})'.format(pearsonr(df['mba_p'],df['hsc_p'])[0]))
sns.scatterplot(x="mba_p",y="hsc_p",data = df, hue='specialisation')
Out[86]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e98afb53c8>

3.Analysis between x="mba_p",y="salary", grouping by 'specialisation' and creating different markers for the legends using style

In [87]:
plt.title('mba_p vs degree_p(r = {0:0.2f})'.format(pearsonr(drop['mba_p'],drop['degree_p'])[0]))
sns.scatterplot(x="mba_p", y="degree_p",hue="specialisation", style="specialisation", data=df)
plt.show()
In [88]:
plt.title('mba_p vs etest_p(r = {0:0.2f})'.format(pearsonr(drop['mba_p'],drop['salary'])[0]))
sns.scatterplot(x="mba_p", y="etest_p",hue="status", style="workex", data=df)
plt.show()
In [89]:
plt.title('mba_p vs salary(r = {0:0.2f})'.format(pearsonr(drop['mba_p'],drop['salary'])[0]))
sns.scatterplot(x="mba_p", y="salary",hue="degree_t", style="degree_t", data=df)
plt.show()
In [90]:
plt.title('mba_p vs salary(r = {0:0.2f})'.format(pearsonr(drop['mba_p'],drop['salary'])[0]))
sns.scatterplot(x="mba_p", y="salary",hue="degree_t", style="gender", data=df)
plt.show()

Multivariate Data Analysis

1. using replot to separate the plot into columns. Here the col = "degree_t" where degree_t has 3 categorical values so the plot is divided into 3 columns here

In [91]:
sns.relplot(x="mba_p",y="salary",col="degree_t",hue="specialisation",style="specialisation",
            kind="scatter",data = df,size ="specialisation",sizes=(80,200))
plt.show()

2. plotting a pair plot for all the features

In [92]:
sns.pairplot(df,hue ='degree_t',size = 2)

plt.show()
In [ ]: