Data Science and AnalyticsUnit 25 min read
Data Sources, Cleaning Techniques & Quality Assurance
Unit 2 of Data Science and Analytics covers structured/unstructured data sources, collection methods (web scraping, APIs, databases), data cleaning techniques (handling missing values, outliers, duplicates), and quality assurance metrics (accuracy, completeness, consistency) with real-world applications in Nepalese tec
Core Concepts
Data Sources: Where Data Comes From
Data can be broadly classified into structured (tabular, fixed schema) and unstructured (text, images, videos) formats. Nepalese companies like eSewa (structured transaction logs) and Pathao (unstructured ride logs with GPS coordinates) rely on both types.
mindmap
root((Data Sources))
Structured
Databases["SQL (MySQL, PostgreSQL)"]
Spreadsheets["Excel, CSV"]
APIs["REST, GraphQL"]
Unstructured
Text["PDFs, Logs"]
Multimedia["Images, Videos"]
Social["WhatsApp, Facebook"]
Semi-Structured
JSON["API responses"]
XML["Config files"]Data Collection Methods
1. Web Scraping
- Extracts data from websites (e.g., Nepal Stock Exchange (NEPSE) price scrapers).
- Tools: BeautifulSoup, Scrapy.
- Example: Scraping Daraz product reviews to analyze sentiment.
2. APIs (Application Programming Interfaces)
- Structured data access (e.g., Khalti’s payment API for transaction records).
- Example: Fetching NTC’s electricity consumption data via API.
3. Databases
- Direct queries (e.g., Global IME Bank’s loan data).
- Example: SQL query to extract Pathao driver earnings from a PostgreSQL database.
-- Example SQL query for Pathao driver data
SELECT driver_id, SUM(fare) AS total_earnings
FROM rides
WHERE date = '2023-10-01'
GROUP BY driver_id;
4. Sensors/IoT
- Real-time data (e.g., NTC’s smart meters for electricity usage).
- Example: Kathmandu traffic sensors feeding into Pathao’s route optimization.
Data Cleaning: Fixing Dirty Data
Common Issues & Solutions
| Issue | Solution | Example (Nepal Context) |
|---|---|---|
| Missing Values | Imputation (mean/median) | Filling missing NEPSE stock prices |
| Duplicates | Deduplication | Removing duplicate Khalti transactions |
| Outliers | IQR, Z-score filtering | Filtering extreme NTC electricity spikes |
| Inconsistent Formats | Standardization (e.g., dates) | Converting Daraz order dates to ISO format |
| Noise | Smoothing (moving averages) | Cleaning Pathao GPS coordinates |
Handling Missing Data
- Deletion: Remove rows/columns (if <5% missing).
- Example: Dropping Ncell customer records with missing phone numbers.
- Imputation:
- Mean/Median: For numerical data (e.g., NEPSE closing prices).
- Mode: For categorical data (e.g., Daraz product categories).
- Advanced: KNN imputation (for complex datasets like bank loan defaults).
# Example: Imputing missing NEPSE prices
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(strategy='mean')
cleaned_prices = imputer.fit_transform(nepse_data[['price']])
Data Quality Metrics
| Metric | Definition | Example |
|---|---|---|
| Accuracy | Correctness of data | Khalti transaction records (99.9% accurate) |
| Completeness | % of non-missing values | NTC meter readings (95% complete) |
| Consistency | Uniformity across datasets | Daraz product IDs (no duplicates) |
| Timeliness | Freshness of data | Pathao ride logs (real-time) |
| Uniqueness | No duplicate records | Ncell customer IDs (unique) |
In the Real World
- eSewa: Uses APIs to collect transaction data and cleans it to detect fraud (e.g., duplicate payments).
- Pathao: Relies on GPS sensor data cleaned via outlier removal to optimize routes in Kathmandu traffic.
- NEPSE: Scrapes stock prices from websites, cleans missing values, and ensures consistency for traders.
Worked Example:
- Problem: Daraz’s order queue has missing delivery times.
- Solution:
- Impute missing times with median delivery duration per city.
- Remove outliers (e.g., 3-day deliveries in Kathmandu).
- Validate with Khalti payment timestamps for consistency.
Exam Tip
- Focus Areas:
- Differentiate between structured/unstructured data (e.g., SQL vs. web logs).
- Know 3 data cleaning techniques (e.g., imputation, deduplication).
- Relate metrics to Nepalese companies (e.g., Khalti’s accuracy, NTC’s completeness).
- Common Pitfalls:
- Forgetting to validate data after cleaning.
- Overusing deletion for missing data (use imputation instead).
- Scoring Boosters:
- Draw a data cleaning workflow in exams.
- Compare API vs. scraping with real examples (e.g., Khalti API vs. NEPSE scraper).
Visual Summary:
flowchart TD
A["Data Collection"] --> B["Web Scraping"]
A --> C["APIs"]
A --> D["Databases"]
A --> E["Sensors"]
B --> F["Clean Missing Values"]
C --> F
D --> G["Remove Duplicates"]
E --> H["Filter Outliers"]
F --> I["Validate"]
G --> I
H --> I
I --> J["Analyze"]Based on the PU BE Computer (PU) syllabus for Data Science and Analytics (CMP422), unit 2.
Discussion
Loading…