- ទំព័រដើម
- Data Science Roadmap
- ជំហានទី ៤
វិភាគ និងគូរក្រាបទិន្នន័យ (EDA & Visual)
រៀនសម្អាតទិន្នន័យកខ្វក់ (Missing values & Outliers), វិភាគទំនាក់ទំនង (EDA · Correlation) និងគូរក្រាបស្អាតៗដោយ Matplotlib/Seaborn និង ggplot2 ។
១. ការសម្អាតទិន្នន័យ (Missing Values & Duplicates)
ទិន្នន័យពិតតែងមានតម្លៃទទេ (NaN / NA) និងជួរស្ទួន (Duplicates)។ វិធីដោះស្រាយទូទៅគឺជំនួសដោយ Median/Mean (Imputation) និងលុបជួរស្ទួន ៖
import pandas as pd, numpy as np
df = pd.DataFrame({
'id': [1, 2, 2, 3, 4],
'score': [80.0, np.nan, np.nan, 90.0, 70.0]
})
df = df.drop_duplicates()
df['score'] = df['score'].fillna(df['score'].median())
print(df)library(dplyr) df <- data.frame( id = c(1, 2, 2, 3, 4), score = c(80, NA, NA, 90, 70) ) %>% distinct() %>% mutate(score = ifelse(is.na(score), median(score, na.rm = TRUE), score)) print(df)
២. ការវិភាគទំនាក់ទំនង (Pearson Correlation) & IQR Outliers
មេគុណទំនាក់ទំនង Pearson \(r \in [-1, 1]\) វាស់កម្រិតទំនាក់ទំនងលីនេអ៊ែររវាងអថេរ ២ (ឧ. ម៉ោងរៀន និងពិន្ទុប្រឡង) ៖
import pandas as pd
df = pd.DataFrame({
'hours': [1, 2, 3, 4, 5],
'score': [52, 60, 71, 82, 95]
})
print('Correlation matrix:\n', df.corr().round(4))df <- data.frame(
hours = c(1, 2, 3, 4, 5),
score = c(52, 60, 71, 82, 95)
)
cat('Pearson r =', round(cor(df$hours, df$score), 4), '\n')៣. ការគូរក្រាប (Data Visualization: Matplotlib/Seaborn vs ggplot2)
ក្រាបជួយមើលលំនាំទិន្នន័យភ្លាមៗ ៖ Scatter Plot សម្រាប់ទំនាក់ទំនង ២ អថេរ, Histogram / Boxplot សម្រាប់របាយទិន្នន័យ ៖
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(style='whitegrid')
sns.regplot(x='hours', y='score', data=df)
plt.title('Study Hours vs Math Score')
plt.xlabel('Hours per week')
plt.ylabel('Exam Score')
plt.tight_layout()
plt.savefig('hours_score.png', dpi=150)library(ggplot2)
p <- ggplot(df, aes(x = hours, y = score)) +
geom_point(color = '#4c2a9a', size = 3) +
geom_smooth(method = 'lm', color = '#0f766e') +
labs(title = 'Study Hours vs Math Score',
x = 'Hours per week', y = 'Exam Score') +
theme_minimal()
ggsave('hours_score.png', plot = p, width = 6, height = 4, dpi = 150)📝 លំហាត់អនុវត្ត (Python & R)
គេមានពិន្ទុ [60, 62, 65, 68, 70, 72, 120]។ ដោយប្រើ \(\text{IQR} = Q_3 - Q_1\) និងព្រំដែនលើ \(Q_3 + 1.5\times\text{IQR}\) ចូរពិនិត្យថាតើ 120 ជា Outlier ឬទេ?
▸ បើកមើលដំណោះស្រាយ (គណិតវិទ្យា + Python & R)
គណិតវិទ្យា៖ \(Q_1=63.5,\ Q_3=71\) (ឬ percentile linear: 63.5, 71) \(\Rightarrow \text{IQR}=7.5\) ព្រំដែនលើ \(= 71 + 1.5(7.5) = 82.25\) ។ ដោយ \(120 > 82.25\) នោះ 120 ជា Outlier ពិតប្រាកដ!
import numpy as np
x = np.array([60, 62, 65, 68, 70, 72, 120])
q1, q3 = np.percentile(x, [25, 75])
iqr = q3 - q1
upper = q3 + 1.5 * iqr
print('Upper bound:', upper, '| Outliers:', x[x > upper])x <- c(60, 62, 65, 68, 70, 72, 120)
q <- quantile(x, c(0.25, 0.75))
upper <- q[2] + 1.5 * (q[2] - q[1])
cat('Upper bound:', upper, '| Outliers:', x[x > upper], '\n')សិស្សថ្នាក់ 12A មានពិន្ទុ [80, NaN, 90]។ ជំនួស NaN ដោយមធ្យមភាគនៃក្រុម 12A។
▸ បើកមើលដំណោះស្រាយ (គណិតវិទ្យា + Python & R)
ចម្លើយ៖ មធ្យមភាគនៃ 80 និង 90 គឺ 85។
import pandas as pd, numpy as np s = pd.Series([80.0, np.nan, 90.0]) print(s.fillna(s.mean()).tolist())
s <- c(80, NA, 90) s[is.na(s)] <- mean(s, na.rm = TRUE) print(s)