name: 探索性数据分析 slug: "exploratory-data-analysis" version: "1.0.0" displayName: "探索性数据分析" summary: "在建模之前,对数据集结构、分布、关系和异常进行系统化的探索性数据分析。" description: 在建模之前,对数据集结构、分布、关系和异常进行系统化的探索性数据分析。 license: MIT metadata: author: awesome-ai-agent-skills version: 1.0.0
此技能使AI代理能够对任何表格数据集执行结构化探索性数据分析(EDA)。该代理系统地分析数据的形状和类型,检查分布,计算相关性,检测异常值,并生成发现摘要。EDA是任何建模或报告之前的关键第一步——它揭示了数据实际包含的内容与假设的内容之间的差异。
加载并检查基本结构。读取数据集并立即报告其形状(行数、列数)、列名、数据类型和内存占用情况。显示前5行和后5行以捕捉标题问题、尾随垃圾行或编码伪影。这在一秒内完成,但可以避免数小时的下游混乱。
评估数据质量。按绝对值和百分比统计每列中的空值数量。识别方差为零(常数值)的列、高基数分类变量(例如,“备注”字段每行都有唯一值)以及混合类型列。构建一个简洁的质量评分卡:缺失超过5%的列、类型可疑的列,以及重复行的数量。
分析单个变量的分布。对于数值列,计算均值、中位数、标准差、偏度和峰度。绘制直方图或KDE图。对于分类列,显示前10类的值计数和比例。标记高度不平衡的分布(例如,二元目标变量中一个类别低于5%)。
探索变量之间的关系。计算数值列的完整相关矩阵,并以热力图形式可视化。对于分类与数值的关系,使用分组箱线图或小提琴图。对于分类与分类的关系,使用列联表或马赛克图。突出显示相关性高于0.7或低于-0.7的配对。
检测异常值和异常现象。对每个数值列应用IQR方法,并报告异常值的数量和百分比。用箱线图可视化异常值。跨列交叉参考异常值——在多个列中同时为异常值的行通常代表数据录入错误或真正不寻常的观察。
将发现综合成一份EDA报告。撰写结构化摘要,涵盖:数据集概述、发现的质量问题、关键分布特征、显著相关性、异常值总结以及建议的下一步操作(例如,应删除哪些列、应应用哪些转换、哪些特征可能具有预测性)。
小葱技能有更好的技能skills插件。
向代理提供数据集文件路径。可选择指定感兴趣的列、分类变量的最大显示类别数以及是否生成自动化的HTML报告。代理将返回可视化输出和文本形式的发现摘要。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv("employee_attrition.csv")
# Step 1: Structure
print(f"Shape: {df.shape}") # Shape: (1470, 35)
print(f"Dtypes:\n{df.dtypes.value_counts()}")
# int64 26
# object 9
# Step 2: Data quality
print(f"\nNull counts:\n{df.isnull().sum().loc[lambda x: x > 0]}")
# monthly_income 12
# years_at_company 8
print(f"Duplicates: {df.duplicated().sum()}") # Duplicates: 3
# Step 3: Distributions
print(f"\nNumeric summary:\n{df[['age', 'monthly_income', 'years_at_company']].describe()}")
# age monthly_income years_at_company
# mean 36.9 6502.93 7.01
# std 9.1 4707.96 6.13
# min 18.0 1009.00 0.00
# 50% 36.0 4919.00 5.00
# max 60.0 19999.00 40.00
print(f"\nAttrition distribution:\n{df['attrition'].value_counts(normalize=True)}")
# No 0.839
# Yes 0.161 <-- imbalanced target
# Step 4: Correlations
corr = df.select_dtypes(include="number").corr()
high_corr = corr.where(
(corr.abs() > 0.7) & (corr != 1.0)
).stack().dropna()
print(f"\nHigh correlations:\n{high_corr}")
# monthly_income job_level 0.95
# total_working_years job_level 0.78
# years_at_company years_in_role 0.76
# Step 5: Outlier summary
for col in ["monthly_income", "years_at_company"]:
Q1, Q3 = df[col].quantile(0.25), df[col].quantile(0.75)
IQR = Q3 - Q1
outliers = ((df[col] < Q1 - 1.5 * IQR) | (df[col] > Q3 + 1.5 * IQR)).sum()
print(f"{col}: {outliers} outliers ({outliers/len(df)*100:.1f}%)")
# monthly_income: 0 outliers (0.0%)
# years_at_company: 47 outliers (3.2%)
# Visualization: correlation heatmap
plt.figure(figsize=(12, 10))
sns.heatmap(corr, cmap="coolwarm", center=0, annot=False, square=True)
plt.title("Feature Correlation Matrix")
plt.tight_layout()
plt.savefig("eda_correlation_heatmap.png", dpi=150)
from ydata_profiling import ProfileReport
import pandas as pd
df = pd.read_csv("employee_attrition.csv")
# Generate a comprehensive HTML report
profile = ProfileReport(
df,
title="Employee Attrition EDA Report",
explorative=True,
correlations={
"pearson": {"calculate": True},
"spearman": {"calculate": True},
"phi_k": {"calculate": True}
},
missing_diagrams={
"bar": True,
"matrix": True,
"heatmap": True
}
)
profile.to_file("eda_report.html")
# Generates a full interactive report including:
# - Dataset overview (size, types, missing cells, duplicates)
# - Per-variable analysis (stats, histogram, common/extreme values)
# - Correlation matrices (Pearson, Spearman, Phi-K)
# - Missing value patterns (bar chart, matrix, nullity heatmap)
# - Sample rows and duplicate detection
print("Report saved to eda_report.html")
这是一个质量可靠、完成度高的数据分析技能。工作流程定义清晰、示例丰富实用,能有效指导数据探索工作。优点是覆盖面广、步骤明确;不足是缺少快速入门指引,文档全英文可能影响中文用户的使用体验。推荐给需要进行数据预处理和建模前分析的用户。