DS Unit 2: Complete Concept Guide
Unit 2: Data Acquisition & Data Preparation -> Generated and Prepared By Thiruselvan (ThiruXD)
Why Data Preparation Matters
- A model is only as good as the data fed into it.
- 70–80% of a data scientist’s time is spent finding, cleaning, and preparing data — not modelling.
- Consequences of dirty data:
- Wrong decisions (e.g., duplicated customer inflates sales counts)
- Broken models (missing values crash training or bias predictions)
- Wasted money (acting on dirty data costs real revenue)
- Lost trust (one bad dashboard and stakeholders stop believing the data team)
The Data Preparation Journey (8 stages)
- Sources of Data
- Types of Data
- Collection Methods
- Data Quality Issues (Missing & Noisy data)
- Data Cleaning
- Integration
- Transformation
- Preprocessing (ready for modelling)
1. Sources of Data
Data scientists group origins into three broad families, each with cost–speed–reliability trade-offs.
Primary Sources (collected by you for the exact problem)
- Surveys & questionnaires
- Experiments & sensors
- Interviews & observation
Secondary Sources (already collected by someone else)
- Government & census records
- Kaggle & research datasets
- Company databases (sales, HR, ERP)
Web & Digital (continuously generated online)
- Social media & clickstreams
- APIs & web scraping
- IoT device streams
Primary vs Secondary Comparison
| Aspect | Primary | Secondary |
|---|---|---|
| Control | You control quality & questions | Quality/bias often unknown |
| Cost & Speed | Expensive, slow | Cheap, fast |
| Fit to problem | Perfect fit | May not match your exact question |
| Ownership | You own the data & rights | Licensing / reuse restrictions may apply |
| Historical coverage | Limited | Often large historical coverage |
Scenario: GCU wants to know why students skip 8 AM classes.
- Primary → run a fresh survey
- Secondary → reuse existing attendance data already in the ERP
2. Types of Data
Classified by internal structure — this decides storage, querying, and preprocessing effort.
1. Structured
- Fixed rows & columns with defined data types
- Examples: Relational tables (SQL), spreadsheets, CSV, ATM transactions
- Easy to store, query, sort, filter, aggregate
- Schema is predefined
2. Semi-structured
- No rigid table, but tags/keys make it self-describing
- Examples: JSON, XML, YAML, emails (header + free body), NoSQL documents
- Flexible schema (records can differ)
- Hierarchical and common in APIs
3. Unstructured
- No predefined model — ~80% of real-world data
- Examples: Text (reviews, PDFs, chats), images, audio, video, social media posts, sensor blobs
- Richest in meaning but hardest to analyse directly (needs NLP / Computer Vision / ML first)
Side-by-side Comparison
| Aspect | Structured | Semi-structured | Unstructured |
|---|---|---|---|
| Schema | Fixed, predefined | Flexible (tags) | None |
| Storage | Relational DB / SQL | NoSQL, JSON/XML | Data lakes, blobs |
| Example | ATM transactions | JSON API response | CCTV video, tweets |
| Query ease | Very easy (SQL) | Moderate | Hard (needs AI/ML) |
| Share of data | ~20% | Growing | ~80% |
3. Data Collection Methods
Each method suits different situations, budgets, and data-change speeds.
- Surveys & Questionnaires — fixed questions to a sample; cheap & scalable but self-report bias
- Sensors & IoT — continuous automatic readings; high-volume real-time but needs hardware
- Web Scraping — extract from web pages when no API exists; powerful but must respect legal terms
- APIs — clean structured data on request (weather, maps, Twitter/X); sanctioned & reliable
- Existing Databases — internal systems already log everything; usually the fastest starting point
- Observation & Logs — server logs, clickstreams, or direct field observation
Which method fits?
- Student satisfaction → Survey
- Campus power usage → IoT sensors
- Live cricket scores → Public API
- Competitor pricing → Web scraping
- Past admissions → Existing database
4. Data Quality Issues
“Garbage in, garbage out.”
Six quality dimensions:
- Accuracy — values truly match the real world (no typos/errors)
- Completeness — no required values are blank or lost
- Consistency — same fact never contradicts itself across sources
- Timeliness — data is recent enough to reflect current reality
- Validity — values obey expected rules, formats, ranges (e.g., age > 0)
- Uniqueness — each real-world entity appears only once (no accidental duplicates)
Dirty Dataset Example (customer table from two systems)
- Duplicate rows (same person with different casing/city spelling)
- Inconsistent city names (Bengaluru vs Bangalore)
- Invalid ages (−5, 210)
- Missing phone & age values
4A. Missing Values
Why data goes missing
- Data entry errors
- Non-response
- Sensor / system failure
- Merging mismatches
How to handle them
- Delete rows/columns — only if few & random
- Mean / Median / Mode imputation
- Forward / Backward fill (time series)
- Predict (regression / KNN) from other columns
- Flag & keep as “Unknown” category
Worked Example (marks: 72, 68, 90, ?, 85, 76)
- Mean ≈ 78.2 (good for roughly symmetric data)
- Median = 76 (robust to outliers)
- Mode — none (not useful for continuous marks)
4B. Noisy Data
Noise = random error or variance that hides the true signal (outliers, wrong entries, fluctuations).
Smoothing techniques
- Binning — sort into bins, smooth by bin mean / median / boundary
- Regression — fit a function and replace with fitted values
- Clustering — group similar points; values outside clusters = outliers
- Outlier removal — detect with IQR / Z-score, then drop or cap
Binning Example (prices: 4, 8, 15, 21, 21, 24, 25, 28, 34 → 3 equal-frequency bins)
- Smooth by mean replaces every value with its bin average
- Smooth by boundary snaps each value to the nearer bin edge
IQR Outlier Detection
- Q1 = 25th percentile, Q3 = 75th percentile
- IQR = Q3 − Q1
- Lower bound = Q1 − 1.5 × IQR
- Upper bound = Q3 + 1.5 × IQR
- Any value outside [Lower, Upper] is an outlier
Example (salaries in ₹k: 20, 22, 24, 25, 27, 90) → 90 is an outlier.
5. Data Cleaning Techniques
A repeatable pipeline that turns raw dirty data into analysis-ready data.
Golden rule: Clean a copy, log every change, keep it reproducible — never overwrite raw data.
Pipeline steps
- Handle Missing (impute or drop)
- Remove Duplicates
- Fix Errors (typos, wrong types)
- Smooth Noise (binning / regression)
- Handle Outliers (IQR / Z-score)
- Standardise Formats (dates, units, case)
Before vs After Cleaning Example
- Duplicate removed
- City names standardised (Bangalore → Bengaluru)
- Invalid ages replaced by median
- Missing age imputed
6. Data Integration
Combining data from multiple sources into one unified, consistent view (usually via ETL / ELT).
Key challenges
- Schema mismatch
- Entity resolution (same entity, different names)
- Data redundancy
- Conflicting values
- Different units / formats
Problems & Fixes
- Schema mismatch (DOB vs BirthDate) → map columns to common schema
- Entity resolution (A. Sharma vs Anil Sharma) → key matching / fuzzy matching
- Redundancy → detect & drop duplicates
- Value conflicts → rule: trust newer / verified source
- Unit mismatch (₹ vs $, kg vs lbs, date formats) → convert to one standard
7. Data Transformation
Convert data into a form better suited for mining and modelling. Six core operations:
-
Normalisation (Min-Max) — rescale to [0, 1]
[ x' = \frac{x - \min}{\max - \min} ]
Prevents large-scale features from dominating.
-
Standardisation (Z-score) — mean 0, std 1
[ z = \frac{x - \text{mean}}{\text{std}} ]
Required by distance-based models (KNN, clustering).
-
Aggregation — summarise detail (daily → monthly)
-
Generalisation — replace low-level with high-level (city → state)
-
Encoding — turn categories into numbers
- Label Encoding — one integer per category (use only for ordinal data)
- One-Hot Encoding — one 0/1 column per category (safe default for nominal data)
-
Discretisation — split continuous range into bands (age → age groups)
Why rescale? Age (0–100) vs Salary (0–20,00,000) → salary dominates distance calculations unless both are scaled.
8. Data Reduction & Preprocessing
Data Reduction — shrink the dataset without losing important signal
- Dimensionality reduction (fewer columns, e.g., PCA)
- Numerosity reduction (fewer rows via sampling)
- Feature selection (keep only useful columns)
- Aggregation (roll up to coarser level)
Data Preprocessing (the umbrella workflow)
- Cleaning — fix missing, noisy, inconsistent data
- Integration — merge multiple sources
- Transformation — normalise, encode, aggregate
- Reduction — fewer rows/features while preserving signal
This is where the majority of a data scientist’s time is spent.
End-to-End Scenario: Swiggy Late Deliveries
Goal: Predict which orders will be delivered late.
- Collect — Order logs (DB) + GPS (IoT) + weather API + reviews (text)
- Assess quality — missing GPS pings, duplicate orders, impossible times
- Clean — impute missing times, drop duplicates, cap outlier distances
- Integrate — join on order_id & timestamp
- Transform — normalise distance, encode weather, extract review sentiment
- Reduce — keep only the 8 features that actually predict delay
Only after these steps is the data ready for a machine-learning model.
Practical Tools
- Pandas — workhorse for cleaning, merging, transforming tables
- NumPy — fast numeric arrays
- Scikit-learn — scalers, encoders, imputers (StandardScaler, OneHotEncoder, etc.)
- SQL — query & join at the source
- OpenRefine — point-and-click cleaning of messy data
- Google Colab — free cloud notebooks
Simple Pandas example:
df.drop_duplicates() and df.fillna(df.median())
Key Takeaways
- Know your source & type — Primary vs Secondary; Structured / Semi / Unstructured decides everything downstream.
- Quality is non-negotiable — Accuracy, Completeness, Consistency… garbage in → garbage out.
- Cleaning is a pipeline — Missing values, noise, duplicates, formats — always on a copy, logged, reproducible.
- Integrate + Transform — Unify sources, then normalise/encode so models can actually use the data.
- Preprocessing ≈ 70% of the job — Master this module and the rest of Data Science becomes far easier.