python——ydata-profiling介绍与使用

这篇具有很好参考价值的文章主要介绍了python——ydata-profiling介绍与使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

ydata-profiling的作用

ydata-profiling的主要目标是提供一种简洁而快速的探索性数据分析(EDA)体验。就像pandas中的df.describe()函数非常方便一样,ydata-profiling可以对DataFrame进行扩展分析,并允许将数据分析导出为不同格式,例如html和json。

该软件包输出了一个简单而易于理解的数据集分析结果,包括时间序列和文本数据。

ydata-profiling的安装与简单使用

1. 安装

pip install ydata-profiling

2. 使用

import numpy as np
import pandas as pd
from ydata_profiling import ProfileReport

df = pd.DataFrame(np.random.rand(100, 5), columns=['a','b','c','d','e'])
profile = ProfileReport(df, title="Profiling Report")

ydata-profiling的结果结构

ydata-profiling的结果会使用一些关键属性:

  • 类型推断 (Type inference):自动检测列的数据类型(分类、数值、日期等)
  • 警告 (Warning):对数据中可能需要处理的问题/挑战的概要(缺失数据、不准确性、偏斜等)
  • 单变量分析 (Univariate analysis):包括描述性统计量(平均值、中位数、众数等)和信息可视化,如分布直方图
  • 多变量分析 (Multivariate analysis):包括相关性分析、详细分析缺失数据、重复行,并为变量之间的交互提供视觉支持
  • 时间序列 (Time-Series):包括与时间相关的不同统计信息,例如自相关和季节性,以及ACF和PACF图。
  • 文本分析 (Text analysis):最常见的类别(大写、小写、分隔符)、脚本(拉丁文、西里尔文)和区块(ASCII、西里尔文)
  • 文件和图像分析 (File and Image analysis):文件大小、创建日期、指示截断图像和存在EXIF元数据的指示
  • 比较数据集 (Compare datasets):一行命令,快速生成完整的数据集比较报告
  • 灵活的输出格式 (Flexible output formats):所有分析结果可以导出为HTML报告,便于与各方共享,也可作为JSON用于轻松集成到自动化系统中,还可以作为Jupyter Notebook中的小部件使用

报告还包含三个额外的部分:

  • 概述 (Overview):主要提供有关数据集的全局详细信息(记录数、变量数、整体缺失值和重复值、内存占用情况)
  • 警告 (Alerts):一个全面且自动的潜在数据质量问题列表(高相关性、偏斜、一致性、零值、缺失值、常数值等)
  • 重现 (Reporduction):分析的技术细节(时间、版本和配置)

ydata-profiling的实际应用场景

1. 数据集比较

ydata-profiling可以用于比较同一数据集的多个版本。当需要对比不同时间段(如两年)的数据时,这非常有用。另一个常见的场景是在机器学习中查看训练、验证和测试数据集的数据概况。例如:

from ydata_profiling import ProfileReport

train_df = pd.read_csv("train.csv")
train_report = ProfileReport(train_df, title="Train")

test_df = pd.read_csv("test.csv")
test_report = ProfileReport(test_df, title="Test")

comparison_report = train_report.compare(test_report)
comparison_report.to_file("comparison.html")

比较报告使用设置中的标题属性作为标签。颜色在settings.html.style.primary_colors中进行配置。可以通过调整numeric precision参数settings.report.precision来获得报告中的一些额外空间。

当比较多个报告时:

from ydata_profiling import ProfileReport, compare

comparison_report = compare([train_report, validation_report, test_report])

# Obtain merged statistics
statistics = comparison_report.get_description()

# Save report to file
comparison_report.to_file("comparison.html")

请注意,此功能仅确保支持对两个数据集进行比较的报告。可以获取统计信息,但报告可能存在格式问题。其中一个可以更改的设置是settings.report.precision。根据经验,可以将值10用于单个报告,将值8用于比较两个报告。

2. 时间序列报告

pandas-profiling可以用于对时间序列数据进行快速的探索性数据分析。这对于快速了解与时间相关的变量的行为(如时间图、季节性、趋势和平稳性)非常有用。

结合profiling reports compare,您可以比较时间上的演变和数据行为,以时间序列特定统计信息(如PACF和ACF图)为基础。

以下语法可用于在假设数据集包含时间相关特征的情况下生成概要报告:

import pandas as pd

from ydata_profiling.utils.cache import cache_file
from ydata_profiling import ProfileReport

file_name = cache_file(
    "pollution_us_2000_2016.csv",
    "https://query.data.world/s/mz5ot3l4zrgvldncfgxu34nda45kvb",
)

df = pd.read_csv(file_name, index_col=[0])

# Filtering time-series to profile a single site
site = df[df["Site Num"] == 3003]

profile = ProfileReport(df, tsmode=True, sortby="Date Local", title="Time-Series EDA")

profile.to_file("report_timeseries.html")

要生成时间序列报告,需要将ts_mode设置为“True”。如果设置为“True”,那些具有时间依赖性的变量将根据自相关的存在自动识别出来。时间序列报告使用sortby属性对数据集进行排序。如果未提供此属性,则假定数据集已经按顺序排列。

在某些情况下,您可能已经清楚哪些变量应该是时间序列,或者您只想确保您希望作为时间序列进行分析的变量被正确地进行概要分析:

import pandas as pd

from ydata_profiling.utils.cache import cache_file
from ydata_profiling import ProfileReport

file_name = cache_file(
    "pollution_us_2000_2016.csv",
    "https://query.data.world/s/mz5ot3l4zrgvldncfgxu34nda45kvb",
)

df = pd.read_csv(file_name, index_col=[0])

# Filtering time-series to profile a single site
site = df[df["Site Num"] == 3003]

# Setting what variables are time series
type_schema = {
    "NO2 Mean": "timeseries",
    "NO2 1st Max Value": "timeseries",
    "NO2 1st Max Hour": "timeseries",
    "NO2 AQI": "timeseries",
    "cos": "numeric",
    "cat": "numeric",
}

profile = ProfileReport(
    df,
    tsmode=True,
    type_schema=type_schema,
    sortby="Date Local",
    title="Time-Series EDA for site 3003",
)

profile.to_file("report_timeseries.html")

3. 对大型数据集进行概要分析

默认情况下,ydata-profiling以最能提供数据分析洞察的方式全面总结输入数据集。对于小型数据集,这些计算可以准实时进行。对于较大的数据集,可能需要事先决定要进行哪些计算。一个计算是否适用于大型数据集不仅取决于数据集的确切大小,还取决于其复杂性以及是否可用快速计算。如果概要分析的计算时间成为瓶颈,ydata-profiling提供了几种解决方案来克服这一问题。

3.1 最小模式
ydata-profiling包含一个最小配置文件,默认情况下关闭了最费力的计算。这是处理较大数据集的推荐起点。

profile = ProfileReport(large_dataset, minimal=True)
profile.to_file("output.html")

3.2 对数据集取样
处理非常大型数据集的另一种方法是使用其中一部分数据生成概要分析报告。一些用户报告称,这是在保持代表性的同时缩短计算时间的好方法。

sample = large_dataset.sample(10000)

profile = ProfileReport(sample, minimal=True)
profile.to_file("output.html")

报告的读者可能想了解概要分析是使用数据样本生成的。可以通过向报告添加描述来说明这一点。

description = "Disclaimer: this profiling report was generated using a sample of 5% of the original dataset."
sample = large_dataset.sample(frac=0.05)

profile = sample.profile_report(dataset={"description": description}, minimal=True)
profile.to_file("output.html")

3.3 禁用费力的计算
为了减少特别大型数据集中的计算负担,但仍然保留可能来自它们的一些感兴趣的信息,可以仅针对某些列过滤一些计算。特别地,可以提供一个目标列表给Interactions,以便仅计算与这些特定变量有关的交互作用。

from ydata_profiling import ProfileReport
import pandas as pd

# Reading the data
data = pd.read_csv(
    "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
)

# Creating the profile without specifying the data source, to allow editing the configuration
profile = ProfileReport()
profile.config.interactions.targets = ["Name", "Sex", "Age"]

# Assigning a DataFrame and exporting to a file, triggering computation
profile.df = data
profile.to_file("report.html")

控制此设置的"interactions.targets" 可以通过多个接口进行更改(配置文件或环境变量)。

3.4 并发性
ydata-profiling是一个正在积极开发的项目。其中一个非常期望的功能是添加可扩展的后端,例如Modin或Dask。

4. 处理敏感数据

在某些数据敏感的背景下(例如,私人健康记录),分享包含样本的报告可能会违反隐私约束。以下配置简写将各种选项分组在一起,以便在报告中只提供聚合信息,而不显示个人记录:

report = df.profile_report(sensitive=True)

此外,pandas-profiling不会将数据发送到外部服务,因此非常适合处理私人数据。

4.1 样本和重复值
可以禁用显示数据集样本和重复行的功能,以确保报告不会直接泄漏任何数据:

report = df.profile_report(duplicates=None, samples=None)

或者,仍然可以显示一个样本,但以下代码片段演示了如何生成报告,但在数据集样本部分使用模拟/合成数据。请注意,name和caption键是可选的。

# Replace with the sample you'd like to present in the report (can be from a mock or synthetic data generator)
sample_custom_data = pd.DataFrame()
sample_description = "Disclaimer: the following sample consists of synthetic data following the format of the underlying dataset."

report = df.profile_report(
    sample={
        "name": "Mock data sample",
        "data": sample_custom_data,
        "caption": sample_description,
    }
)

4.2 数据集元数据、数据字典和配置

当与同事共享报告或在网上发布时,包含数据集的元数据(如作者、版权持有人或描述)可能很重要。ydata-profiling允许用这些信息来补充报告。受到schema.org的数据集启发,目前支持的属性有description、creator、author、url、copyright_year和copyright_holder。

以下示例展示了如何生成一个包含描述、版权持有人、版权年份、创作者和URL的报告。在生成的报告中,这些属性将出现在概述部分的“关于”下面。

report = df.profile_report(
    title="Masked data",
    dataset={
        "description": "This profiling report was generated using a sample of 5% of the original dataset.",
        "copyright_holder": "StataCorp LLC",
        "copyright_year": 2020,
        "url": "http://www.stata-press.com/data/r15/auto2.dta",
    },
)

report.to_file(Path("stata_auto_report.html"))

除了提供数据集的详细信息外,用户在与团队成员和利益相关者分享报告时,通常希望包含针对每列的具体描述。ydata-profiling支持创建这些描述,以便报告中包含内置的数据字典。默认情况下,这些描述会在报告的概述部分中呈现,在每个变量旁边显示。

profile = df.profile_report(
    variables={
        "descriptions": {
            "files": "Files in the filesystem, # variable name: variable description",
            "datec": "Creation date",
            "datem": "Modification date",
        }
    }
)

profile.to_file(report.html)

另外,列描述可以从一个JSON文件中加载:

   {
       column name 1: column 1 definition,
       column name 2: column 2 definition
   }
import json
import pandas as pd
import ydata_profiling

definition_file = dataset_column_definition.json

# Read the variable descriptions
with open(definition_file, r) as f:
    definitions = json.load(f)

# By default, the descriptions are presented in the Overview section, next to each variable
report = df.profile_report(variable={"descriptions": definitions})

# We can disable showing the descriptions next to each variable
report = df.profile_report(
    variable={"descriptions": definitions}, show_variable_description=False
)

report.to_file("report.html")

除了提供数据集的详细信息,用户通常还希望包含设置类型模式。当将ydata-profiling生成与数据目录中已有的信息集成时,这一点尤为重要。当使用ydata-profiling的ProfileReport时,用户可以设置type_schema属性来控制生成的数据类型分析。默认情况下,type_schema会通过visions自动推断。

import json
import pandas as pd

from ydata_profiling import ProfileReport
from ydata_profiling.utils.cache import cache_file

file_name = cache_file(
    "titanic.csv",
    "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv",
)
df = pd.read_csv(file_name)

type_schema = {"Survived": "categorical", "Embarked": "categorical"}

# We can set the type_schema only for the variables that we are certain of their types. All the other will be automatically inferred.
report = ProfileReport(df, title="Titanic EDA", type_schema=type_schema)

report.to_file("report.html")

5. 自定义报告的外观

在某些情况下,用户可能希望根据个人喜好或公司品牌来自定义报告的外观。ydata-profiling提供了两个主要的自定义方面:HTML报告的样式和其中包含的可视化和图表的样式

5.1 自定义报告的主题

报告的多个方面都可以进行自定义。下表显示了可用的设置:

参数 类型 默认 描述
html.minify_html bool True 如果为True,则使用htmlmin包对输出的HTML进行最小化处理。
html.use_local_assets bool True 如果为True,则所有资源(样式表、脚本、图片)将被存储在本地。如果为False,则使用CDN来提供部分样式表和脚本。
html.inline boolean True 如果为True,则所有资源都包含在报告中。如果为False,则创建一个Web导出,其中所有资源都存储在“[REPORT_NAME]_assets/”目录中。
html.navbar_show boolean True 是否在报告中包含导航栏。
html.style.theme string None 选择开机自检主题。可选项:平坦(深色)和团结(橙色)
html.style.logo string base64 编码的徽标,显示在导航栏中
html.style.primary_color string #337ab7 报告中使用的主色调。
html.style.full_width boolean False 默认情况下,报告的宽度是固定的。如果设置为 “True”,则使用屏幕全宽。

向底层 matplotlib 可视化引擎传递参数的一种方法是在计算剖面图时使用 plot 参数。可以使用关键对 image_format: “png”,并使用 dpi: 800 更改图像的分辨率。举例如下

profile = ProfileReport(
    planets,
    title="Pandas Profiling Report",
    explorative=True,
    plot={"dpi": 200, "image_format": "png"},
)

饼图用于绘制分类(或布尔)特征中的类别频率。默认情况下,如果一个特征的独特值不超过 10 个,则该特征被视为分类特征。这个阈值可以通过 plot.pie.max_unique 设置来配置。

如果特征未被视为分类特征,则不会显示饼图。因此,可以通过设置:plot.pie.max_unique = 0 来删除所有饼图。

饼图的颜色可以通过 plot.pie.colors 设置配置为任何可识别的 matplotlib 颜色。

profile = ProfileReport(pd.DataFrame([1, 2, 3]))
profile.config.plot.pie.colors = ["gold", "b", "#FF796C"]

相关矩阵和缺失值概览等可视化工具中使用的调色板也可以通过 plot 参数进行自定义。要自定义相关矩阵使用的调色板,请使用相关键:

from ydata_profiling import ProfileReport

profile = ProfileReport(
    df,
    title="Pandas Profiling Report",
    explorative=True,
    plot={"correlation": {"cmap": "RdBu_r", "bad": "#000000"}},
)

同样,缺失值的调色板也可以使用missing参数来更改:文章来源地址https://www.toymoban.com/news/detail-771712.html

from ydata_profiling import ProfileReport

profile = ProfileReport(
    df,
    title="Pandas Profiling Report",
    explorative=True,
    plot={"missing": {"cmap": "RdBu_r"}},
)

到了这里,关于python——ydata-profiling介绍与使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • spring.profiles的使用详解

    本文来说下spring.profiles.active和spring.profiles.include的使用与区别 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配

    2024年02月11日
    浏览(24)
  • 使用Profiler进行性能分析

    帧率:单位FPS,是衡量游戏性能的标准。 渲染:绘制一帧到屏幕被称为渲染一帧。 每帧花费时间=1000/[渴望的帧率] 在目标平台上的播放器中对应用程序进行性能分析 在Unity编辑器中以运行模式对应用程序进行性能分析 对Unity编辑器进行性能分析 获得有关应用程序的准确时序

    2024年02月05日
    浏览(54)
  • Mysql调优工具------profiling使用

    提示:MYSQL调优工具 0:表示为开启 1:表示开启 Set profiling=1; 此时profiling状态开启, 查看所有语句消耗的具体时间(Duration)和使用的查询(Query ) 语法: show profile for query query_id 查看具体的query_ID所经历的过程以及消耗的资源详细分析报告 cpu CPU_user:当前用户占用的 CP

    2024年02月17日
    浏览(24)
  • ES-使用profile做性能分析

    用来查看各个组件执行时间的详细信息,但是注意,这个api不会用来测量网络延迟,请求在队列中的等待时间,以及协调节点合并各个分片响应时所花费的时间。 总体的返回结构 (1) 当前参与响应的分片,id是唯一的,格式是由 节点名 + 索引名 + 分片 组成 (2) query部分的耗时

    2024年02月09日
    浏览(36)
  • Android Profiler 内存分析器使用

    Android Profiler是Android Studio的一部分,提供了一个集成的性能分析工具套件,包括内存分析。Android Profiler 工具可提供实时数据,帮助您了解应用的 CPU、内存、网络和电池资源使用情况。 在Android Profiler中,您可以查看内存使用情况的实时图表、堆转储快照、分析内存泄漏等,

    2024年02月08日
    浏览(36)
  • Spring @Profile注解使用和源码解析

    在之前的文章中,写了一篇使用Spring @Profile实现开发环境,测试环境,生产环境的切换,之前的文章是使用SpringBoot项目搭建,实现了不同环境数据源的切换,在我们实际开发中,会分为dev,test,prod等环境,他们之间数独立的,今天进来详解介绍Spring @Profile的原理。 # Spring注

    2023年04月13日
    浏览(33)
  • python实战应用讲解-【numpy科学计算】line_profiler模块(附python示例代码)

    目录   Numpy 安装line_profiler 准备工作 具体步骤 Numpy 用line_profiler分析代码 具体步骤 攻略小结

    2023年04月08日
    浏览(35)
  • 使用sql profile 稳定执行计划的案例

    接上次一次hard parse处理过程 自从为了解决hard parse的问题而设置了cursor_sharing=force后,又衍生了其他的问题,那就是执行计划的稳定性,如下记录发生的一起强制绑定变量后引起的执行计划绑定的问题 用戶反映早上接数据变得很慢,使用如下sql检查该时段最频繁的sql 在跟系统

    2024年02月09日
    浏览(27)
  • Py:代码性能分析之使用python工具—如利用cProfile【输出每个函数的运行时间和调用次数】/line_profiler【输出每行代码的执行时间】)同时对比斐波那契数列问题的递归方法和动态规划

    Py:代码性能分析之使用python工具—如利用cProfile【输出每个函数的运行时间和调用次数】/line_profiler【输出每行代码的执行时间】)同时对比斐波那契数列问题的递归方法和动态规划算法实现 目录

    2024年02月04日
    浏览(57)
  • unity 使用模拟器进行Profiler性能调试

    这篇文章主要记录如何实现通过模拟器对打包的app游戏进行Profiler调试。主要记录一些比较重要的点。 首先你要能够打包unity的安卓包,如果没有安装安卓组件,请先安装组件。 安装完成以后,会在unity的安装目录找到相应的SDK 这个platform-tools后面会用到,而这些组件设置了

    2024年02月07日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包