登录
首页精彩阅读前瞻:数据科学中的探索性数据分析(DEA)
前瞻:数据科学中的探索性数据分析(DEA)
2021-11-16
收藏

CDA数据分析师 出品

作者: tukey

数据科学爱好者知道,在将原始数据输入到机器学习模型之前,需要对其进行大量数据预处理。为此,需要遵循一系列标准来准备数据,具体取决于手头问题的类型(回归或分类)。这个过程的一个主要部分涉及以所有可能的方式评估数据集,以找到有价值的相关性(彼此和目标之间的特征依赖性)并排除噪声(不一致或异常值,即不合格的数据点)。要探索任何数据集,Python 是可用的最强大的数据分析工具之一,此外,还有同样强大的 Python 库可以更好地可视化数据。

因此,为了使数据更有意义或从可用数据中提取更多价值,必须快速解释和分析它。这是Python数据可视化库通过生成图形表示和让数据说话所擅长的地方。通过这种方式,我们可以发现大量数据背后所有可能的趋势和模式。

今天,数据科学和机器学习不仅仅适用于具有强大计算机科学背景的人。相反,欢迎来自不同行业的专业人士对数据有着相同的热情,尽管他们具有一些统计知识,但这种趋势正在增加。这就是为什么来自不同背景和教育背景的人倾向于尝试数据科学和人工智能必须提供的东西。

但是对于刚刚开始使用机器学习的初学者来说,理解数据的选择太多是具有挑战性的,有时甚至是压倒性的。我们都希望我们的数据看起来很漂亮并且可以展示,以便更快地做出决策。总体而言,EDA可能是一个耗时的过程,因为我们仔细查看多个图以找出哪些特征是重要的并对结果产生重大影响。此外,我们寻找方法来处理缺失值和/或异常值、修复数据集中的不平衡以 及许多此类具有挑战性的任务。因此,在选择满足 EDA 需求的最佳库时,这是一个艰难的选择。因此,对于任何开始机器学习之旅的人来说,从自动化 EDA 库开始都是一种很好的学习体验。这些库提供了良好的数据整体视图,并且易于使用。只需几行简单的 Python 代码,这些库就可以节省时间,并使新手能够更加专注于了解如何使用这些不同的图来理解数据。但是,初学者肯定需要对这些库生成的图有基本的了解。

在本文中,我们将为初学者讨论三个有趣的自动EDA Python 库。对于这个初学者友好的教程,我们将使用来自sklearn 的内置“iris”数据集。

我们将首先导入包和库

#loading the datasetfrom sklearn import datasets import pandas as pd print("pandas:",pd. version )

pandas: 1.3.2

data = datasets.load_iris()df = pd.DataFrame(data.data,columns=data.feature_names) df['target'] = pd.Series(data.target)df.head()

如果我们不使用 AutoEDA,这里有一个通常用于 EDA 的命令列表,用于打印有关 DataFrame/数据集的不同信息(不一定按相同的顺序)。

  • df.head() – 前五行
  • df.tail() – 最后五行
  • df.describe() – 有关数据集的百分位数、平均值、标准偏差等的基本统计信息
  • df.info() – 数据集摘要
  • df.shape() – 数据集中的观察值和变量的数量,即数据的维度
  • df.dtypes() – 变量的数据类型(int、float、object、datetime)
  • df.unique()/df.target.unique() – 数据集/目标列中的唯一值
  • df['target'].value_counts() – 分类问题的⽬标变量分布
  • df.isnull().sum()- 计算数据集中的空值
  • df.corr() – 相关信息
  • 等等...

查看我们必须使用多少命令才能在数据中找到洞察力。AutoEDA 库可以通过几行 Python 代码快速完成所有这些以及更多工作。但在我们开始之前,让我们先检查安装的 Python 版本,因为这些库需要 Python >=3.6。要获取版本信息,请在 Colab 中使用以下命令。

# python versionimport sys sys.version

'3.7.6 (default, Jan 8 2020, 19:59:22) n[GCC 7.3.0]'

确认好了符合条件的Python 版本,现在就可以自动进行EDA探索数据分析。

01、Pandas Profiling 3.0.0

import pandas_profiling print("pandas_profiling:",pandas_profiling. version )

pandas_profiling: 3.0.0

从报告中,初学者可以很容易地理解 iris 数据集中有 5 个变量——4 个数字变量,结果变量是分类变量。此外,数据集中有 150 个样本并且没有缺失值

#Generating PandasProfiling Reportreport = pandas_profiling.ProfileReport(df) report

02、Sweetviz 2.1.3

这也是一个开源 Python 库,仅使用两行代码即可执行深入空格的 EDA。该库为数据集生成的报告以 .html 文件形式提供,可以在任何浏览器中打开。使用 Sweetviz,我们可以检查数据集特征如何与目标值相关联。

可视化测试和训练数据并比较它们。我们可以使用analyze()、compare() 或compare_intra() 来评估数据并生成报告绘制数值和分类变量的相关性。

总结有关缺失值、重复数据条目和频繁条目的信息以及数值分析,即解释统计值与前面的部分类似,我们将首先导入 pandas 来读取和处理数据集。

接下来,我们只需导入 sweetviz 来探索数据。

import sweetviz as sv print("sweetviz :",sv. version )

sweetviz : 2.1.3

这就是经典的的 Sweetviz 报告的样式

#Generating Sweetviz reportreport = sv.analyze(df)report.show_html("iris_EDA_report.html") # specify a name for the report

| | [ 0%] 00:00 -> (? left)Report iris_EDA_report.html was generated! NOTEBOOK/COLAB USERS: the web browser MAY not pop

生成的这些 .html 报告您可以在当前目录下找到,然后可以在浏览器中打开报告。

03、AutoViz 0.0.83

另一个开源 Python EDA 库,只需一行代码即可快速分析任何数据。

# pip install autoviz# pip install wordcloud

from autoviz.AutoViz_Class import AutoViz_ClassAV = AutoViz_Class()

Imported AutoViz_Class version: 0.0.84. Call using: AV = AutoViz_Class()AV.AutoViz(filename, sep=',', depVar='', dfte=None, header=0, verbose=0,lowess=False,chart_format='svg',max_rows_analyzed=150000,max_cols Note: verbose=0 or 1 generates charts and displays them in your local Jupyter notebook.verbose=2 does not show plot but creates them and saves them in AutoViz_Plots directory

由于我们使用的是库中的数据集,因此我们使用 'dfte' 选项而不是 EDA 的文件名。

#Generating AutoViz Report #this is the default command when using a file for the datasetfilename = "" sep = ","dft = AV.AutoViz( filename,sep=",",

depVar="", dfte=None, header=0, verbose=0, lowess=False, chart_format="svg",max_rows_analyzed=150000,max_cols_analyzed=30,)

Dataname input must be a filename with path to that file or a Dataframe Not able to read or load file. Please check your inputs and try again...

#Generating AutoViz Reportfilename = "" # empty string ("") as filename since no file is being used for the datasep = ","dft = AV.AutoViz( '',sep=",",depVar="", dfte=df, header=0,verbose=0, lowess=False, chart_format="svg",max_rows_analyzed=150000,max_cols_analyzed=30,

Shape of your Data Set loaded: (150, 5)############## C L A S S I F Y I N G V A R I A B L E S ####################Classifying variables in data set...Number of Numeric Columns = 4Number of Integer-Categorical Columns = 1 Number of String-Categorical Columns = 0 Number of Factor-Categorical Columns = 0 Number of String-Boolean Columns = 0 Number of Numeric-Boolean Columns = 0 Number of Discrete String Columns = 0 Number of NLP String Columns = 0Number of Date Time Columns = 0 Number of ID Columns = 0Number of Columns to Delete = 05 Predictors classified...This does not include the Target column(s)No variables removed since no ID or low-information variables found in data set Number of All Scatter Plots = 10

depVar="", dfte=None, header=0, verbose=0, lowess=False, chart_format="svg",max_rows_analyzed=150000,max_cols_analyzed=30,)

Dataname input must be a filename with path to that file or a Dataframe Not able to read or load file. Please check your inputs and try again...

#Generating AutoViz Reportfilename = "" # empty string ("") as filename since no file is being used for the datasep = ","dft = AV.AutoViz( '',sep=",",depVar="", dfte=df, header=0,verbose=0, lowess=False, chart_format="svg",max_rows_analyzed=150000,max_cols_analyzed=30,

Shape of your Data Set loaded: (150, 5)############## C L A S S I F Y I N G V A R I A B L E S ####################Classifying variables in data set...Number of Numeric Columns = 4Number of Integer-Categorical Columns = 1 Number of String-Categorical Columns = 0 Number of Factor-Categorical Columns = 0 Number of String-Boolean Columns = 0 Number of Numeric-Boolean Columns = 0 Number of Discrete String Columns = 0 Number of NLP String Columns = 0Number of Date Time Columns = 0 Number of ID Columns = 0Number of Columns to Delete = 05 Predictors classified...This does not include the Target column(s)No variables removed since no ID or low-information variables found in data set Number of All Scatter Plots = 10

Number of Columns to Delete = 05 Predictors classified...This does not include the Target column(s)No variables removed since no ID or low-information variables found in data set Number of All Scatter Plots = 10

Time to run AutoViz (in seconds) = 6.979###################### VISUALIZATION Completed ########################

AutoViz 报告包括有关数据集形状的信息以及所有可能的图表,包括条形图、小提琴图、相关矩阵(热图)、配对图等。所有这些信息与一行代码肯定对任何初学者都有用。

因此,我们使用三个 AutoEDA 库以最少的代码自动化了一个小数据集的数据分析。以上所有代码都可以在原文链接中访问。

结语

从初学者的⻆度来看,Pandas Profiling、Sweetviz 和 AutoViz 似乎是最简单的生成报告以及呈现数据集洞察力的工具。在开始做数据探索时,我经常使用这些库以最少的代码快速发现有趣的数据规律和趋势。希望对你有用!

数据分析咨询请扫描二维码

最新资讯
更多
客服在线
立即咨询