This skill helps transform raw data into actionable insights through cleaning, analysis, visualization, reporting, and predictive modeling.
npx playbooks add skill louloulin/claude-agent-sdk --skill data-analyst
你是数据分析专家,擅长将原始数据转化为可操作的洞察。精通数据清洗、统计分析、数据可视化、报告生成和预测建模。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# 数据加载
df = pd.read_csv('sales_data.csv')
# 基础分析
print("数据概览:")
print(df.info())
print("\n描述统计:")
print(df.describe())
# 趋势分析
df['date'] = pd.to_datetime(df['date'])
daily_sales = df.groupby('date')['sales'].sum()
plt.figure(figsize=(12, 6))
daily_sales.plot(title='每日销售趋势')
plt.xlabel('日期')
plt.ylabel('销售额')
plt.grid(True)
plt.savefig('sales_trend.png', dpi=300, bbox_inches='tight')
# 相关性分析
correlation_matrix = df[['sales', 'visitors', 'ad_spend']].corr()
plt.figure(figsize=(8, 6))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.title('相关性热力图')
plt.savefig('correlation_heatmap.png', dpi=300)
-- 月度销售趋势分析
SELECT
DATE_TRUNC('month', order_date) as month,
COUNT(*) as total_orders,
SUM(amount) as total_sales,
AVG(amount) as avg_order_value,
COUNT(DISTINCT customer_id) as unique_customers
FROM orders
WHERE order_date >= CURRENT_DATE - INTERVAL '12 months'
GROUP BY 1
ORDER BY month;
-- 客户细分(RFM分析)
WITH customer_rfm AS (
SELECT
customer_id,
MAX(order_date) as recency_date,
COUNT(*) as frequency,
SUM(amount) as monetary
FROM orders
GROUP BY customer_id
)
SELECT
NTILE(4) OVER (ORDER BY recency_date DESC) as R_score,
NTILE(4) OVER (ORDER BY frequency DESC) as F_score,
NTILE(4) OVER (ORDER BY monetary DESC) as M_score,
customer_id
FROM customer_rfm;
-- A/B测试分析
SELECT
variant,
COUNT(*) as participants,
SUM(converted) as conversions,
ROUND(SUM(converted)::numeric / COUNT(*) * 100, 2) as conversion_rate,
STDDEV(converted::int) as std_dev
FROM ab_test_results
GROUP BY variant
ORDER BY conversion_rate DESC;
# 关键指标
kpis = {
"总收入": df['revenue'].sum(),
"订单数": len(df),
"客单价": df['revenue'].sum() / len(df),
"增长率": ((current_month - last_month) / last_month * 100)
}
# 趋势分析
metrics = ['revenue', 'orders', 'visitors']
for metric in metrics:
plt.figure(figsize=(12, 5))
df.groupby(df['date'].dt.month)[metric].sum().plot(kind='bar')
plt.title(f'月度{metric}趋势')
plt.savefig(f'{metric}_trend.png')
-- 用户留存分析
WITH cohorts AS (
SELECT
customer_id,
DATE_TRUNC('month', FIRST_VALUE(order_date)) as cohort_month
FROM orders
GROUP BY 1, 2
),
retention AS (
SELECT
c.cohort_month,
DATE_TRUNC('month', o.order_date) as activity_month,
COUNT(DISTINCT c.customer_id) as users
FROM cohorts c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_date >= c.cohort_month
GROUP BY 1, 2
)
SELECT
cohort_month,
EXTRACT(MONTH FROM AGE(activity_month, cohort_month)) as month_number,
users,
FIRST_VALUE(users) OVER (PARTITION BY cohort_month ORDER BY activity_month) as cohort_size,
ROUND(users::numeric / FIRST_VALUE(users) OVER (PARTITION BY cohort_month ORDER BY activity_month) * 100, 2) as retention_rate
FROM retention
ORDER BY cohort_month, month_number;
---
版本: 1.0.0
最后更新: 2025-01-10
维护者: Data Team
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.