Exploratory data analysis using data visualization techniques.

Contents

This post was released as part of the Data Science Blogathon

Introduction

to better understand your data, which helps in further data preprocessing. And data visualization is key, as it streamlines the exploratory data analysis procedure and analyzes the data easily through wonderful charts and graphs.

87001eda20with20visualization20feature20img-1734958

Table of Contents

  • Data visualization
  • Exploratory data analysis
  • Univariate analysis
    • Categorical data
    • Numerical data
  • Bivariate analysis / multivariate
    • Numeric and numeric
    • Numerical and categorical
    • Categorical and categorical
  • Final notes

Data visualization

Data visualization represents text or numeric data in a visual format, which facilitates the understanding of the information expressed by the data. U.S, the humans, we remember images more easily than readable text, so Python provides us with various libraries for data visualization like matplotlib, seaborn, plotly, etc. In this tutorial, we will use Matplotlib and seaborn to perform various techniques to explore data using various plots.

Exploratory data analysis

Create hypotheses, testing various business assumptions while dealing with any machine learning problem statement is very important and this is what EDA helps to achieve. There are several tools and techniques to understand your data, and the basic need is that you must have the knowledge of Numpy for math operations and Pandas for data manipulation.

We will use a very popular Titanic dataset that everyone is familiar with and can download from here.

Now let's start exploring data and studying different data visualization charts with different types of data.. And to demonstrate some of the techniques, We will also use a built-in dataset from seaborn as tip data that explains the tips that each waiter receives from different customers..

let's start by importing libraries and loading data

import numpy as np
import pandas pd
import matplotlib.pyplot as plt
import seaborn as sns
from seaborn import load_dataset
#titanic dataset
data = pd.read_csv("titanic_train.csv")
#tips dataset
tips = load_dataset("tips")

Univariate analysis

Univariate analysis is the simplest form of analysis where we explore a single variable. Univariate analysis is performed to describe the data in a better way. we perform univariate analyzes of numerical and categorical variables differently because the plot uses different graphs.

Categorical data

A variable that has text-based information is known as categorical variables. let's look at several graphs that we can use to visualize categorical data.

1) CountPlot

Countplot is simply a graph of frequency count in the form of bar graphic. Plot the count for each category on a separate bar. When we use the count function of pandas values ​​in any column, is the same visual form of the count value function. Our data destination variable survives and is categorical, so let's draw a tally graph of this.

sns.countplot(data['Survived'])
plt.show()
90944countplot-9084540

2) pie chart

The pie chart is also the same as the counting chart, it only gives you additional information about the percentage of presence of each category in the data, which means which category gets the amount of weighting in the data. Let's look at the Sex column, what is the percentage of male and female members traveling.

data['Sex'].value_counts().plot(kind="pie", autopct="%.2f")
plt.show()
82708pie_chart-5279286

Numerical data

Numerical data analysis is essential because understanding the distribution of variables helps to further process the data. Most of the time you will find a lot of inconsistency with the numerical data, therefore explore numeric variables.

1) Histogram

A histogram is a distribution graph of numeric column values. It just creates bins in various ranges of values ​​and plots them where we can visualize how the values ​​are distributed. We can see where more values ​​are found, as positive, negative or in the center (media). Let's take a look at the Age column.

plt.hist(data['Age'], bins=5)
plt.show()
92476histogram-1909364

2) Distplot

Distplot is also known as the second histogram because it is a slightly enhanced version of the histogram.. Distplot gives us a KDE (Kernel density estimation) about histogram explaining PDF (Probability density function), which means what is the probability that each value occurs in this column. If you have studied statistics before, you should definitely know the PDF function.

sns.distplot(data['Age']) 
plt.show()
61808distplot-7525709

3) Box plot

Boxplot is a very interesting plot that simply plots a summary of 5 numbers. For a summary of 5 numbers, we need to describe some terms.

  • Median: mean value in series after sorting
  • Percentile: gives whatever number is the number of values ​​present before this percentile as, as an example, 50 below percentile 25, so it explains the total of 50 values ​​that are below the percentile 25
  • Minimum and maximum: these are not minimum and maximum values, Rather, they describe the lower and upper limit of the standard deviation that is calculated using the interquartile range. (IQR).
IQR = Q3 - Q1
Lower_boundary = Q1 - 1.5 * IQR
Upper_bounday = Q3 +  1.5 * IQR

Here Q1 and Q3 are the first quantile (percentile 25) and the third quantile (percentile 75)

Bivariate analysis / multivariate

We have studied several plots to explore unique numerical and categorical data. Bivariate Analysis is used when we have to explore the link between 2 different variables and we have to do it because, in the end, our main task is to explore the linkage between the variables to build a powerful model. And when we analyze more than 2 variables together, known as multivariate analysis. we will work on different graphs for Bivariate as well as Multivariate Analysis.

Numeric and numeric

First, let's explore the graphs when both variables are numeric.

1) Scatter plot

Plotting the link between two scatter plots of numeric variables is a simple graph to do. Let's look at the link between the total bill and the tip provided through a Dispersion diagram.

sns.scatterplot(tips["total_bill"], tips["tip"])
55638num_num20scatter-7996878

Multivariate analysis with scatter plot

we can also graph relationships of 3 variables or 4 variables with a scatter plot. Suppose we want to find the separate ratio of men and women with the total bill and tip provided.

sns.scatterplot(tips["total_bill"], tips["tip"], hue=tips["sex"])
plt.show()
266103_var20scatter-9593368

We can also see multivariate analysis of 4 variables with scatterplots using style arguments. Suppose now, along with gender, I also want to know if the client was a smoker or not so that we can do this.

sns.scatterplot(tips["total_bill"], tips["tip"], hue=tips["sex"], style=tips['smoker'])
plt.show()
45007scatter_plot_4_var-4638789

Numerical and categorical

If one variable is numeric and the other is categorical, there are several graphs that we can use for bivariate and multivariate analysis.

1) Bar graphic

The bar chart is a simple diagram that we can use to plot a categorical variable on the x-axis and a numeric variable on the y-axis and explore the relationship between both variables.. The black tip at the top of each bar shows the confidence interval. Let's explore P-Class with age.

sns.barplot(data['Pclass'], data['Age'])
plt.show()
29348bivar_barplot-7789600

Multivariate analysis through bar graph

Hue's argument is very useful and helps to analyze more than 2 variables. Now, along with the above linking, we want to do with gender.

sns.barplot(data['Pclass'], data['Fare'], hue = data["Sex"])
plt.show()
20542multivar_barplot-3233455

2) Box plot

We have already studied about box plots in the previous univariate analysis. we can draw a separate box plot for both variables. Let's explore gender with age using a box plot.

sns.boxplot(data['Sex'], data["Age"])
48591boxplot_bivar-7705788

Multivariate analysis with box plot

Along with age and gender, let's see who has survived and who has not.

sns.boxplot(data['Sex'], data["Age"], data["Survived"])
plt.show()
42456multivar_boxplot-2607238

3) Distplot

Distplot explains PDF function through kernel density estimation. Distplot does not have a pitch parameter, but we can create it. Suppose we want to see the probability of people with a survival probability age range and find out whose survival probability is high for the mortality rate age range.

sns.distplot(data[data['Survived'] == 0]['Age'], hist=False, color="blue") 
sns.distplot(data[data['Survived'] == 1]['Age'], hist=False, color="orange")
plt.show()
97225multivaar_distplot-6852561

As we can see, the graphic is really very interesting. blue shows probability of dying and orange graph shows probability of survival. If we observe it, we can see that the probability of survival of children is greater than death and that it is the opposite in the case of the elderly. This little analysis sometimes says some important things about the data and helps when preparing data stories.

Categorical and categorical

Now we will work on categorical and categorical columns.

1) Heat map

If you've ever used a pandas crosstab function, Heatmap is an equivalent visual representation of that only. Simply, shows how much presence of a category in link with another category is present in the data set. let me show first with the crosstab and later with the heatmap.

pd.crosstab(data['Pclass'], data['Survived'])
91814crosstab-2168745

Now, with the heat map, we have to find how many people survived and died.

sns.heatmap(pd.crosstab(data['Pclass'], data['Survived']))
25852heatmap-8896723

2) Cluster Map

we can also use a cluster map to understand the link between two categorical variables. A cluster map simply draws a dendrogram showing the categories of equivalent behavior together..

sns.clustermap(pd.crosstab(data['Parch'], data['Survived']))
plt.show()
80148clustermap-6359716

If you know clustering algorithms mainly on DBSCAN, then you should know the dendrogram. Then, these are all charts that are used primarily when doing exploratory analysis. There are a few more plots that you can draw as a violent plot, line pattern, a joint weft that are not primarily used.

The complete Notebook for more practice in EDA and data visualization is enabled in my Notebooks by kaggle, access it from here.

Final notes

EDA is just one key to understanding and representing your data in a better way, what, due, helps you build a powerful and more generalized model. Data visualization is easy to perform EDA, making it easier for others to understand our analysis.

I hope it was easy to catch up with all the plots that we have drawn. If you have any doubts, mention it in the comment section below. I will be happy to help you.

About the Author

Raghav Agrawal

I am pursuing my degree in computer science. I really like data science and big data. I love working with data and learning new technologies. Please, feel free to connect with me on Linkedin.

The media shown in this post is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker