关于 correlation-explorer
correlation-explorer 是用于分析 CSV 和 Excel 数据集中变量相关性的工具,面向数据探索与特征筛选。它支持多种相关系数、显著性检验和热图输出。
主要功能
- 01计算成对相关矩阵
- 02绘制相关性热图
- 03检验相关性的 p 值
- 04支持 Pearson、Spearman、Kendall
- 05筛选强相关变量对
- 06分析指定目标变量
适用场景
- 01探索数据集中的变量关系
- 02筛选与目标相关的特征
- 03分析销售额相关因素
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill correlation-explorer安装后在 Claude Code 中输入/correlation-explorer确认已加载。
附带文件scripts/correlation_explorer.pyscripts/requirements.txt
详细介绍
相关性探索器
分析 CSV/Excel 数据集中的变量之间的相关性。
功能
- 相关性矩阵:计算所有成对变量的相关性
- 热力图可视化:以颜色显示相关性
- 显著性检验:计算相关性的 P 值
- 多种方法:Pearson、Spearman、Kendall
- 强相关性:查找高度相关的变量对
- 目标分析:分析与特定变量的相关性
快速开始
from correlation_explorer import CorrelationExplorer
explorer = CorrelationExplorer()
# Load and analyze
explorer.load_csv("sales_data.csv")
matrix = explorer.correlation_matrix()
# Find strong correlations
strong = explorer.find_strong_correlations(threshold=0.7)
print(strong)
# Generate heatmap
explorer.plot_heatmap("correlation_heatmap.png")
CLI 用法
# Compute correlation matrix
python correlation_explorer.py --input data.csv --output correlations.csv
# Generate heatmap
python correlation_explorer.py --input data.csv --heatmap heatmap.png
# Find strong correlations
python correlation_explorer.py --input data.csv --strong --threshold 0.7
# Correlations with target variable
python correlation_explorer.py --input data.csv --target sales
# Use Spearman correlation
python correlation_explorer.py --input data.csv --method spearman
# Include p-values
python correlation_explorer.py --input data.csv --pvalues
API 参考
CorrelationExplorer 类
class CorrelationExplorer:
def __init__(self)
# Data loading
def load_csv(self, filepath: str, **kwargs) -> 'CorrelationExplorer'
def load_dataframe(self, df: pd.DataFrame) -> 'CorrelationExplorer'
# Analysis
def correlation_matrix(self, method: str = "pearson") -> pd.DataFrame
def correlation_with_pvalues(self, method: str = "pearson") -> tuple
def correlate_with_target(self, target: str, method: str = "pearson") -> pd.Series
# Discovery
def find_strong_correlations(self, threshold: float = 0.7) -> list
def find_weak_correlations(self, threshold: float = 0.3) -> list
# Visualization
def plot_heatmap(self, output: str, **kwargs) -> str
def plot_scatter(self, var1: str, var2: str, output: str) -> str
# Export
def to_csv(self, output: str) -> str
def to_json(self, output: str) -> str
相关性方法
| 方法 | 适用场景 |
|---|---|
pearson | 线性关系、正态数据 |
spearman | 非线性、序数数据 |
kendall | 小样本、序数数据 |
# Pearson (default) - parametric
matrix = explorer.correlation_matrix(method="pearson")
# Spearman - rank-based, non-parametric
matrix = explorer.correlation_matrix(method="spearman")
# Kendall - robust to outliers
matrix = explorer.correlation_matrix(method="kendall")
输出格式
相关性矩阵
sales marketing customers
sales 1.000 0.854 0.723
marketing 0.854 1.000 0.612
customers 0.723 0.612 1.000
强相关性
[
{"var1": "sales", "var2": "marketing", "correlation": 0.854, "abs_corr": 0.854},
{"var1": "sales", "var2": "customers", "correlation": 0.723, "abs_corr": 0.723}
]
包含 P 值
{
"correlations": DataFrame,
"pvalues": DataFrame,
"significant": [...], # p < 0.05
}
示例工作流
特征选择
explorer = CorrelationExplorer()
explorer.load_csv("features.csv")
# Find features correlated with target
target_corr = explorer.correlate_with_target("target")
important_features = target_corr[abs(target_corr) > 0.3].index.tolist()
print(f"Important features: {important_features}")
# Find multicollinear features (to potentially drop)
strong = explorer.find_strong_correlations(threshold=0.9)
print("Highly correlated pairs (consider dropping one):")
for pair in strong:
print(f" {pair['var1']} <-> {pair['var2']}: {pair['correlation']:.3f}")
销售分析
explorer = CorrelationExplorer()
explorer.load_csv("sales_data.csv")
# What drives sales?
sales_corr = explorer.correlate_with_target("revenue")
print("Factors correlated with revenue:")
for var, corr in sales_corr.sort_values(ascending=False).items():
if var != "revenue":
print(f" {var}: {corr:.3f}")
# Visualize
explorer.plot_heatmap("sales_correlations.png")
数据探索
explorer = CorrelationExplorer()
explorer.load_csv("dataset.csv")
# Get full picture
corr, pvals = explorer.correlation_with_pvalues()
# Find all significant correlations
significant = []
for i in range(len(corr.columns)):
for j in range(i+1, len(corr.columns)):
if pvals.iloc[i, j] < 0.05:
significant.append({
'var1': corr.columns[i],
'var2': corr.columns[j],
'r': corr.iloc[i, j],
'p': pvals.iloc[i, j]
})
热力图选项
explorer.plot_heatmap(
output="heatmap.png",
cmap="coolwarm", # Color scheme
annot=True, # Show values
figsize=(12, 10), # Figure size
vmin=-1, vmax=1, # Color scale
title="Correlation Matrix"
)
依赖项
- pandas>=2.0.0
- numpy>=1.24.0
- scipy>=1.10.0
- matplotlib>=3.7.0
- seaborn>=0.12.0
评论
支持 Markdown