Bird
Raised Fist0
Prompt Engineering / GenAIml~6 mins

Training data preparation in Prompt Engineering / GenAI - Full Explanation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Imagine trying to teach someone a new skill without giving them the right materials or examples. Training data preparation solves this problem by organizing and cleaning the information that a machine learning model needs to learn effectively.
Explanation
Data Collection
This step involves gathering raw data from various sources like text, images, or audio. The quality and variety of this data directly affect how well the model will learn and perform.
Collecting diverse and relevant data is the foundation for good training.
Data Cleaning
Raw data often contains errors, duplicates, or irrelevant parts. Cleaning removes these issues to ensure the model learns from accurate and useful information.
Cleaning data improves the model's accuracy by removing noise.
Data Labeling
Labeling means adding tags or categories to data so the model knows what to learn from each example. For instance, labeling images as 'cat' or 'dog' helps the model recognize them later.
Proper labeling guides the model to understand the data correctly.
Data Splitting
The prepared data is divided into parts: training data to teach the model, validation data to tune it, and test data to check its performance. This prevents the model from just memorizing the data.
Splitting data helps evaluate the model's ability to generalize.
Data Augmentation
Sometimes, there isn't enough data, so new examples are created by slightly changing existing ones. This helps the model learn better by seeing more varied examples.
Augmentation increases data variety to improve learning.
Real World Analogy

Imagine preparing ingredients before cooking a meal. You gather fresh vegetables, wash and chop them, label containers for spices, divide portions for different dishes, and sometimes add extra herbs to enhance flavor. This preparation ensures the meal turns out delicious.

Data Collection → Gathering fresh vegetables and ingredients from the market
Data Cleaning → Washing and removing spoiled parts from vegetables
Data Labeling → Labeling spice containers so you know what each is
Data Splitting → Dividing ingredients into portions for different dishes
Data Augmentation → Adding extra herbs or spices to enhance the meal
Diagram
Diagram
┌───────────────┐
│ Data Collection│
└──────┬────────┘
       │
┌──────▼───────┐
│ Data Cleaning│
└──────┬───────┘
       │
┌──────▼───────┐
│ Data Labeling│
└──────┬───────┘
       │
┌──────▼───────┐
│ Data Splitting│
└──────┬───────┘
       │
┌──────▼──────────┐
│ Data Augmentation│
└─────────────────┘
This diagram shows the step-by-step flow of preparing training data from collection to augmentation.
Key Facts
Training DataThe examples used to teach a machine learning model.
Data CleaningThe process of fixing or removing incorrect or irrelevant data.
Data LabelingAdding descriptive tags to data to help the model learn.
Data SplittingDividing data into training, validation, and test sets.
Data AugmentationCreating new data examples by modifying existing ones.
Common Confusions
Believing that more data always means better model performance.
Believing that more data always means better model performance. Quality matters more than quantity; poor or noisy data can harm learning even if there is a lot of it.
Thinking data labeling is optional for all machine learning tasks.
Thinking data labeling is optional for all machine learning tasks. Labeling is essential for supervised learning but not needed for unsupervised learning.
Assuming data splitting is just random and unimportant.
Assuming data splitting is just random and unimportant. Proper splitting ensures fair evaluation and prevents the model from memorizing data.
Summary
Training data preparation organizes and cleans data so models learn effectively.
Key steps include collecting, cleaning, labeling, splitting, and augmenting data.
Good preparation improves model accuracy and helps it work well on new data.

Practice

(1/5)
1. What is the main purpose of training data preparation in machine learning?
easy
A. To clean and organize data for better model learning
B. To create the final model architecture
C. To deploy the model to production
D. To write the code for model training

Solution

  1. Step 1: Understand the role of training data preparation

    Training data preparation involves cleaning and organizing data so the model can learn effectively.
  2. Step 2: Differentiate from other steps in machine learning

    Creating model architecture, deployment, and coding are separate steps after data preparation.
  3. Final Answer:

    To clean and organize data for better model learning -> Option A
  4. Quick Check:

    Training data preparation = cleaning and organizing data [OK]
Hint: Focus on data cleaning and organizing for training [OK]
Common Mistakes:
  • Confusing data preparation with model building
  • Thinking deployment is part of data preparation
  • Assuming coding is data preparation
2. Which of the following is the correct way to split data into training and testing sets in Python using scikit-learn?
easy
A. split_train_test(data, 0.2)
B. train_test(data, split=0.2)
C. train_test_split(data, test_size=0.2)
D. test_train_split(data, size=0.2)

Solution

  1. Step 1: Recall the scikit-learn function for splitting data

    The correct function is train_test_split with parameters like test_size.
  2. Step 2: Check the syntax of each option

    Only train_test_split(data, test_size=0.2) uses the correct function name and parameter syntax.
  3. Final Answer:

    train_test_split(data, test_size=0.2) -> Option C
  4. Quick Check:

    Correct function and parameter = train_test_split(data, test_size=0.2) [OK]
Hint: Remember scikit-learn's train_test_split function name [OK]
Common Mistakes:
  • Using wrong function names
  • Incorrect parameter names
  • Mixing order of parameters
3. Given the code below, what will be the output of print(X_train.shape, X_test.shape)?
from sklearn.model_selection import train_test_split
import numpy as np
X = np.arange(20).reshape(10, 2)
X_train, X_test = train_test_split(X, test_size=0.3, random_state=42)
medium
A. (7, 2) (3, 2)
B. (3, 2) (7, 2)
C. (10, 2) (0, 2)
D. (5, 2) (5, 2)

Solution

  1. Step 1: Understand the data shape and split ratio

    The data X has 10 rows and 2 columns. test_size=0.3 means 30% data for testing (3 rows) and 70% for training (7 rows).
  2. Step 2: Calculate the shapes of training and testing sets

    Training set shape: (7, 2), Testing set shape: (3, 2).
  3. Final Answer:

    (7, 2) (3, 2) -> Option A
  4. Quick Check:

    70% train = 7 rows, 30% test = 3 rows [OK]
Hint: Calculate rows by multiplying total by split ratio [OK]
Common Mistakes:
  • Swapping train and test sizes
  • Ignoring the shape's second dimension
  • Misunderstanding test_size meaning
4. Identify the error in the following code snippet for normalizing data using MinMaxScaler:
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]
X_scaled = scaler.fit_transform(X)
print(X_scaled)
medium
A. MinMaxScaler cannot handle negative values
B. MinMaxScaler requires data as a numpy array, not list
C. fit_transform should be called on scaler.fit(X).transform(X)
D. No error, code runs correctly

Solution

  1. Step 1: Check input data type compatibility

    MinMaxScaler accepts lists or numpy arrays as input, so list input is valid.
  2. Step 2: Verify method usage

    Calling scaler.fit_transform(X) is the correct way to fit and transform data in one step.
  3. Final Answer:

    No error, code runs correctly -> Option D
  4. Quick Check:

    MinMaxScaler works with lists and fit_transform method [OK]
Hint: MinMaxScaler accepts lists and arrays directly [OK]
Common Mistakes:
  • Thinking input must be numpy array
  • Misusing fit and transform methods
  • Assuming scaler rejects negative values
5. You have a dataset with categorical text features and numeric features. Which sequence of steps correctly prepares the data for training a machine learning model?
hard
A. Split data, encode categorical features, normalize numeric features, then clean missing values
B. Clean missing values, encode categorical features, normalize numeric features, then split data
C. Normalize numeric features, clean missing values, split data, then encode categorical features
D. Encode categorical features, split data, clean missing values, then normalize numeric features

Solution

  1. Step 1: Clean missing values first

    Cleaning missing data ensures no errors during encoding or normalization.
  2. Step 2: Encode categorical features before normalization

    Categorical data must be converted to numbers before normalization.
  3. Step 3: Normalize numeric features and then split data

    Normalization scales numeric data; splitting last avoids data leakage.
  4. Final Answer:

    Clean missing values, encode categorical features, normalize numeric features, then split data -> Option B
  5. Quick Check:

    Proper order: clean -> encode -> normalize -> split [OK]
Hint: Always clean first, encode before normalize, split last [OK]
Common Mistakes:
  • Splitting data before cleaning causes leakage
  • Normalizing before encoding categorical data
  • Encoding after splitting leads to inconsistent categories