Exploratory Data Analysis Ultimate Guide
Tags: datascience, data, codenewbie
Published: 2023-02-28 12:17:49

Exploratory Data Analysis (EDA) is the process of analyzing data to summarize its main characteristics, often with visual methods. It is a critical step in the data analysis pipeline because it helps to understand the data and identify any issues or insights that may be hidden in it. This article serves as a comprehensive guide to EDA, covering its key concepts, best practices, and examples of how to perform EDA on real-world datasets from Kaggle.

**Objectives of Exploratory Data Analysis**

- Identifying and removing data outliers

- Identifying trends in time and space 

- Uncovering patterns related to the target

- Creating hypotheses and testing them through experiments

- Identifying new sources of data

**Types of Exploratory Data Analysis**

**Univariate Analysis**
The output is a single variable and all data collected is for it. There is no cause-and-effect relationship at all.

**Bivariate Analysis**
The outcome is dependent on two variables, while the relation with it is compared with two variables.

**Multivariate Analysis**
The outcome is more than two. The analysis of data is done on variables that can be numerical or categorical. The result of the analysis can be represented in numerical values, visualization or graphical form.

**Key Concepts of EDA**
Before diving into the details of how to perform EDA, it is important to understand some of the key concepts that underpin it.

<u>Data Cleaning</u>
Data cleaning is the process of identifying and correcting errors, inaccuracies, and inconsistencies in the data. This is an essential step in the EDA process because it ensures that the analysis is based on accurate and reliable data.

Some common data cleaning techniques include removing duplicates, handling missing values, and correcting inconsistent data formats. For example, if a dataset contains missing values, you may choose to either remove those rows or fill them in with a reasonable estimate.

<u>Data Visualization</u>
Data visualization is a crucial aspect of EDA because it helps to identify patterns, trends, and relationships within the data. It involves creating charts, graphs, and other visual representations of the data that can be easily understood by both technical and non-technical audiences.

Some common types of data visualizations include histograms, scatter plots, and heat maps. For example, a scatter plot can be used to visualize the relationship between two variables, while a histogram can be used to visualize the distribution of a single variable.

<u>Data Analysis</u>
Data analysis is the process of using statistical and mathematical techniques to extract insights from the data. This involves identifying patterns, trends, and relationships within the data, as well as making predictions and drawing conclusions based on those insights.

Some common data analysis techniques include regression analysis, hypothesis testing, and clustering. For example, regression analysis can be used to identify the relationship between two variables, while hypothesis testing can be used to determine whether a particular hypothesis is statistically significant.

**Best Practices for EDA**
When performing EDA, there are several best practices that you should follow to ensure that your analysis is accurate and reliable.
<u>Start with a Clear Question or Hypothesis</u>
Before beginning your analysis, it is important to have a clear question or hypothesis that you are trying to answer. This will help to guide your analysis and ensure that you are focusing on the most relevant aspects of the data.
For example, if you are analyzing a dataset on customer behavior, you may want to start by asking questions such as "What factors are driving customer purchases?" or "What are the key drivers of customer loyalty?"

<u>Keep an Open Mind</u>
While it is important to have a clear question or hypothesis, it is also important to keep an open mind and be willing to explore unexpected insights or patterns in the data. This can often lead to new and valuable insights that may not have been considered otherwise.

<u>Use Multiple Methods of Analysis</u>
To ensure that your analysis is robust and reliable, it is important to use multiple methods of analysis. This can include both quantitative and qualitative methods, such as statistical analysis, data visualization, and expert interviews.

<u>Document Your Analysis Process</u>
Finally, it is important to document your analysis process to ensure that your results are reproducible and transparent. This can involve keeping a detailed record of the data cleaning and analysis techniques used, as well as any assumptions or limitations of the analysis.

**Example: EDA on the Titanic Dataset**
[](https://www.kaggle.com/c/titanic/data)
This dataset contains information about passengers on the Titanic, including their demographics, ticket class, and survival status. The goal of this dataset is to predict which passengers survived the sinking of the Titanic based on the given features.

<u>Loading the Data</u>
To begin, we will load the Titanic dataset from Kaggle into a Pandas DataFrame:

```
import pandas as pd

titanic_df = pd.read_csv('train.csv')

```
This code reads in the Titanic dataset from a CSV file and stores it in a Pandas DataFrame called titanic_df.

<u>Understanding the Data</u>
The next step in EDA is to gain a basic understanding of the data by exploring its characteristics, such as the size and shape of the dataset, the data types of each column, and the summary statistics of the variables.

```
print(titanic_df.shape)
print(titanic_df.dtypes)
print(titanic_df.describe())

```
The first line of code prints the size and shape of the dataset, which shows that there are 891 rows and 12 columns in the Titanic dataset.

The second line of code prints the data types of each column, which shows that there are both numerical and categorical variables in the dataset.

The third line of code prints summary statistics of the numerical variables in the dataset, including the count, mean, standard deviation, minimum, and maximum values for each variable. From this output, we can see that the average age of passengers on the Titanic was 29.7 years old, and that the majority of passengers (75%) did not travel with parents or children.

<u>Cleaning the Data</u>
After gaining a basic understanding of the data, the next step is to clean the data by addressing any missing or erroneous values, removing duplicate data, and transforming the data into a format that is suitable for analysis.
One common issue with datasets is missing values. We can use the isnull() function to identify missing values in the Titanic dataset:

```
print(titanic_df.isnull().sum())

```
This code prints the number of missing values for each column in the dataset. From this output, we can see that there are 177 missing values for the Age column, 687 missing values for the Cabin column, and 2 missing values for the Embarked column.
We can also drop columns that have a large number of missing values, such as the Cabin column:
`titanic_df.drop(columns=['Cabin'], inplace=True)
`
This code drops the Cabin column using the drop() function and the inplace=True parameter, which modifies the DataFrame in place.

Finally, we can transform categorical variables into numerical variables using techniques such as one-hot encoding. For example, we can create dummy variables for the Sex column:

```
sex_dummies = pd.get_dummies(titanic_df['Sex'], prefix='Sex')
titanic_df = pd.concat([titanic_df, sex_dummies], axis=1)

```
This code creates dummy variables for the Sex column using the get_dummies() function and then concatenates the dummy variables with the original DataFrame using the concat() function and the axis=1 parameter.

<u>Visualizing the Data</u>
Once the data has been cleaned and prepared, the next step is to visualize the data using various charts and graphs to understand its characteristics.

One of the most important things to understand about the Titanic dataset is the survival rate of the passengers. We can create a bar chart to visualize the survival rate based on gender:

```
import matplotlib.pyplot as plt

survived = titanic_df.groupby('Sex')['Survived'].sum()
total = titanic_df.groupby('Sex')['Survived'].count()
survival_rate = survived/total

plt.bar(survival_rate.index, survival_rate.values)
plt.title('Survival Rate by Gender')
plt.xlabel('Gender')
plt.ylabel('Survival Rate')
plt.show()

```
This code calculates the survival rate for each gender and then creates a bar chart to visualize the results. From this chart, we can see that the survival rate for women was much higher than the survival rate for men.
We can also create a histogram to visualize the distribution of passenger ages:

```
plt.hist(titanic_df['Age'], bins=20)
plt.title('Distribution of Passenger Ages')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()

```
This code creates a histogram with 20 bins to visualize the distribution of passenger ages. From this chart, we can see that the majority of passengers were between 20 and 40 years old.

<u>Analyzing the Data</u>
The final step in the EDA process is to analyze the data and draw insights from it. One way to do this is to create a correlation matrix to identify the relationships between different variables in the dataset:

```
import seaborn as sns

corr_matrix = titanic_df.corr()
sns.heatmap(corr_matrix, annot=True)
plt.title('Correlation Matrix')
plt.show()

```
This code creates a correlation matrix using the corr() function from Pandas and then visualizes it using a heatmap from the Seaborn library. From this chart, we can see that there is a strong negative correlation between passenger class and survival rate, meaning that passengers in higher classes were more likely to survive. We can also see a strong positive correlation between the number of siblings/spouses on board and the number of parents/children on board, indicating that families tended to travel together.

**Conclusion**
EDA is a powerful tool that can be used to uncover valuable insights from data, and by following the best practices outlined in this article, analysts can ensure that their analysis is accurate, reliable, and transparent.