correlation-explorer について
correlation-explorer は、CSV や Excel のデータセット内の変数間の相関を分析するツールです。複数の相関手法、有意性検定、ヒートマップ出力に対応し、データ探索や特徴量選択に使えます。
主な機能
- 01変数間の相関行列を計算
- 02相関ヒートマップを作成
- 03相関の p 値を検定
- 04Pearson、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 対応