EDA – 探索型数据分析: 使用Python函数

在之前的文章中,我们已经看到了如何使用图形方法进行探索性数据分析。在本文中,我们将专注于Python中用于探索性数据分析的函数。众所周知,探索性数据分析的重要性是不言而喻的,它提供了对数据的简要理解。所以,不浪费太多时间,让我们开始吧!


探索性数据分析-EDA

  • EDA is applied to investigate the data and summarize the key insights.
  • It will give you the basic understanding of your data, it’s distribution, null values and much more.
  • You can either explore data using graphs or through some python functions.
  • There will be two type of analysis. Univariate and Bivariate. In the univariate, you will be analyzing a single attribute. But in the bivariate, you will be analyzing an attribute with the target attribute.
  • In the non-graphical approach, you will be using functions such as shape, summary, describe, isnull, info, datatypes and more.
  • In the graphical approach, you will be using plots such as scatter, box, bar, density and correlation plots.

加载数据

首先,我们将把泰坦尼克号的数据集加载到Python中,用于进行数据探索分析(EDA)。

#Load the required libraries
import pandas as pd
import numpy as np
import seaborn as sns

#Load the data
df = pd.read_csv('titanic.csv')


#View the data
df.head()
Titanic 3

我们的数据已经准备好供探索了!


1. 数据的基本信息 – 探索性数据分析

df.info()函数将为我们提供有关数据集的基本信息。对于任何数据,了解其信息是一个很好的起点。让我们看看它在我们的数据上是如何工作的。

#Basic information

df.info()

#Describe the data

df.describe()
EDA Df Info
  • Describe the data – Descriptive statistics.
Describe

使用此函数,您可以查看上面输出中显示的空值数量、数据类型和内存使用情况以及描述性统计信息。


2. 重复的值 de zhí)

你可以使用df.duplicate.sum()函数来计算重复值的总和,如果有的话。它会显示数据中重复值的数量。

#Find the duplicates

df.duplicated().sum()

0. 我喜欢在家里放松和温馨地度过时间。

好的,该函数返回了‘0’。这意味着我们的数据集中没有重复的值,这是一个非常好的事情。


3.数据中的独特值

在Python中,您可以使用unique()函数找到特定列中唯一值的数量。

#unique values

df['Pclass'].unique()

df['Survived'].unique()

df['Sex'].unique()
array([3, 1, 2], dtype=int64)


array([0, 1], dtype=int64)


array(['male', 'female'], dtype=object)

unique() 函数返回了数据中存在的唯一值,这太酷了!


4. 将独特计数可视化

是的,你可以用可视化的方式展示数据中的唯一值。为此,我们将使用seaborn库。你需要调用sns.countplot()函数,并指定要绘制计数图的变量。

#Plot the unique values

sns.countplot(df['Pclass']).unique()
Countplot

太好了!你做得很好。就是这么简单。虽然 EDA 有两种方法,图形与非图形的结合将会给你整体更大的画面。


找到空值

寻找空值是探索数据分析中最重要的一步。正如我多次强调的那样,确保数据质量至关重要。因此,让我们看看如何找到这些空值。

#Find null values

df.isnull().sum()
PassengerId      0
Survived         0
Pclass           0
Name             0
Sex              0
Age            177
SibSp            0
Parch            0
Ticket           0
Fare             0
Cabin          687
Embarked         2

dtype: int64

哦不,我们的‘年龄’和‘舱位’变量中有一些空值。不过,别担心,我们很快会找到处理它们的方法。


6. 替换空值

嘿,我们有一个replace()函数,可以将所有的空值替换成特定的数据。它太好用了!

#Replace null values

df.replace(np.nan,'0',inplace = True)

#Check the changes now
df.isnull().sum()
PassengerId    0
Survived       0
Pclass         0
Name           0
Sex            0
Age            0
SibSp          0
Parch          0
Ticket         0
Fare           0
Cabin          0
Embarked       0

dtype: int64

哇!太棒了。如上所示,在数据中查找和替换空值非常容易。我已经用0替换了空值。你甚至可以选择更有意义的方法,例如平均值或中位数。


7. 了解数据类型

了解你正在探索的数据类型非常重要,而且也是一个简单的过程。让我们看看它是如何工作的。

#Datatypes

df.dtypes
PassengerId      int64
Survived         int64
Pclass           int64
Name            object
Sex             object
Age             object
SibSp            int64
Parch            int64
Ticket          object
Fare           float64
Cabin           object
Embarked        object

dtype: object

就是这样。你必须按照所示使用 dtypes 函数,并且你将会得到每个属性的数据类型。


8. 过滤数据

是的,你可以根据一些逻辑过滤数据。

#Filter data

df[df['Pclass']==1].head()
Filter

您可以看到,上述代码仅返回属于类别1的数据值。


9. 一幅快速箱线图

只需一行代码,就可以为任何数值列创建一个箱线图。

#Boxplot

df[['Fare']].boxplot()
Eda Boxplot

10. 相关性可视化图 – 探索性数据分析

最后,要找出变量之间的相关性,我们可以利用相关函数。这将让你对不同变量之间的相关强度有一个清晰的了解。

#Correlation 

df.corr()
Corr Matrix

这是一个相关矩阵,范围从+1到-1,其中+1表示高度正相关,-1表示高度负相关。

你甚至可以使用seaborn库将相关矩阵可视化,如下所示。

#Correlation plot

sns.heatmap(df.corr())
Sns Correlation Plot

结尾语 – EDA

探索性数据分析(EDA)是任何分析的最重要部分,能帮助你了解关于数据的许多事情。通过EDA,你将能够找到大部分问题的答案。我已经尽量展示了用于数据探索的许多Python函数和可视化方法。希望本文对你有所帮助。

现在就说这么多吧!祝你愉快地使用Python 🙂

更多阅读:探索性数据分析

广告
将在 10 秒后关闭
bannerAds