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行以捕捉標題問題、尾隨垃圾行或編碼偽影。這在一秒內完成,但可以避免數小時的下游混亂。
小蔥技能有更好的技能skills外掛。
評估資料質量。按絕對值和百分比統計每列中的空值數量。識別方差為零(常數值)的列、高基數分類變數(例如,“備註”欄位每行都有唯一值)以及混合型別列。構建一個簡潔的質量評分卡:缺失超過5%的列、型別可疑的列,以及重複行的數量。
分析單個變數的分佈。對於數值列,計算均值、中位數、標準差、偏度和峰度。繪製直方圖或KDE圖。對於分類列,顯示前10類的值計數和比例。標記高度不平衡的分佈(例如,二元目標變數中一個類別低於5%)。
探索變數之間的關係。計算數值列的完整相關矩陣,並以熱力圖形式視覺化。對於分類與數值的關係,使用分組箱線圖或小提琴圖。對於分類與分類的關係,使用列聯表或馬賽克圖。突出顯示相關性高於0.7或低於-0.7的配對。
檢測異常值和異常現象。對每個數值列應用IQR方法,並報告異常值的數量和百分比。用箱線圖視覺化異常值。跨列交叉參考異常值——在多個列中同時為異常值的行通常代表資料錄入錯誤或真正不尋常的觀察。
將發現綜合成一份EDA報告。撰寫結構化摘要,涵蓋:資料集概述、發現的質量問題、關鍵分佈特徵、顯著相關性、異常值總結以及建議的下一步操作(例如,應刪除哪些列、應應用哪些轉換、哪些特徵可能具有預測性)。
向代理提供資料集檔案路徑。可選擇指定感興趣的列、分類變數的最大顯示類別數以及是否生成自動化的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")
這是一個質量可靠、完成度高的資料分析技能。工作流程定義清晰、示例豐富實用,能有效指導資料探索工作。優點是覆蓋面廣、步驟明確;不足是缺少快速入門指引,文件全英文可能影響中文使用者的使用體驗。推薦給需要進行資料預處理和建模前分析的使用者。