A Comprehensive Guide to Exploratory Data Analysis using Python
Exploratory Data Analysis (EDA) is a crucial initial step in any data science project. It involves analyzing and visualizing the data to uncover hidden patterns, spot anomalies, test hypotheses and check assumptions. EDA provides direction for deeper data analysis and helps data scientists and analysts take decisions on how to approach the modeling tasks.
In this step-by-step guide, we will walk through the process of performing EDA on a dataset using Python. We will be using popular Python libraries like Pandas, NumPy, Matplotlib and Seaborn for analysis and visualization. By the end of this article, you will have a solid understanding of what EDA entails and how to implement it effectively using Python.
Why is Exploratory Data Analysis Important?
EDA is a mandatory step before diving into machine learning or statistical modeling. Here are some of the reasons why EDA is so important:
- Helps understand the variables and their relationships
- Identifies missing data and outliers
- Determines data transformations that are needed
- Provides ideas for feature engineering and selection
- Gives an intuition about the machine learning models that would be appropriate
- Allows testing hypotheses and supporting analysis with statistical evidence
Without EDA, you risk building models on faulty assumptions or drawing incorrect conclusions from your data. EDA is what gives meaning to the data and paves way for more advanced analysis down the line.
Steps Involved in Exploratory Data Analysis
While there is no one set way to approach EDA, here is the general workflow that most data scientists follow:
Step 1: Import Required Python Libraries
The first step is to import the Python libraries that we will be using for analysis. Here are the main ones:
- Pandas: For data manipulation and analysis
- NumPy: For numerical computing
- Matplotlib: For creating static, animated, and interactive visualizations
- Seaborn: For drawing attractive and informative statistical graphics
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
Step 2: Load the Dataset
The next step is to read the data into a Pandas DataFrame. Pandas provides convenient functions like read_csv(), read_excel() etc. to read data from various file formats.
For the examples in this article, we will be using the Titanic dataset which contains information about passengers aboard the Titanic. You can download the dataset from Kaggle.
data = pd.read_csv(‘train.csv‘)
Step 3: Inspect the Dataset
Once the data is loaded, it‘s important to take a look at it and understand its structure. Some useful functions for inspecting a DataFrame are:
head()andtail(): Shows the first and last few rows of datashape: Prints the number of rows and columnsinfo(): Provides a concise summary of the DataFramedescribe(): Generates descriptive statistics for numerical columns
data.head()
data.info()
data.describe()
This initial inspection will give you an idea about the number of samples, features, data types, and missing values in your dataset.
Step 4: Check for Missing Values and Handle Them
Real-world data often has missing values. It‘s crucial to identify and handle them appropriately before proceeding with analysis.
To check for missing values:
print(data.isnull().sum())
There are different ways to deal with missing data:
- Remove samples or features with a high percentage of missing values
- Fill missing values with mean, median or mode
- Use advanced imputation techniques like KNN or MICE
Step 5: Perform Univariate Analysis
Univariate analysis involves analyzing one variable at a time. The two main types of univariate analysis are:
- Measures of Central Tendency: Mean, Median, Mode
- Measures of Dispersion: Range, Variance, Standard Deviation, Skewness, Kurtosis
For numerical variables, you can use histograms, box plots and distribution plots to visualize the spread and skewness.
plt.figure(figsize=(8,4))
plt.hist(data[‘Age‘], bins=20, density=True)
plt.xlabel(‘Age‘)
plt.ylabel(‘Frequency‘)
plt.title(‘Distribution of Age‘)
plt.show()
For categorical variables, you can use count plots and pie charts to see the frequency of each category.
sns.countplot(x=‘Sex‘, data=data)
plt.xlabel(‘Sex‘)
plt.ylabel(‘Count‘)
plt.title(‘Counts of Male and Female Passengers‘)
plt.show()
Univariate analysis helps understand each variable independently before studying their interactions.
Step 6: Perform Bivariate Analysis
Bivariate analysis looks at the relationship between two variables. The choice of method depends on whether the variables are numerical or categorical.
For two numerical variables, you can use scatter plots and pair plots to visualize relationships. The Pearson correlation coefficient quantifies the strength of a linear relationship between two variables.
sns.scatterplot(x=‘Age‘, y=‘Fare‘, data=data)
plt.xlabel(‘Age‘)
plt.ylabel(‘Fare‘)
plt.title(‘Fare vs Age‘)
plt.show()
print(data[‘Age‘].corr(data[‘Fare‘]))
For a numerical and categorical variable, you can use box plots, violin plots and bar plots for comparisons.
sns.boxplot(x=‘Pclass‘, y=‘Age‘, data=data)
plt.xlabel(‘Passenger Class‘)
plt.ylabel(‘Age‘)
plt.title(‘Age Distribution by Passenger Class‘)
plt.show()
To find the association between two categorical variables, you can use a chi-square test for independence and visualize it using a stacked bar plot.
Step 7: Perform Multivariate Analysis
Multivariate analysis studies relationships between multiple variables simultaneously. Common techniques are:
- Correlation matrices and heatmaps to visualize correlations between numerical features
- Parallel coordinates and andrews curves to find clusters between instances
- Dimensionality reduction using PCA, t-SNE to uncover hidden patterns
sns.heatmap(data.corr(), cmap=‘coolwarm‘, annot=True, fmt=‘.2f‘)
plt.title(‘Correlation Heatmap‘)
plt.show()
pd.plotting.parallel_coordinates(data, ‘Survived‘, colormap=‘cool‘)
plt.xlabel(‘Features‘)
plt.ylabel(‘Value‘)
plt.title(‘Parallel Coordinates Plot‘)
plt.show()
Step 8: Create New Features and Transform Existing Ones
Feature engineering plays an important role in improving model performance. During EDA, you may uncover opportunities to create new variables or transform existing ones. Some examples:
- Combine variables to create ratios like Price/Earning
- Use domain knowledge to categories or bin numeric variables
- Convert cyclical features into separate sin and cos components
- Apply mathematical functions like log, sqrt, square etc.
data[‘FamilySize‘] = data[‘SibSp‘] + data[‘Parch‘] + 1
data[‘IsAlone‘] = np.where(data[‘FamilySize‘] == 1, 1, 0)
Step 9: Detect and Remove Outliers
Outliers are extreme values that can skew your analyses and models. It‘s important to detect and handle them.
Some common outlier detection methods are:
- Using scatter plots and box plots
- Applying the IQR method
- Using z-scores for normal distributions
- Fitting models like Isolation forest and Local Outlier Factor
Once detected, you can remove outliers or cap them within certain limits depending on the problem.
Q1 = data[‘Age‘].quantile(0.25)
Q3 = data[‘Age‘].quantile(0.75)
IQR = Q3 – Q1
lower_lim = Q1 – 1.5IQR
upper_lim = Q3 + 1.5IQR
data[(data[‘Age‘] < lower_lim) | (data[‘Age‘] > upper_lim)]
Step 10: Derive Insights and Document Your Findings
The final step is to summarize your findings from the analysis. Identify the key insights and relationships you discovered. Use visualizations to convey your results effectively.
Some best practices for documenting EDA are:
- Start with univariate analysis, then move to bivariate and multivariate
- Include specific examples and data points to support your conclusions
- Focus on variables that are most relevant to your problem statement
- Explain the reasoning behind new features you created
- Discuss the limitations of your dataset and potential sources of bias
- Suggest ideas for further analysis or modeling
Tools for Efficient Exploratory Data Analysis
There are some great open-source Python libraries that can automate a big portion of EDA and save time:
- Pandas Profiling: Generates an interactive HTML report with descriptive statistics, missing values, correlations and distribution plots for each feature
- Sweetviz: Creates a high-density dashboard with graphs and text to understand the distribution, relationships and outliers in data
- Autoviz: Performs automatic visualization of any dataset with a single line of code and shows interesting insights
I highly recommend exploring these tools to speed up your EDA workflow.
Conclusion
Exploratory Data Analysis is an underrated skill that can make or break your data science project. It requires a blend of statistical knowledge, coding skills and domain expertise. A well-executed EDA lays the foundation for building robust and insightful models.
By following the step-by-step approach discussed in this article, you can perform a thorough EDA of any dataset using Python. Remember, EDA is not a linear process and you may have to iterate several times as you uncover new patterns. The key is to remain curious and let the data guide your analysis.
I hope this guide gave you a comprehensive understanding of what EDA entails and how to implement it in your data science projects. The complete code used in this article is available on my GitHub repository. Feel free to use it as a starting point for your own analyses.
Happy Exploring!
Cover Image Credits: Photo by NeONBRAND on Unsplash
Frequently Asked Questions
Q. What are the prerequisites for learning exploratory data analysis?
A. To learn EDA effectively, it‘s good to have a basic understanding of statistics, Python programming (mainly Pandas and visualization libraries) and domain knowledge for the dataset you‘re working with. Familiarity with SQL can also be helpful for querying and extracting relevant data.
Q. What are some common challenges faced during EDA?
A. Some of the challenges you may encounter are handling large volumes of data, dealing with missing values and outliers, selecting the right visualization techniques, uncovering hidden biases and knowing when to stop exploring. It‘s important to be aware of these challenges and address them systematically.
Q. How much time should be spent on EDA?
A. The time spent on EDA depends on the complexity of the problem, size of data and your experience level. A typical EDA can take anywhere between a few hours to a few days. The key is to time-box your analysis and know when you have achieved sufficient understanding to move to modeling. Perfectionism can be a big productivity killer in EDA.
Q. Are there any real-world examples of EDA making a significant impact?
A. Absolutely! One famous example is how Google used EDA to optimize the color and placement of ad labels to increase revenue by $200 million a year. EDA has also been used to identify fraud in financial transactions, predict diseases from health records and improve supply chain operations. The applications are endless!