Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Data Transformation

Introduction

Data transformation is a crucial step in the data cleaning process. It involves converting data from one format or structure into another format or structure. The purpose of data transformation is to organize data, make it more usable, and prepare it for analysis. This tutorial will guide you through various data transformation techniques using practical examples.

Normalization

Normalization is the process of scaling individual samples to have unit norm. This technique is useful when you want to ensure that all your data is on the same scale.

Example: Normalizing a list of numbers in Python.

import numpy as np

data = np.array([1, 2, 3, 4, 5])
normalized_data = data / np.linalg.norm(data)
print(normalized_data)
                    
[0.13483997 0.26967994 0.40451992 0.53935989 0.67419986]
                    

Standardization

Standardization involves rescaling data to have a mean of zero and a standard deviation of one. This technique is commonly used in preparation for machine learning algorithms.

Example: Standardizing a list of numbers in Python.

from sklearn.preprocessing import StandardScaler

data = np.array([[1, 2], [3, 4], [5, 6]])
scaler = StandardScaler()
standardized_data = scaler.fit_transform(data)
print(standardized_data)
                    
[[-1.22474487 -1.22474487]
 [ 0.          0.        ]
 [ 1.22474487  1.22474487]]
                    

Encoding Categorical Data

Encoding categorical data involves converting categorical values into numerical values. This step is essential because many machine learning algorithms cannot operate on label data directly.

Example: Encoding categorical data using One-Hot Encoding in Python.

from sklearn.preprocessing import OneHotEncoder

data = np.array([['cat'], ['dog'], ['cat'], ['bird']])
encoder = OneHotEncoder(sparse=False)
encoded_data = encoder.fit_transform(data)
print(encoded_data)
                    
[[0. 0. 1.]
 [0. 1. 0.]
 [0. 0. 1.]
 [1. 0. 0.]]
                    

Handling Missing Values

Handling missing values is an essential part of data cleaning. Common techniques include removing missing values, imputing them with a specific value, or using algorithms to predict them.

Example: Imputing missing values with the mean in Python.

from sklearn.impute import SimpleImputer

data = np.array([[1, 2], [3, np.nan], [7, 6]])
imputer = SimpleImputer(strategy='mean')
imputed_data = imputer.fit_transform(data)
print(imputed_data)
                    
[[1. 2.]
 [3. 4.]
 [7. 6.]]
                    

Feature Engineering

Feature engineering involves creating new features or modifying existing ones to improve the performance of machine learning models. This can include techniques such as polynomial features, interaction terms, and more.

Example: Creating polynomial features in Python.

from sklearn.preprocessing import PolynomialFeatures

data = np.array([[2, 3], [3, 4], [4, 5]])
poly = PolynomialFeatures(degree=2)
poly_features = poly.fit_transform(data)
print(poly_features)
                    
[[ 1.  2.  3.  4.  6.  9.]
 [ 1.  3.  4.  9. 12. 16.]
 [ 1.  4.  5. 16. 20. 25.]]
                    

Conclusion

Data transformation is a critical step in the data cleaning process. This tutorial covered various techniques such as normalization, standardization, encoding categorical data, handling missing values, and feature engineering. By mastering these techniques, you can ensure that your data is well-prepared for analysis and machine learning tasks.