Exploratory data analysis (EDA) | Introduction to EDA

Contents

This article was published as part of the Data Science Blogathon

draft. EDA is the process of investigating the data set to discover patterns and anomalies (Atypical values) and formulate hypotheses based on our understanding of the data set.

EDA involves generating summary statistics for numerical data in the dataset and creating various graphical representations to better understand the data.. In this article, we will understand EDA with the help of an example data set. we will use Piton idiom (Pandas library) for this purpose.

shutterstock_330005462-5927184

Libraries import

We will start by importing the libraries that we will need to perform EDA. These include NumPy, Pandas, Matplotlib y Seaborn.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

Read data

Now we will read the data from a CSV file into a Pandas DataFrame. You can download the dataset for your reference.

df = pd.read_csv(r'C:UsersVipinData AnalyticsStudentsPerformance.csv')

Let's take a look at what our dataset looks like using df.head (). The output should look like this:

374571-8462784

Descriptive statistics

Perfect! Data looks exactly how we wanted. You can easily tell just by looking at the dataset that contains data about different students in a school / university and their scores in 3 subjects. Comencemos mirando los parameters estadísticos descriptivos para el conjunto de datos. We will use describe () for this.

df.describe(include="all")

By assigning the inclusion attribute a value of 'all', we make sure that categorical characteristics are also included in the result. The output DataFrame should look like this:

716222-6607907

For numeric parameters, fields were filled as the mean, the standard deviation, percentiles and maximum. For categorical characteristics, the count has been completed, the only, the superior (most frequent value) and the corresponding frequency. This gives us a broad idea of ​​our data set.

Missing value imputation

Now we will check for missing values. in our dataset. In case of missing tickets, we will impute them with the appropriate values (moda en caso de característica categórica y median o media en caso de característica numérica). We will use the isnull function () for this purpose.

df.isnull().sum()

This will tell us how many missing values ​​we have in each column of our data set. The exit (Pandas Series) it should look like this:

857853-6896282

Fortunately for us, no missing values ​​in this dataset. Now we will proceed to analyze this data set, observe patterns and identify outliers with the help of graphs and figures.

Graphic representation

We will start with Univariate analysis. We will use a bar graphic for this purpose. We will observe the distribution of students by gender, race / ethnicity, their lunch status and whether they have an exam prep course or not.

plt.subplot(221)

df['gender'].value_counts().plot(kind='bar', title="Gender of students", figsize=(16,9))

plt.xticks(rotation=0)

plt.subplot(222)

df['race/ethnicity'].value_counts().plot(kind='bar', title="Race/ethnicity of students")

plt.xticks(rotation=0)

plt.subplot(223)

df['lunch'].value_counts().plot(kind='bar', title="Lunch status of students")

plt.xticks(rotation=0)

plt.subplot(224)

df['test preparation course'].value_counts().plot(kind='bar', title="Test preparation course")

plt.xticks(rotation=0)

plt.show()

The output should look like this:

871114-3533076

We can infer many things from the graph. There are more girls in school than boys. Most of the students belong to groups C and D. More than 60% of students have a standard lunch at school. What's more, more than 60% of students have not taken any exam preparation courses.

Continuing with the univariate analysis, then, we will do a box plot of the numeric columns (math score, reading score and writing score) in the data set. A box plot helps us visualize the data in terms of quartiles. Also identifies outliers in the data set, if there were. We will use the boxplot function () for this.

df.boxplot()

The output should look like this:

1_df_boxplot-4708051

The middle portion represents the interquartile range (IQR). The horizontal green line in the middle represents the median of the data. Hollow circles near the tails represent outliers in the data set. But nevertheless, since it is quite possible for a student to score extremely low on a test, we will not eliminate these atypical values.

Now we will do a distribution plot of students' math scores. A distribution plot tells us how the data is distributed. We will use the distplot function.

sns.distplot(df['math score'])

The plot in the output should look like this:

2_distplot_math_score-6144861

The graph represents a perfect bell curve up close. The peak is around 65 points, the mean of the students' math scores in the data set. A similar distribution plot can also be made for reading and writing scores..

Now we will see the correlation between 3 scores with the help of a heat map. For this, we will use the corr function () y heatmap () for this exercise.

corr = df.corr()
sns.heatmap(corr, annot=True, square=True)
plt.yticks(rotation=0)
plt.show()

The plot in the output should look like this:

3_df_corr_heatmap-7200129

The heat map shows that 3 scores are highly correlated. The reading score has a correlation coefficient of 0,95 with writing score. The math score has a correlation coefficient of 0,82 with the reading score and 0,80 with writing score.

Now we will move on to Bivariate analysis. We will look at one relational plot en Seaborn. It helps us understand the relationship between 2 variables in different subsets of the data set. We will try to understand the relationship between the math score and the writing score of students of different genders.

sns.relplot(x='math score', y='writing score', hue="gender", data=df)

The relational plot should look like this:

341724-9086863

The graph shows a clear difference in scores between male and female students. For the same score in math, female students are more likely to have higher writing scores than male students. But nevertheless, for the same writing score, male students are expected to score higher in math than female students.

Relational graphs help us to perform bivariate analysis. You can refer to the documentation of the relplot function () en Seaborn here.

Finally, we will analyze the performance of students in mathematics, reading and writing according to the level of education of your parents and the course of preparation for the exam. First, Let's take a look at the impact of parents' education level on their children's performance in school using a line graph.

df.groupby('parental level of education')[['math score', 'reading score', 'writing score']].mean().T.plot(figsize=(12,8))

The output will look like this:

549346-3282871

It is very clear from this graph that students whose parents are more educated than others (master's degree, bachelor's and associate's degree) perform better on average than students whose parents have less education (high school). This may be a genetic difference or simply a difference in the students' home environment. More educated parents are more likely to push their students toward studies.

Secondly, Let's look at the impact of the test preparation course on student performance using a horizontal bar chart.

df.groupby('test preparation course')[['math score', 'reading score', 'writing score']].mean().T.plot(kind='barh', figsize=(10,10))

The output should look like this:

371065-3169059

One more time, it is very clear that students who have completed the exam preparation course have performed better, on average, compared to students who have not opted for the course.

Final notes

In this article, we understood the meaning of Exploratory Data Analysis (EDA) with the help of a sample data set. We look at how we can analyze the data set, draw conclusions from it and form a hypothesis based on that.

The author of this article is Vishesh Arora. You can connect with me at LinkedIn.

The media shown in this article 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