ML Notes
Learn effective data collection strategies for ML projects including web scraping, APIs, databases, surveys, and data quality assessment techniques.
Every machine learning project begins with data — and the quality, quantity, and relevance of your data determines the absolute ceiling of your model's performance. No algorithm, no matter how sophisticated, can extract patterns that do not exist in the training data. This lesson covers where to find data, how to collect it efficiently, how to assess whether you have enough, and how to ensure the data you gather will actually help solve your problem.
Understanding Data Requirements
Before collecting a single data point, clearly define what you need. Ask yourself: What am I trying to predict? What features might be predictive? How much historical data exists? What biases might be present in available sources? These questions guide your collection strategy and prevent wasting effort on irrelevant data.
The minimum data requirements depend on your problem complexity. Simple linear relationships might need only hundreds of examples. Complex image classification typically requires thousands per class. Deep learning models are data-hungry, often needing tens of thousands or millions of examples. A useful rule of thumb: you need at least 10 times more examples than model parameters for reliable training, though transfer learning can dramatically reduce this requirement.
Public Dataset Sources
The machine learning community maintains extensive collections of free, well-curated datasets perfect for learning and prototyping:
Major dataset repositories include Kaggle (competition datasets and community uploads), UCI Machine Learning Repository (classic benchmarks), Google Dataset Search (search engine for datasets), AWS Open Data Registry (large-scale datasets), and government open data portals (census, health, economic data).
Web Scraping for Custom Data
When no existing dataset fits your needs, web scraping lets you collect custom data from websites. Always respect robots.txt, rate-limit your requests, and check the website's terms of service.
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
def scrape_product_data(base_url, num_pages=5):
products = []
for page in range(1, num_pages + 1):
url = f"{base_url}?page={page}"
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
if response.status_code != 200:
print(f"Failed page {page}: status {response.status_code}")
continue
soup = BeautifulSoup(response.text, 'html.parser')
for item in soup.find_all('div', class_='product-card'):
product = {
'name': item.find('h2').text.strip(),
'price': item.find('span', class_='price').text.strip(),
'rating': item.find('span', class_='rating').text.strip(),
}
products.append(product)
time.sleep(1) # Rate limiting — be polite!
return pd.DataFrame(products)Collecting Data via APIs
APIs provide structured, reliable data access. Unlike scraping, APIs give you clean JSON data with consistent formatting and often include pagination for large datasets.
Data Quality Assessment
After collecting data, assess its quality before investing time in modeling. Check completeness (how much is missing?), accuracy (are values reasonable?), consistency (do related fields agree?), and timeliness (is the data current enough?).
How Much Data Do You Need?
This is one of the most common questions in ML. The answer depends on several factors: problem complexity, number of features, class balance, and noise level. Learning curves help you determine if more data would improve performance.
from sklearn.model_selection import learning_curve
from sklearn.ensemble import RandomForestClassifier
import numpy as np
# Generate learning curve
train_sizes, train_scores, val_scores = learning_curve(
RandomForestClassifier(n_estimators=100), X, y,
train_sizes=np.linspace(0.1, 1.0, 10), cv=5, scoring='accuracy'
)
# If training and validation scores are both low → need better features or model
# If training score is high but validation is low → need more data or regularization
# If both scores plateau → more data won't help muchEthical Considerations and Data Privacy
When collecting data involving people, privacy and ethics are paramount. Ensure compliance with regulations like GDPR and CCPA. Obtain informed consent when collecting personal data. Anonymize sensitive information. Be aware of biases in your data sources — historical data often reflects societal biases that your model will learn and perpetuate unless you actively mitigate them.
Practical Tips for Production Systems
When implementing data collection in production machine learning pipelines, consistency between training and inference is paramount. Every transformation applied during training must be identically applied when making predictions on new data. Save your preprocessing parameters (means, standard deviations, vocabulary mappings, category encodings) alongside your model so they can be loaded and applied during inference. Use pipeline objects from scikit-learn that bundle preprocessing and modeling into a single serializable unit. This prevents the common bug where training and production preprocessing diverge silently, causing model performance degradation that is difficult to diagnose.
Another production consideration is handling edge cases gracefully. What happens when inference data contains a category your model has never seen? What if a numerical feature has a value far outside the training range? Build defensive code that logs warnings and applies sensible defaults rather than crashing. Monitoring input data distributions against training distributions helps catch these drift issues before they significantly impact predictions.
Common Pitfalls and How to Avoid Them
Beginners working with data collection frequently make mistakes that compromise model validity. The most dangerous is data leakage — accidentally including information from the test set in your training preprocessing. For example, computing mean and standard deviation across the entire dataset before splitting introduces subtle leakage. Always fit preprocessing parameters on training data only, then transform both training and test data using those parameters. Another common mistake is applying transformations that destroy useful information, such as binning continuous variables unnecessarily or dropping features without understanding their predictive value. Finally, failing to document preprocessing steps makes collaboration difficult and debugging nearly impossible.
Key Takeaways
Data collection is the foundation of every ML project. Start with public datasets for learning and prototyping, then collect custom data via APIs and scraping for production projects. Always assess data quality before modeling — missing values, duplicates, and inconsistencies must be addressed. Use learning curves to determine if you need more data. Remember that data quality matters more than quantity, and invest time in understanding the domain and potential biases in your sources before building models.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Data Collection for Machine Learning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, data, preprocessing, collection, data collection for machine learning
Related Machine Learning Topics