Study interactive :: Progress tools open in the Study Hub reader.

Kaggle Competitions

Participating in Kaggle competitions, from getting started to advanced strategies.

Table of Contents


Introduction to Kaggle

What is Kaggle?

Kaggle is a platform for data science competitions where you can:

Why Participate in Kaggle?

Benefits:

  1. Real-World Experience: Work with real datasets and problems
  2. Learn from Experts, see top solutions and approaches
  3. Build Portfolio: Showcase your skills
  4. Network: Connect with data scientists
  5. Career Growth: Kaggle competitions are valued by employers
  6. Prizes: Win cash and recognition

Types of Competitions

  1. Featured Competitions: Major competitions with prizes
  2. Research Competitions: Academic/research focused
  3. Getting Started: Beginner-friendly competitions
  4. Playground: Practice competitions
  5. InClass: Educational competitions

Getting Started

Step 1: Create Account

  1. Go to Kaggle.com
  2. Sign up with Google or email
  3. Complete your profile
  4. Verify your account

Step 2: Join Your First Competition

  1. Browse Competitions
  2. Start with "Getting Started" competitions
  3. Read competition rules and description
  4. Accept competition rules
  5. Download data

Step 3: Set Up Environment

# Install Kaggle API
pip install kaggle

# Configure API (download credentials from Kaggle account)
# Place kaggle.json in ~/.kaggle/

# Download competition data
kaggle competitions download -c competition-name

# Or use Kaggle Notebooks (recommended for beginners)

Understanding the Problem

Key Questions to Ask

  1. What is the task?

    • Classification, Regression, Time Series, etc.
  2. What is the evaluation metric?

    • Accuracy, RMSE, Log Loss, MAPE, etc.
  3. What data is provided?

    • Training set, test set, sample submission
  4. What are the constraints?

    • Time limits, submission limits, team size
  5. What is the business context?

    • Understanding helps with feature engineering

Example: Titanic Competition

Problem: Predict which passengers survived the Titanic disaster

Task: Binary classification

Metric: Accuracy

Data:

Goal: Predict survival for test set passengers


Exploring Datasets

Initial Data Exploration

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Load data
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')

# Basic info
print("Training set shape:", train.shape)
print("Test set shape:", test.shape)
print("\nTraining set info:")
print(train.info())
print("\nTraining set head:")
print(train.head())
print("\nTraining set describe:")
print(train.describe())

# Check for missing values
print("\nMissing values:")
print(train.isnull().sum())

# Check target distribution
print("\nTarget distribution:")
print(train['target'].value_counts())

EDA Checklist


Strategy and Approach

Competition Strategy Framework

1. Baseline First:

# Start with simple baseline
from sklearn.ensemble import RandomForestClassifier

baseline = RandomForestClassifier(n_estimators=100, random_state=42)
baseline.fit(X_train, y_train)
baseline_score = baseline.score(X_val, y_val)
print(f"Baseline score: {baseline_score:.3f}")

2. Iterate and Improve:

3. Validate Properly:

Common Strategies

Strategy 1: Feature Engineering Focus

Strategy 2: Model Ensemble

Strategy 3: Hyperparameter Optimization


Model Selection and Preprocessing

Model Selection Guide

For Tabular Data:

For Images:

For Text:

Preprocessing Pipeline

from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer

# Create preprocessing pipeline
preprocessor = ColumnTransformer(
    transformers=[
        ('num', Pipeline([
            ('imputer', SimpleImputer(strategy='median')),
            ('scaler', StandardScaler())
        ]), numeric_features),
        ('cat', Pipeline([
            ('imputer', SimpleImputer(strategy='most_frequent')),
            ('encoder', OneHotEncoder(drop='first', sparse=False))
        ]), categorical_features)
    ]
)

# Use in pipeline
pipeline = Pipeline([
    ('preprocessor', preprocessor),
    ('model', RandomForestClassifier(n_estimators=100, random_state=42))
])

Validation Strategy

Why Validation Matters

Validation Strategies

1. Time-Based Split (Time Series):

# For time series competitions
split_date = '2023-01-01'
train = df[df['date'] < split_date]
val = df[df['date'] >= split_date]

2. Stratified K-Fold:

from sklearn.model_selection import StratifiedKFold

skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
for train_idx, val_idx in skf.split(X, y):
    X_train_fold, X_val_fold = X[train_idx], X[val_idx]
    y_train_fold, y_val_fold = y[train_idx], y[val_idx]
    # Train and evaluate

3. Group K-Fold:

from sklearn.model_selection import GroupKFold

gkf = GroupKFold(n_splits=5)
for train_idx, val_idx in gkf.split(X, y, groups):
    # Train and evaluate

Matching Competition Evaluation

# If competition uses specific metric, use it locally
from sklearn.metrics import mean_squared_error, log_loss

# Example: RMSE for regression
def competition_metric(y_true, y_pred):
    return np.sqrt(mean_squared_error(y_true, y_pred))

# Use in cross-validation
scores = cross_val_score(model, X, y, cv=5, scoring=competition_metric)

Collaboration and Teamwork

Forming Teams

Benefits:

How to Find Teammates:

Team Workflow

  1. Divide Tasks:

    • Feature engineering
    • Model development
    • Validation
    • Ensembling
  2. Share Code:

    • Use GitHub
    • Kaggle Notebooks
    • Shared drives
  3. Merge Solutions:

    • Blending predictions
    • Stacking models
    • Voting ensembles

Merging Predictions

# Simple average
pred1 = model1.predict_proba(X_test)
pred2 = model2.predict_proba(X_test)
pred3 = model3.predict_proba(X_test)

ensemble_pred = (pred1 + pred2 + pred3) / 3

# Weighted average
weights = [0.4, 0.3, 0.3]
ensemble_pred = weights[0] * pred1 + weights[1] * pred2 + weights[2] * pred3

# Stacking
meta_model = LogisticRegression()
meta_model.fit([pred1_train, pred2_train, pred3_train], y_train)
ensemble_pred = meta_model.predict_proba([pred1, pred2, pred3])

Submission and Evaluation

Preparing Submission

# Create submission file
submission = pd.DataFrame({
    'id': test['id'],
    'target': predictions
})

# Save submission
submission.to_csv('submission.csv', index=False)

# Verify format
print(submission.head())
print(f"Submission shape: {submission.shape}")

Submission Best Practices

  1. Check Format: Match sample submission exactly
  2. Verify Predictions: Check for NaN, inf, or invalid values
  3. Multiple Submissions: Try different approaches
  4. Track Submissions: Keep notes on what worked

Understanding Leaderboard

Public Leaderboard:

Private Leaderboard:

Leaderboard Shake-up:


Learning from Feedback

Analyzing Results

After Submission:

  1. Check your rank
  2. Compare with baseline
  3. Analyze what worked
  4. Learn from top solutions

Key Questions:

Learning from Top Solutions

After Competition Ends:

  1. Read winning solutions
  2. Study their approaches
  3. Replicate their methods
  4. Adapt to your projects

Common Patterns in Winning Solutions:


Best Practices

Do's

  1. Start Simple: Build baseline first
  2. Validate Properly: Use cross-validation
  3. Feature Engineering: Often more important than model choice
  4. Ensemble: Combine multiple models
  5. Learn: Study winning solutions
  6. Document: Keep notes on what works
  7. Be Patient: Improvement takes time

Don'ts

  1. Don't Overfit: Avoid overfitting to public leaderboard
  2. Don't Skip EDA: Always explore data first
  3. Don't Ignore Rules: Follow competition rules
  4. Don't Give Up: Persistence pays off
  5. Don't Copy Blindly: Understand solutions you use

Competition Checklist


Resources

Kaggle Resources

Getting Started Competitions

  1. Titanic: Binary classification
  2. House Prices: Regression
  3. Digit Recognizer: Image classification
  4. Spaceship Titanic: Multi-class classification

Learning Resources


Key Takeaways

  1. Start Small: Begin with getting started competitions
  2. Learn Continuously: Study winning solutions
  3. Validate Properly: Don't trust public leaderboard alone
  4. Feature Engineering: Often key to success
  5. Collaborate: Teams often perform better
  6. Be Patient: Improvement takes time and practice

Try next: Enter one playground competition. Beat a simple baseline, then stop and write what you learned.