3. DATA PREPROCESSING
3.1) Data Cleaning
3.1.1) Missing Values
- Missing values can be due to incomplete forms, sensor errors, etc.
- Techniques to handle missing data:
- Remove rows/columns with too many missing values
- Fill (impute) missing values using:
- Mean/Median/Mode
- Forward/Backward fill
- Predictive models (like KNN)
3.1.2) Outliers
- Outliers are data points that are very different from the others.
- They can distort results and reduce model performance.
- Detection methods:
- Box plot, Z-score, IQR method
- Handling outliers:
- Remove them
- Transform data (e.g., log scaling)
- Cap them (set a maximum/minimum)
3.2) Data Normalization and Standardization
- Normalization (Min-Max Scaling): Scales all values between 0 and 1
- Formula:
(x - min)/(max - min)
Used when the data is not normally distributed
- Standardization (Z-score Scaling): Centers the data around mean = 0 and standard deviation = 1
- Formula:
(x - mean) / std
Used when data is normally distributed
3.3) Encoding Categorical Variables
ML models work with numbers, not text. Categorical data needs to be converted into numerical form.
Label Encoding: Assigns each unique category a number.
Example:
Red ->0, Blue -> 1, Green -> 2
One-Hot Encoding: Creates new binary columns for each category.
Example:
Red -> [1,0,0], Blue -> [0,1,0], Green -> [0,0,1]
Label encoding is good for ordinal data (ranked), while one-hot encoding is best for nominal data (non-ranked).
3.4) Feature Scaling
Min-Max Scaling:
- Scales feature between 0 and 1.
- Good for algorithms like KNN, neural networks.
- Useful for models that assume normality, like linear regression or logistic regression.
3.5) Feature Engineering
Polynomial Features:
- Create new features by raising existing features to a power.
- Example: From x, create x², x³
- Convert continuous data into categories.
- Example: Age → [0–18], [19–35], [36–60], 60+
3.6) Handling Imbalanced Data
Undersampling: Remove some samples from the majority class.
Oversampling: Duplicate or generate more samples of the minority class.
Balancing data improves the ability of the model to correctly predict both classes.
