Data Prep | Dagster Glossary

Back to Glossary Index

Data Prep

Transform your data so it is fit-for-purpose.

Data prep definition:

Data preparation refers to the process of cleaning, transforming, and enriching raw data into a desired format for downstream analytics or machine learning models, often involving steps like handling missing data, data type conversion, normalization, and feature extraction.

In data engineering, data preparation can refer to a broad range of activities on a large scale, using distributed systems. Data cleaning (or cleansing), imputating or transforming are just some techniques used in data prep.

Data prep example using Python:

Let's look at a simple example where we'll ingest a CSV file, clean the data, and transform it via imputation before using in a hypothetical machine learning model. Please note that you need to have the necessary Python libraries installed in your Python environment to run this code.

Let's say we have a simple input file called data.csv:

Age,Salary,City,Is_Smoker
25,50000,New York,Yes
32,70000,Los Angeles,No
29,,San Francisco,Yes
42,90000,New York,No
36,60000,Los Angeles,Yes
27,65000,New York,Yes
,75000,Los Angeles,No
33,80000,San Francisco,Yes
40,95000,New York,No

Let's assume that we want to predict the Is_Smoker column in the data. We can define target as this column and use the rest of the columns as features.

In this example, the Age and Salary columns are numeric, the City column is categorical, and the Is_Smoker column is binary. There are also some missing values in the Age and Salary columns. So our little model cannot run with these missing values.

We will run a simple data preparation script to fill these in with the mean value of the respective column.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Load data from CSV file
data = pd.read_csv('data.csv')
print("Original data:\n", data.head(), "\n")

# Handle missing data - Simple imputation: fill missing values with mean
data = data.fillna(data.mean())
print("Data after filling missing values:\n", data.head(), "\n")

# Define target variable
target = data['Is_Smoker']
data = data.drop('Is_Smoker', axis=1)

# Data Transformation - convert categorical data to numerical data using one-hot encoding
data = pd.get_dummies(data)
print("Data after one-hot encoding:\n", data.head(), "\n")

# Feature Scaling - Standardize features by removing the mean and scaling to unit variance
scaler = StandardScaler()
scaled_features = scaler.fit_transform(data)
print("Scaled features:\n", scaled_features[:5], "\n")  # Print the first 5 rows

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(scaled_features, target, test_size=0.2, random_state=42)
print(f"Training set size: {len(X_train)}, Testing set size: {len(X_test)}")

In this code, we first define the target as the Is_Smoker column from the data and then drop this column from the data. The remaining columns are used as features for the prediction. Note that the target column in this example is categorical, so you might need to convert it into numerical data if your model requires that.

In this example:

  • scaled_features and target are the feature matrix and the target vector respectively. They are split into training (X_train, y_train) and test (X_test, y_test) subsets.
  • test_size is a float between 0.0 and 1.0 and represents the proportion of the dataset to include in the test split. In this case, 20% of the data will be used for the test set and the remaining 80% for the training set.
  • random_state is the seed used by the random number generator for shuffling the data. Setting a random_state ensures that the splits you generate are reproducible.

Note: Scikit-Learn uses stratified sampling by default when splitting a dataset using the train_test_split() function when the target variable is binary or multiclass classification. Stratified sampling aims to ensure that each split is representative of all strata of the data. This is particularly useful when the data is imbalanced.

This script prints the first few rows of the original data, the data after filling missing values, and the data after one-hot encoding. It also prints the first 5 rows of the scaled features and the sizes of the training and testing sets.

Using train_test_split()

It is worth explaining the train_test_split() function from the sklearn.model_selection module of the Scikit-Learn library in Python. This function is used to split the data into two sets: a training set and a test set.

These two sets are used for different purposes in machine learning:

The training set is used to train the model, meaning that the model learns from this data to make predictions or decisions.

The test set is used to evaluate the model's performance, meaning that the model uses this data to make predictions and then compares its predictions to the actual values to assess its accuracy.

The train_test_split() function shuffles the dataset using a pseudorandom number generator before making the split. This is important to ensure that the training and testing sets are representative of the overall distribution of the data.

Since we included a number of output (print) statements, our example will produce the following:

Original data:
     Age   Salary           City Is_Smoker
0  25.0  50000.0       New York       Yes
1  32.0  70000.0    Los Angeles        No
2  29.0      NaN  San Francisco       Yes
3  42.0  90000.0       New York        No
4  36.0  60000.0    Los Angeles       Yes

Data after filling missing values:
     Age   Salary           City Is_Smoker
0  25.0  50000.0       New York       Yes
1  32.0  70000.0    Los Angeles        No
2  29.0  73125.0  San Francisco       Yes
3  42.0  90000.0       New York        No
4  36.0  60000.0    Los Angeles       Yes

Data after one-hot encoding:
     Age   Salary  City_Los Angeles  City_New York  City_San Francisco
0  25.0  50000.0                 0              1                   0
1  32.0  70000.0                 1              0                   0
2  29.0  73125.0                 0              0                   1
3  42.0  90000.0                 0              1                   0
4  36.0  60000.0                 1              0                   0

Scaled features:
 [[-1.5        -1.73607121 -0.70710678  1.11803399 -0.53452248]
 [-0.1875     -0.23460422  1.41421356 -0.89442719 -0.53452248]
 [-0.75        0.         -0.70710678 -0.89442719  1.87082869]
 [ 1.6875      1.26686278 -0.70710678  1.11803399 -0.53452248]
 [ 0.5625     -0.98533771  1.41421356 -0.89442719 -0.53452248]]

Training set size: 7, Testing set size: 2

Other data engineering terms related to
Data Management:
Dagster Glossary code icon

Append

Adding or attaching new records or data items to the end of an existing dataset, database table, file, or list.
An image representing the data engineering concept of 'Append'

Archive

Move rarely accessed data to a low-cost, long-term storage solution to reduce costs. Store data for long-term retention and compliance.
An image representing the data engineering concept of 'Archive'
Dagster Glossary code icon

Augment

Add new data or information to an existing dataset to enhance its value.
An image representing the data engineering concept of 'Augment'

Auto-materialize

The automatic execution of computations and the persistence of their results.
An image representing the data engineering concept of 'Auto-materialize'

Backup

Create a copy of data to protect against loss or corruption.
An image representing the data engineering concept of 'Backup'
Dagster Glossary code icon

Batch Processing

Process large volumes of data all at once in a single operation or batch.
An image representing the data engineering concept of 'Batch Processing'
Dagster Glossary code icon

Cache

Store expensive computation results so they can be reused, not recomputed.
An image representing the data engineering concept of 'Cache'
Dagster Glossary code icon

Categorize

Organizing and classifying data into different categories, groups, or segments.
An image representing the data engineering concept of 'Categorize'
Dagster Glossary code icon

Deduplicate

Identify and remove duplicate records or entries to improve data quality.
An image representing the data engineering concept of 'Deduplicate'

Deserialize

Deserialization is essentially the reverse process of serialization. See: 'Serialize'.
An image representing the data engineering concept of 'Deserialize'
Dagster Glossary code icon

Dimensionality

Analyzing the number of features or attributes in the data to improve performance.
An image representing the data engineering concept of 'Dimensionality'
Dagster Glossary code icon

Encapsulate

The bundling of data with the methods that operate on that data.
An image representing the data engineering concept of 'Encapsulate'
Dagster Glossary code icon

Enrich

Enhance data with additional information from external sources.
An image representing the data engineering concept of 'Enrich'

Export

Extract data from a system for use in another system or application.
An image representing the data engineering concept of 'Export'
Dagster Glossary code icon

Graph Theory

A powerful tool to model and understand intricate relationships within our data systems.
An image representing the data engineering concept of 'Graph Theory'
Dagster Glossary code icon

Idempotent

An operation that produces the same result each time it is performed.
An image representing the data engineering concept of 'Idempotent'
Dagster Glossary code icon

Index

Create an optimized data structure for fast search and retrieval.
An image representing the data engineering concept of 'Index'
Dagster Glossary code icon

Integrate

Combine data from different sources to create a unified view for analysis or reporting.
An image representing the data engineering concept of 'Integrate'
Dagster Glossary code icon

Lineage

Understand how data moves through a pipeline, including its origin, transformations, dependencies, and ultimate consumption.
An image representing the data engineering concept of 'Lineage'
Dagster Glossary code icon

Linearizability

Ensure that each individual operation on a distributed system appear to occur instantaneously.
An image representing the data engineering concept of 'Linearizability'
Dagster Glossary code icon

Materialize

Executing a computation and persisting the results into storage.
An image representing the data engineering concept of 'Materialize'
Dagster Glossary code icon

Memoize

Store the results of expensive function calls and reusing them when the same inputs occur again.
An image representing the data engineering concept of 'Memoize'
Dagster Glossary code icon

Merge

Combine data from multiple datasets into a single dataset.
An image representing the data engineering concept of 'Merge'
Dagster Glossary code icon

Model

Create a conceptual representation of data objects.
An image representing the data engineering concept of 'Model'

Monitor

Track data processing metrics and system health to ensure high availability and performance.
An image representing the data engineering concept of 'Monitor'
Dagster Glossary code icon

Named Entity Recognition

Locate and classify named entities in text into pre-defined categories.
An image representing the data engineering concept of 'Named Entity Recognition'
Dagster Glossary code icon

Parse

Interpret and convert data from one format to another.
Dagster Glossary code icon

Partition

Data partitioning is a technique that data engineers and ML engineers use to divide data into smaller subsets for improved performance.
An image representing the data engineering concept of 'Partition'
Dagster Glossary code icon

Preprocess

Transform raw data before data analysis or machine learning modeling.
Dagster Glossary code icon

Replicate

Create a copy of data for redundancy or distributed processing.

Scaling

Increasing the capacity or performance of a system to handle more data or traffic.
Dagster Glossary code icon

Schema Inference

Automatically identify the structure of a dataset.
An image representing the data engineering concept of 'Schema Inference'
Dagster Glossary code icon

Schema Mapping

Translate data from one schema or structure to another to facilitate data integration.
Dagster Glossary code icon

Secondary Index

Improve the efficiency of data retrieval in a database or storage system.
An image representing the data engineering concept of 'Secondary Index'
Dagster Glossary code icon

Software-defined Asset

A declarative design pattern that represents a data asset through code.
An image representing the data engineering concept of 'Software-defined Asset'

Synchronize

Ensure that data in different systems or databases are in sync and up-to-date.
Dagster Glossary code icon

Validate

Check data for completeness, accuracy, and consistency.
An image representing the data engineering concept of 'Validate'
Dagster Glossary code icon

Version

Maintain a history of changes to data for auditing and tracking purposes.
An image representing the data engineering concept of 'Version'