ตัวแทนสรุปผลทางสถิติ
การวิเคราะห์การแจกแจง สหสัมพันธ์ และการตรวจจับค่าผิดปกติในรายงานที่สร้างโดยตัวแทน
ตัวแทนสรุปผลทางสถิติ เป็นบทเรียน AI Agents ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents มีบทเรียนทั้งหมด 4 บทเรียน
เหตุผลที่สรุปทางสถิติมีความสำคัญ
ข้อมูลดิบแทบไม่เป็นประโยชน์เมื่อพิจารณาเพียงลำพัง เอเจนต์วิเคราะห์ข้อมูลจะมีคุณค่าอย่างแท้จริงเมื่อสามารถคำนวณสรุปทางสถิติและแปลงตัวเลขเป็นข้อค้นพบที่เข้าใจได้ง่ายโดยอัตโนมัติ
บทเรียนนี้ครอบคลุมการดำเนินการทางสถิติหลัก ได้แก่ การสรุปข้อมูล ความสัมพันธ์ การตรวจจับค่าผิดปกติ การระบุการกระจาย และการสร้างข้อค้นพบอัตโนมัติ
df.describe(): จุดเริ่มต้น
df.describe() คำนวณจำนวน ค่าเฉลี่ย ส่วนเบี่ยงเบนมาตรฐาน ค่าต่ำสุด ควอไทล์ และค่าสูงสุดของคอลัมน์เชิงตัวเลขทั้งหมดด้วยการเรียกใช้เพียงครั้งเดียว นี่เป็นขั้นตอนแรกมาตรฐานของการวิเคราะห์ข้อมูลเชิงสำรวจ
import pandas as pd
df = pd.read_csv('sales_data.csv')
# Basic numeric summary
print(df.describe())
# Also describe categorical columns
print(df.describe(include='object'))
# Custom percentiles
print(df.describe(percentiles=[0.05, 0.25, 0.5, 0.75, 0.95]))
# Convert to clean dict for agent use
def get_numeric_summary(df):
desc = df.describe().round(2)
return {
col: desc[col].to_dict()
for col in desc.columns
}เมทริกซ์สหสัมพันธ์
เมทริกซ์สหสัมพันธ์แสดงความสัมพันธ์เชิงเส้นแบบจับคู่ระหว่างคอลัมน์เชิงตัวเลขทั้งหมด ค่าอยู่ในช่วงตั้งแต่ -1 (ความสัมพันธ์เชิงลบอย่างมาก) ถึง +1 (ความสัมพันธ์เชิงบวกอย่างมาก) ค่าที่ใกล้ 0 หมายถึงไม่มีความสัมพันธ์เชิงเส้น
import pandas as pd
import numpy as np
def compute_correlation_matrix(df):
numeric = df.select_dtypes(include='number')
corr = numeric.corr().round(3)
return corr.to_dict()
def find_strong_correlations(df, threshold=0.7):
numeric = df.select_dtypes(include='number')
corr = numeric.corr()
strong = []
cols = corr.columns
for i in range(len(cols)):
for j in range(i + 1, len(cols)): # upper triangle only
val = corr.iloc[i, j]
if abs(val) >= threshold:
strong.append({
'col_a': cols[i],
'col_b': cols[j],
'correlation': round(val, 3),
'direction': 'positive' if val > 0 else 'negative'
})
return sorted(strong, key=lambda x: abs(x['correlation']), reverse=True)การตรวจจับค่าผิดปกติ: วิธี IQR
วิธีพิสัยระหว่างควอไทล์ (IQR) ระบุค่าผิดปกติว่าเป็นค่าที่สูงกว่า Q3 มากกว่า 1.5 เท่าของ IQR หรือต่ำกว่า Q1 มากกว่า 1.5 เท่าของ IQR วิธีนี้ไม่ไวต่อค่าที่สุดโต่ง ซึ่งแตกต่างจากคะแนน z และยังทำงานได้ดีแม้ข้อมูลจะมีค่าผิดปกติอยู่แล้ว
import pandas as pd
def detect_outliers_iqr(df, columns=None, multiplier=1.5):
numeric = df.select_dtypes(include='number')
if columns:
numeric = numeric[columns]
outlier_report = {}
for col in numeric.columns:
q1 = numeric[col].quantile(0.25)
q3 = numeric[col].quantile(0.75)
iqr = q3 - q1
lower = q1 - multiplier * iqr
upper = q3 + multiplier * iqr
outliers = numeric[(numeric[col] < lower) | (numeric[col] > upper)][col]
outlier_report[col] = {
'q1': round(q1, 3),
'q3': round(q3, 3),
'iqr': round(iqr, 3),
'lower_bound': round(lower, 3),
'upper_bound': round(upper, 3),
'outlier_count': len(outliers),
'outlier_pct': round(len(outliers) / len(df) * 100, 1),
'outlier_values': outliers.tolist()[:10] # first 10
}
return outlier_reportการระบุประเภทการกระจาย
การระบุประเภทการกระจาย (ปกติ เบ้ สองยอด หรือสม่ำเสมอ) ช่วยให้เอเจนต์เลือกการทดสอบทางสถิติที่เหมาะสมและอธิบายข้อมูลได้อย่างถูกต้อง
ใช้ความเบ้และความโด่งเป็นวิธีประเมินอย่างรวดเร็วก่อนทำการทดสอบอย่างเป็นทางการ
import pandas as pd
from scipy import stats
def identify_distribution(series):
'''Classify distribution type using skewness, kurtosis, and normality test.'''
clean = series.dropna()
if len(clean) < 8:
return {'type': 'insufficient_data'}
skewness = clean.skew()
kurtosis = clean.kurtosis()
# Shapiro-Wilk normality test (reliable up to ~5000 samples)
sample = clean.sample(min(500, len(clean)), random_state=42)
_, p_value = stats.shapiro(sample)
distribution = 'normal' if p_value > 0.05 else 'non-normal'
if abs(skewness) < 0.5 and p_value > 0.05:
dist_type = 'normal'
elif skewness > 1.0:
dist_type = 'right-skewed (long right tail)'
elif skewness < -1.0:
dist_type = 'left-skewed (long left tail)'
elif abs(skewness) < 0.5 and kurtosis < -1:
dist_type = 'uniform-like'
else:
dist_type = 'approximately normal'
return {
'type': dist_type,
'skewness': round(skewness, 3),
'kurtosis': round(kurtosis, 3),
'normality_pvalue': round(p_value, 4),
'is_normal': p_value > 0.05
}การทดสอบภาวะปรกติ
มีการทดสอบภาวะปรกติอย่างเป็นทางการหลายวิธี การทดสอบ Shapiro-Wilk เหมาะที่สุดสำหรับกลุ่มตัวอย่างขนาดเล็ก ส่วน D'Agostino-Pearson เหมาะสำหรับชุดข้อมูลขนาดใหญ่ โดยทั่วไปกำหนดค่าเกณฑ์ของค่า p ไว้ที่ 0.05 หาก p < 0.05 ให้ปฏิเสธสมมติฐานว่าข้อมูลมีการแจกแจงปกติ
from scipy import stats
import numpy as np
def test_normality(series, alpha=0.05):
clean = series.dropna().values
n = len(clean)
results = {}
# Shapiro-Wilk (best for n < 5000)
if n <= 5000:
stat, p = stats.shapiro(clean)
results['shapiro_wilk'] = {
'statistic': round(stat, 4),
'p_value': round(p, 4),
'is_normal': p > alpha
}
# D'Agostino-Pearson (n >= 20)
if n >= 20:
stat, p = stats.normaltest(clean)
results['dagostino_pearson'] = {
'statistic': round(stat, 4),
'p_value': round(p, 4),
'is_normal': p > alpha
}
# Kolmogorov-Smirnov
mean, std = np.mean(clean), np.std(clean)
stat, p = stats.kstest(clean, 'norm', args=(mean, std))
results['kolmogorov_smirnov'] = {
'statistic': round(stat, 4),
'p_value': round(p, 4),
'is_normal': p > alpha
}
return resultsโปรไฟล์ทางสถิติแบบเต็ม
รวมการวิเคราะห์ทางสถิติทั้งหมดไว้ในฟังก์ชัน full_profile() เดียว ซึ่งส่งคืนสรุปทางสถิติที่ครอบคลุมสำหรับคอลัมน์เชิงตัวเลขแต่ละคอลัมน์ในดาต้าเฟรม
def full_statistical_profile(df):
numeric = df.select_dtypes(include='number')
profiles = {}
for col in numeric.columns:
series = numeric[col].dropna()
profile = {
'count': len(series),
'null_count': df[col].isnull().sum(),
'mean': round(series.mean(), 4),
'median': round(series.median(), 4),
'std': round(series.std(), 4),
'min': round(series.min(), 4),
'max': round(series.max(), 4),
'range': round(series.max() - series.min(), 4),
'q1': round(series.quantile(0.25), 4),
'q3': round(series.quantile(0.75), 4)
}
profile['distribution'] = identify_distribution(series)
outliers = detect_outliers_iqr(df, columns=[col])
profile['outliers'] = outliers.get(col, {})
profiles[col] = profile
return {
'profiles': profiles,
'correlations': find_strong_correlations(df),
'shape': {'rows': len(df), 'columns': len(df.columns)}
}การสร้างข้อค้นพบอัตโนมัติ
ส่งโปรไฟล์ทางสถิติให้ LLM และขอให้สร้างข้อค้นพบด้วยภาษาที่เข้าใจง่าย โมเดลจะระบุรูปแบบ แจ้งเตือนความผิดปกติ และนำเสนอประเด็นที่ผู้ใช้ทางธุรกิจให้ความสำคัญ
import json
INSIGHT_PROMPT = '''You are a data analyst. Generate 3-5 key insights from this statistical summary.
Dataset: {dataset_name}
Statistical profile:
{profile_json}
For each insight:
- Be specific (use actual numbers)
- Focus on business relevance
- Flag any data quality concerns (high null counts, extreme outliers)
- Note unexpected patterns
Format as bullet points.'''
def generate_insights(df, dataset_name='dataset'):
profile = full_statistical_profile(df)
profile_text = json.dumps(profile, indent=2, default=str)[:3000]
return llm_call(INSIGHT_PROMPT.format(
dataset_name=dataset_name,
profile_json=profile_text
))
# Example output:
# - Revenue column has 3.2% outliers; max value ($48,500) is 12x the median ($3,900)
# - Customer age follows a right-skewed distribution (skewness: 1.4) with most users under 35
# - Strong positive correlation (r=0.87) between session_count and lifetime_valueการตรวจจับแนวโน้มของอนุกรมเวลา
สำหรับข้อมูลอนุกรมเวลา ให้ตรวจจับแนวโน้มโดยรวม (เพิ่มขึ้น ลดลง หรือคงที่) ด้วยการถดถอยเชิงเส้น วิธีนี้ทำให้เอเจนต์สามารถกล่าวได้ว่า "รายได้เพิ่มขึ้นเดือนละ $2,400"
import numpy as np
from scipy import stats
def detect_trend(values):
if len(values) < 3:
return {'trend': 'insufficient_data'}
x = np.arange(len(values))
y = np.array(values, dtype=float)
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
if p_value > 0.05:
trend = 'flat (no significant trend)'
elif slope > 0:
trend = 'increasing'
else:
trend = 'decreasing'
return {
'trend': trend,
'slope_per_period': round(slope, 4),
'r_squared': round(r_value ** 2, 4),
'p_value': round(p_value, 4),
'significant': p_value < 0.05
}
# Example: monthly revenue trend
monthly_revenue = [10000, 11200, 10800, 12500, 13100, 14200, 15000]
print(detect_trend(monthly_revenue))
# {'trend': 'increasing', 'slope_per_period': 834.2, 'r_squared': 0.95, ...}สถิติเชิงเปรียบเทียบ
บ่อยครั้งข้อค้นพบที่มีประโยชน์ที่สุดคือการเปรียบเทียบกลุ่ม: "มูลค่าคำสั่งซื้อเฉลี่ยของกลุ่ม A สูงกว่ากลุ่ม B อยู่ 40%" ใช้การทดสอบ t หรือ Mann-Whitney เพื่อเปรียบเทียบกลุ่ม
from scipy import stats
import pandas as pd
def compare_groups(df, group_column, value_column):
groups = df.groupby(group_column)[value_column].apply(list)
group_names = list(groups.index)
comparison = []
for i, g1 in enumerate(group_names):
for j, g2 in enumerate(group_names):
if j <= i:
continue
a, b = groups[g1], groups[g2]
mean_a = pd.Series(a).mean()
mean_b = pd.Series(b).mean()
# Mann-Whitney U (non-parametric, no normality assumption)
stat, p = stats.mannwhitneyu(a, b, alternative='two-sided')
pct_diff = (mean_a - mean_b) / mean_b * 100 if mean_b != 0 else 0
comparison.append({
'group_a': g1, 'group_b': g2,
'mean_a': round(mean_a, 2),
'mean_b': round(mean_b, 2),
'pct_difference': round(pct_diff, 1),
'p_value': round(p, 4),
'significant': p < 0.05
})
return comparisonการวิเคราะห์ค่าที่หายไป
ก่อนคำนวณสถิติใด ๆ ให้ตรวจสอบค่าที่หายไปก่อน อัตราค่าว่างที่สูงในคอลัมน์อาจทำให้สถิติที่คำนวณจากคอลัมน์นั้นใช้ไม่ได้ ให้รายงานจำนวนค่าว่าง เปอร์เซ็นต์ และรูปแบบของค่าว่าง (ค่าว่างเกิดขึ้นแบบสุ่มหรือเป็นระบบ)
import pandas as pd
import numpy as np
def analyze_missing_values(df):
total_rows = len(df)
missing_report = {}
for col in df.columns:
null_count = df[col].isnull().sum()
if null_count == 0:
continue
null_pct = null_count / total_rows * 100
# Check if nulls correlate with another column (systematic pattern)
pattern = 'random'
if null_pct > 5:
for other_col in df.select_dtypes(include='object').columns:
if other_col == col:
continue
null_rates = df.groupby(other_col)[col].apply(
lambda x: x.isnull().mean()
)
if null_rates.max() - null_rates.min() > 0.3: # 30% difference
pattern = f'correlated_with_{other_col}'
break
missing_report[col] = {
'null_count': int(null_count),
'null_pct': round(null_pct, 1),
'severity': 'high' if null_pct > 20 else 'medium' if null_pct > 5 else 'low',
'pattern': pattern
}
return missing_reportตรวจสอบความเข้าใจ
เหตุใดจึงนิยมใช้วิธี IQR มากกว่าคะแนน z ในการตรวจจับค่าผิดปกติสำหรับแอปพลิเคชันเอเจนต์
สรุป: เอเจนต์สรุปทางสถิติ
เอเจนต์สรุปทางสถิติผสานการทำงานต่อไปนี้: df.describe() สำหรับสถิติพื้นฐาน เมทริกซ์สหสัมพันธ์ สำหรับค้นหาความสัมพันธ์ การตรวจจับค่าผิดปกติด้วย IQR ซึ่งไม่ไวต่อค่าที่สุดโต่ง การระบุการกระจาย ด้วยความเบ้และการทดสอบภาวะปรกติ และการตรวจจับแนวโน้มด้วยการถดถอยเชิงเส้น
ส่งโปรไฟล์ทางสถิติให้ LLM เพื่อสร้างข้อค้นพบอัตโนมัติ โดยแปลงตัวเลขเป็นประเด็นทางธุรกิจที่อธิบายด้วยภาษาที่เข้าใจง่าย สถิติเชิงเปรียบเทียบ (การเปรียบเทียบกลุ่มพร้อมการทดสอบนัยสำคัญ) ช่วยเปิดเผยข้อค้นพบที่นำไปใช้ได้จริงมากที่สุด
เรียนรู้ AI Agents ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 60
- บทเรียน
- 239
คำถามที่พบบ่อย
บทเรียน “ตัวแทนสรุปผลทางสถิติ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวแทนสรุปผลทางสถิติ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวแทนสรุปผลทางสถิติ”
การวิเคราะห์การแจกแจง สหสัมพันธ์ และการตรวจจับค่าผิดปกติในรายงานที่สร้างโดยตัวแทน คุณปฏิบัติ AI Agents ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวแทนสรุปผลทางสถิติ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Agents นี้ได้ไหม
ได้ บทเรียน AI Agents ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รูปแบบตัวแปลโค้ดสำหรับการวิเคราะห์ข้อมูล
- เครื่องมือตัวแทนขับเคลื่อนด้วย Pandas
- การสร้างแผนภูมิและภาพข้อมูลอัตโนมัติ
- ตัวแทนสรุปผลทางสถิติ