Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S19L04 -LabelEncoding classes

Machine Leaning Articles
1 views

S19L04 -LabelEncoding classes


May 13, 2025

Mastering Label Encoding in Machine Learning: A Comprehensive Guide

Table of Contents

  1. Introduction to Label Encoding
  2. Understanding the Dataset
  3. Handling Missing Data
    • Numeric Data
    • Categorical Data
  4. Encoding Categorical Variables
    • One-Hot Encoding
    • Label Encoding
    • Selecting the Right Encoding Technique
  5. Feature Selection
  6. Building and Evaluating a KNN Model
    • Train-Test Split
    • Feature Scaling
    • Model Training and Evaluation
  7. Visualizing Decision Regions
  8. Conclusion

Introduction to Label Encoding

In machine learning, Label Encoding is a technique used to convert categorical data into numerical format. Since many algorithms cannot work directly with categorical data, encoding these categories into numbers becomes a necessity. Label encoding assigns a unique integer to each category, facilitating the model's ability to interpret and process the data efficiently.

Key Concepts:

  • Categorical Data: Variables that represent categories, such as "Yes/No," "Red/Blue/Green," etc.
  • Numerical Encoding: The process of converting categorical data into numerical values.

Understanding the Dataset

For this guide, we'll use the Weather AUS dataset sourced from Kaggle. This dataset encompasses various weather-related attributes across different Australian locations and dates.

Dataset Overview:

  • URL: Weather AUS Dataset
  • Features: Date, Location, Temperature metrics, Rainfall, Wind details, Humidity, Pressure, Cloud cover, and more.
  • Target Variable: RainTomorrow indicating whether it rained the next day.

Handling Missing Data

Real-world datasets often contain missing values, which can hinder the performance of machine learning models. Properly handling these missing values is crucial for building robust models.

Numeric Data

Strategy: Impute missing values using the mean of the column.

Implementation:

import numpy as np
from sklearn.impute import SimpleImputer

# Initialize the imputer with mean strategy
imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')

# Identify numerical columns
numerical_cols = list(np.where((X.dtypes == np.int64) | (X.dtypes == np.float64))[0])

# Fit and transform the data
imp_mean.fit(X.iloc[:, numerical_cols])
X.iloc[:, numerical_cols] = imp_mean.transform(X.iloc[:, numerical_cols])

Categorical Data

Strategy: Impute missing values using the most frequent category.

Implementation:

# Identify string columns
string_cols = list(np.where((X.dtypes == object))[0])

# Initialize the imputer with the most frequent strategy
imp_mean = SimpleImputer(missing_values=np.nan, strategy='most_frequent')

# Fit and transform the data
imp_mean.fit(X.iloc[:, string_cols])
X.iloc[:, string_cols] = imp_mean.transform(X.iloc[:, string_cols])

Encoding Categorical Variables

After handling missing data, the next step involves encoding categorical variables to prepare them for machine learning algorithms.

One-Hot Encoding

One-Hot Encoding transforms categorical variables into a format that can be provided to ML algorithms to do a better job in prediction.

Implementation:

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder

def OneHotEncoderMethod(indices, data):
    columnTransformer = ColumnTransformer(
        [('encoder', OneHotEncoder(), indices)], 
        remainder='passthrough'
    )
    return columnTransformer.fit_transform(data)

Label Encoding

Label Encoding converts each value of a categorical column into a unique integer. It's particularly useful for binary categorical variables.

Implementation:

from sklearn import preprocessing

def LabelEncoderMethod(series):
    le = preprocessing.LabelEncoder()
    le.fit(series)
    return le.transform(series)

Selecting the Right Encoding Technique

Choosing between One-Hot Encoding and Label Encoding depends on the nature of the categorical data.

Guidelines:

  • Binary Categories: Label Encoding is sufficient.
  • Multiple Categories: One-Hot Encoding is preferable to avoid introducing ordinal relationships.

Implementation:

def EncodingSelection(X, threshold=10):
    # Select string columns
    string_cols = list(np.where((X.dtypes == object))[0])
    one_hot_encoding_indices = []
    
    # Decide encoding method based on unique values
    for col in string_cols:
        unique_length = len(pd.unique(X[X.columns[col]]))
        if unique_length == 2 or unique_length > threshold:
            X[X.columns[col]] = LabelEncoderMethod(X[X.columns[col]])
        else:
            one_hot_encoding_indices.append(col)
            
    # Apply One-Hot Encoding
    X = OneHotEncoderMethod(one_hot_encoding_indices, X)
    return X

X = EncodingSelection(X)

Feature Selection

Selecting the most relevant features enhances model performance and reduces computational complexity.

Technique: SelectKBest with Chi-Squared (chi2) as the scoring function.

Implementation:

from sklearn.feature_selection import SelectKBest, chi2
from sklearn import preprocessing

# Initialize SelectKBest
kbest = SelectKBest(score_func=chi2, k=10)

# Initialize Min-Max Scaler
MMS = preprocessing.MinMaxScaler()

# Scale features
x_temp = MMS.fit_transform(X)

# Fit SelectKBest
x_temp = kbest.fit(x_temp, y)

# Identify best features
best_features = np.argsort(x_temp.scores_)[-K_features:]
features_to_delete = best_features = np.argsort(x_temp.scores_)[:-K_features]

# Reduce dataset
X = np.delete(X, features_to_delete, axis=1)

Building and Evaluating a KNN Model

With the dataset preprocessed and features selected, we proceed to build and evaluate a K-Nearest Neighbors (KNN) classifier.

Train-Test Split

Splitting the dataset ensures that the model is evaluated on unseen data, providing a measure of its generalization capability.

Implementation:

from sklearn.model_selection import train_test_split

# Split the data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.20, random_state=1
)

print(X_train.shape)  # Output: (113754, 12)

Feature Scaling

Feature scaling standardizes the range of the features, which is essential for algorithms like KNN that are sensitive to the scale of data.

Implementation:

from sklearn import preprocessing

# Initialize StandardScaler
sc = preprocessing.StandardScaler(with_mean=False)

# Fit and transform the training data
sc.fit(X_train)
X_train = sc.transform(X_train)

# Transform the test data
X_test = sc.transform(X_test)

print(X_train.shape)  # Output: (113754, 12)
print(X_test.shape)   # Output: (28439, 12)

Model Training and Evaluation

Implementation:

from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

# Initialize KNN classifier
knnClassifier = KNeighborsClassifier(n_neighbors=3)

# Train the model
knnClassifier.fit(X_train, y_train)

# Predict on test data
y_pred = knnClassifier.predict(X_test)

# Evaluate accuracy
accuracy = accuracy_score(y_pred, y_test)
print(f"Accuracy: {accuracy}")

Output:

Accuracy: 0.8258

An accuracy of approximately 82.58% indicates that the model performs reasonably well in predicting whether it will rain the next day based on the provided features.


Visualizing Decision Regions

Visualizing decision regions can provide insights into how the KNN model is making predictions. Although it's more illustrative with fewer features, here's a sample code snippet for visualization.

Implementation:

# Install mlxtend if not already installed
# pip install mlxtend

from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt

# Plotting decision regions (Example with first two features)
plot_decision_regions(X_train[:, :2], y_train, clf=knnClassifier, legend=2)

# Adding axis labels
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('KNN Decision Regions')
plt.show()

Note: Visualization is most effective with two features. For datasets with more features, consider dimensionality reduction techniques like PCA before visualization.


Conclusion

Label Encoding is a fundamental technique in the data preprocessing arsenal, enabling machine learning models to interpret categorical data effectively. By systematically handling missing data, selecting relevant features, and appropriately encoding categorical variables, you set a strong foundation for building robust predictive models. Incorporating these practices into your workflow not only enhances model performance but also ensures scalability and efficiency in your machine learning projects.

Key Takeaways:

  • Label Encoding transforms categorical data into numerical format, essential for ML algorithms.
  • Handling Missing Data appropriately can prevent skewed model outcomes.
  • Encoding Techniques should be chosen based on the nature and number of categories.
  • Feature Selection improves model performance by eliminating irrelevant or redundant features.
  • KNN Model effectiveness is influenced by proper preprocessing and feature scaling.

Embark on your machine learning journey by mastering these preprocessing techniques, and unlock the potential to build models that are both accurate and reliable.


Enhance Your Learning:

  • Explore more preprocessing techniques in our Advanced Data Preprocessing Guide.
  • Dive deeper into machine learning algorithms with our Comprehensive ML Models Tutorial.

Happy Coding!

Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.