1. ទំព័រដើម
  2. Data Science Roadmap
  3. ជំហានទី ៤
Data Science Roadmap · ជំហានទី ៤ នៃ ៦ (Python 🐍 & R 📊)

វិភាគ និងគូរក្រាបទិន្នន័យ (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)
id score 0 1 80.0 1 2 80.0 3 3 90.0 4 4 70.0
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)
id score 1 1 80 2 2 80 3 3 90 4 4 70

២. ការវិភាគទំនាក់ទំនង (Pearson Correlation) & IQR Outliers

មេគុណទំនាក់ទំនង Pearson \(r \in [-1, 1]\) វាស់កម្រិតទំនាក់ទំនងលីនេអ៊ែររវាងអថេរ ២ (ឧ. ម៉ោងរៀន និងពិន្ទុប្រឡង) ៖

$$r = \frac{\sum (x_i-\bar{x})(y_i-\bar{y})}{\sqrt{\sum (x_i-\bar{x})^2}\sqrt{\sum (y_i-\bar{y})^2}}$$
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))
Correlation matrix: hours score hours 1.0000 0.9968 score 0.9968 1.0000
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')
Pearson r = 0.9968

៣. ការគូរក្រាប (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)
Saved crisp scatter + regression trendline figure: hours_score.png (150 dpi)
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)
Saved ggplot2 scatter + regression band figure: hours_score.png (6x4 in)

📝 លំហាត់អនុវត្ត (Python & R)

លំហាត់ទី ១ ៖ រកតម្លៃ Outliers ដោយវិធី IQR

គេមានពិន្ទុ [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])
Upper bound: 82.25 | Outliers: [120]
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')
Upper bound: 82.25 | Outliers: 120
លំហាត់ទី ២ ៖ បំពេញទិន្នន័យទទេតាមក្រុម (Group Imputation)

សិស្សថ្នាក់ 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())
[80.0, 85.0, 90.0]
s <- c(80, NA, 90)
s[is.na(s)] <- mean(s, na.rm = TRUE)
print(s)
[1] 80 85 90