Exploratory Data Analysis: Understanding Your Data for Machine Learning

Ali Zahid Raja
5 min readFeb 23, 2023

--

Exploratory Data Analysis: Understanding Your Data for Machine Learning

This article is the fourth of the series.

The first: A Beginner’s Guide to Machine Learning: Key Concepts and Terminology.

The second: The Machine Learning Process: From Data Collection to Model Deployment.

The Third: Data Collection and Preparation for Machine Learning: Best Practices and Techniques

In the previous articles in this series, we discussed the essential steps in building accurate and effective machine-learning models, including data collection and preparation. Once the data is prepared, the next step is to perform exploratory data analysis (EDA). EDA analyzes the data to identify patterns, correlations, and outliers. This article will discuss the best practices and techniques for exploratory data analysis in machine learning, with examples, images, and code to illustrate each step.

Visualizing the Data

One of the critical aspects of exploratory data analysis is visualizing the data. Visualizations can provide insights into the data that may be absent from numerical summaries or statistical tests. Standard visualization techniques include histograms, scatter plots, heat maps, and box plots. For example, let’s consider a dataset of house prices. We can use Python’s Seaborn library to create a scatter plot that shows the relationship between house size and price:

import seaborn as sns
import pandas as pd

house_prices = pd.read_csv('house_prices.csv')
sns.scatterplot(x='size', y='price', data=house_prices)

We can see from the scatter plot that there is a strong positive correlation between house size and price, with larger houses generally commanding higher prices.

Identifying Correlations

Another critical aspect of EDA is identifying correlations between variables. Correlations provide insights into the relationships between variables and help identify important features for building accurate machine-learning models. Correlations can be identified using correlation coefficients, such as the Pearson correlation coefficient, or by visualizing the data using scatter plots or heat maps. For example, let’s consider a dataset of customer purchases. We can use Python’s Pandas library to calculate the Pearson correlation coefficient between the frequency of investments and the total amount spent:

import pandas as pd

customer_purchases = pd.read_csv('customer_purchases.csv')
corr = customer_purchases['frequency'].corr(customer_purchases['amount'])
print('Pearson Correlation Coefficient:', corr)

This will output the Pearson correlation coefficient between frequency and amount. We can also create a scatter plot to visualize the relationship between frequency and amount:

import seaborn as sns
import pandas as pd

customer_purchases = pd.read_csv('customer_purchases.csv')
sns.scatterplot(x='frequency', y='amount', data=customer_purchases)

We can see from the scatter plot that there is a positive correlation between frequency and amount, with customers who make more purchases generally spending more.

Handling Outliers

Outliers are data points that are significantly different from other data points. Outliers can occur due to errors in data collection or anomalies in the data. Outliers can affect the performance of machine learning models, and it is essential to identify and handle them appropriately. EDA can help identify outliers and inform the appropriate method for taking them, such as removing or transforming them using statistical methods. For example, let’s consider a dataset of medical records. We can use Python’s Pandas library to identify outliers in the age variable using the Z-score:

import pandas as pd
import numpy as np

medical_records = pd.read_csv('medical_records.csv')
z_scores = np.abs((medical_records['age'] - medical_records['age'].mean()) / medical_records['age'].std())
outliers = medical_records[z_scores > 3]

This will output the rows that contain outliers in the age variable. We can then handle these outliers by removing them or transforming them using statistical methods.

Feature Engineering

EDA can also inform the feature engineering process, which involves selecting and transforming the input features used in the machine learning model. EDA can help identify the most relevant and informative features and inform the appropriate transformation techniques, such as scaling or encoding categorical variables. For example, let’s consider a dataset of customer reviews. We can use Python’s Scikit-learn library to identify the most common words or phrases used in positive and negative reviews using the TfidfVectorizer:

import pandas as pd from sklearn.feature_extraction.text 
import TfidfVectorizer

customer_reviews = pd.read_csv('customer_reviews.csv')
vectorizer = TfidfVectorizer(stop_words='english')
tfidf = vectorizer.fit_transform(customer_reviews['text'])
feature_names = vectorizer.get_feature_names()
top_features = tfidf[0].toarray()[0].argsort()[::-1][:10]
print('Top Features:', [feature_names[i] for i in top_features])

Based on the TF-IDF score, this will output the top features for the first review. We can use these features as input features in the machine learning model.

Conclusion

Exploratory data analysis (EDA) is crucial in building accurate and effective machine learning models. By analyzing and visualizing the data, EDA can reveal patterns, correlations, and outliers that provide insights into the relationships and features of the data. Python libraries such as Seaborn, Pandas, and Scikit-learn offer a range of tools for performing EDA efficiently and effectively.

Visualizations such as scatter plots, heat maps, histograms, and box plots can provide quick and intuitive insights into the data. Identifying correlations between variables can help identify relevant features and inform the selection and transformation of input features for machine learning models. Handling outliers is also essential to avoid their potential negative impact on the performance of machine learning models. Feature engineering is another crucial aspect of EDA, which involves selecting and transforming the most relevant and informative features for use in the machine learning model.

By following best practices and techniques in EDA, we can ensure the accuracy and effectiveness of our machine-learning models. In the following article in this series, we will explore the different types of machine learning models and how they can be applied to other kinds of problems.

Stay tuned for in-depth articles!

If you need my help with anything, do let me know in the comments or send me a message!

The Fifth: Model Selection and Training: Choosing the Right Model for Your Data

Links:

  1. https://alizahidraja.com/
  2. https://alizahidraja.com/projects
  3. https://www.linkedin.com/in/alizahidraja/
  4. https://github.com/alizahidraja
  5. https://twitter.com/alizahidraja

--

--

Ali Zahid Raja

Founder | CTO | AI, Data & ML Engineer | Creator | Developer | Entrepreneur | Mentor