Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Understand feature engineering, numerical and categorical variables, scaling, normalization, and handling missing data.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Average value of feature | ||
| Standard Deviation | Spread of values | |
| Extreme bounds | Minimum and maximum values in dataset |
Standardizing an income of $80,000 where mean is $50,000 and standard deviation is $15,000.
Result: Income is 2.0 standard deviations above the mean
Inspect distributions, missing value percentages, and data types.
Convert categorical strings to numerical dummy vectors via one-hot encoding.
Apply StandardScaler or MinMaxScaler to prevent high-magnitude features from dominating gradients.
Problem: Dataset contains mixed text (Employment: "Engineer", "Nurse") and numbers (Income: $85,000, Age: 34).
Solution: Pipeline one-hot encodes job titles and standardizes income/age for input into neural networks.
Problem: Raw data contains missing customer tenure values.
Solution: Median imputation fills missing fields without distorting underlying distributions.
Interactive activity component
Preprocess raw tabular data with missing values, categorical features, and numerical scaling using Pandas and Scikit-Learn.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install pandas scikit-learn numpy1 import pandas as pd 2 from sklearn.preprocessing import OneHotEncoder, StandardScaler
import pandas as pd → Pandas for tabular data processing
from sklearn.preprocessing import OneHotEncoder, StandardScaler → Scikit-Learn preprocessing transformers
1 # Sample dataset with missing values and categorical data 2 data = {'Age': [25, 30, 35, 40, None], 'Salary': [50000, 60000, 70000, None, 90000], 'Gender': ['Male', 'Female', 'Male', 'Female', 'Male']} 3 df = pd.DataFrame(data) 4 5 # Handling missing values (fill with column mean) 6 df["Age"] = df["Age"].fillna(df["Age"].mean()) 7 df["Salary"] = df["Salary"].fillna(df["Salary"].mean()) 8 9 # Encoding categorical variable 10 encoder = OneHotEncoder(sparse_output=False) 11 gender_encoded = encoder.fit_transform(df[['Gender']]) 12 df_gender = pd.DataFrame(gender_encoded, columns=['Male', 'Female']) 13 14 # Scaling numerical features with StandardScaler 15 scaler = StandardScaler() 16 df[['Age', 'Salary']] = scaler.fit_transform(df[['Age', 'Salary']]) 17 18 # Concatenating final processed data 19 df_final = pd.concat([df[['Age', 'Salary']], df_gender], axis=1) 20 print("Preprocessed Data:\n", df_final)
# Sample dataset with missing values and categorical data
Sample data
data = {'Age': [25, 30, 35, 40, None], 'Salary': [50000, 60000, 70000, None, 90000], 'Gender': ['Male', 'Female', 'Male', 'Female', 'Male']}
Raw dictionary
df = pd.DataFrame(data)
Create DataFrame
# Handling missing values (fill with column mean)
Mean imputation
df["Age"] = df["Age"].fillna(df["Age"].mean())
Fill missing Age
df["Salary"] = df["Salary"].fillna(df["Salary"].mean())
Fill missing Salary
# Encoding categorical variable
One-hot encoding
encoder = OneHotEncoder(sparse_output=False)
Instantiate dense OneHotEncoder
gender_encoded = encoder.fit_transform(df[['Gender']])
Fit & transform gender
df_gender = pd.DataFrame(gender_encoded, columns=['Male', 'Female'])
Categorical DataFrame
# Scaling numerical features with StandardScaler
Standardization
scaler = StandardScaler()
Instantiate StandardScaler
df[['Age', 'Salary']] = scaler.fit_transform(df[['Age', 'Salary']])
Scale age and salary
# Concatenating final processed data
Merge features
df_final = pd.concat([df[['Age', 'Salary']], df_gender], axis=1)
Combine numerical + encoded features
print("Preprocessed Data:\n", df_final)
Display preprocessed DataFrame
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Data Preprocessing: Handling Missing Values, One-Hot Encoding & Scaling 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install pandas scikit-learn numpy 8 9 # Step 2: Import libraries 10 import pandas as pd 11 from sklearn.preprocessing import OneHotEncoder, StandardScaler 12 13 # Step 3: Run Demonstration 14 # Sample dataset with missing values and categorical data 15 data = {'Age': [25, 30, 35, 40, None], 'Salary': [50000, 60000, 70000, None, 90000], 'Gender': ['Male', 'Female', 'Male', 'Female', 'Male']} 16 df = pd.DataFrame(data) 17 18 # Handling missing values (fill with column mean) 19 df["Age"] = df["Age"].fillna(df["Age"].mean()) 20 df["Salary"] = df["Salary"].fillna(df["Salary"].mean()) 21 22 # Encoding categorical variable 23 encoder = OneHotEncoder(sparse_output=False) 24 gender_encoded = encoder.fit_transform(df[['Gender']]) 25 df_gender = pd.DataFrame(gender_encoded, columns=['Male', 'Female']) 26 27 # Scaling numerical features with StandardScaler 28 scaler = StandardScaler() 29 df[['Age', 'Salary']] = scaler.fit_transform(df[['Age', 'Salary']]) 30 31 # Concatenating final processed data 32 df_final = pd.concat([df[['Age', 'Salary']], df_gender], axis=1) 33 print("Preprocessed Data:\n", df_final)
Missing values in Age and Salary are filled with their respective column means.
Gender is one-hot encoded into binary columns [Male, Female].
Age and Salary are standardized to zero mean and unit variance.
Tabular dataset with missing numbers (None) and categorical Gender strings
Preprocessed Data:
Age Salary Male Female
0 -1.632993 -1.341641 0.0 1.0
1 -0.544331 -0.447214 1.0 0.0
2 0.544331 0.447214 0.0 1.0
3 1.632993 0.000000 1.0 0.0
4 0.000000 1.341641 0.0 1.0The data is fully cleaned, imputed, encoded, and scaled without information loss, ready for machine learning model training.
Data preprocessing, handling missing values, one-hot encoding, and standard scaling.
Test your understanding with these questions. Remember, this is for learning, not examination!