Skip to content

Latest commit

Β 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Exoplanet Detection ML Model Training

A comprehensive machine learning pipeline for detecting and classifying exoplanets using ensemble methods and advanced data preprocessing techniques.

πŸ“‹ Table of Contents

🌟 Overview

This project implements a sophisticated machine learning pipeline for exoplanet detection using data from NASA's Kepler, K2, and TESS missions. The system employs advanced ensemble learning techniques, including voting classifiers, stacking classifiers, and gradient boosting methods to achieve high accuracy in distinguishing confirmed exoplanets from false positives.

Key Highlights

  • Multiple Data Sources: Support for Kepler, K2, and TESS exoplanet datasets
  • Advanced Ensemble Methods: Voting, Stacking, and Weighted ensemble classifiers
  • Overfitting Prevention: SMOTE balancing, stratified sampling, and proper train/val/test splits
  • Comprehensive Evaluation: ROC curves, confusion matrices, and detailed performance metrics
  • Production-Ready Models: Serialized models ready for deployment

πŸ“ Project Structure

ml_model_training/
β”œβ”€β”€ README.md                          # Project documentation
β”œβ”€β”€ clean data.py                      # Data cleaning and preprocessing
β”œβ”€β”€ split data.py                      # Dataset splitting with overfitting prevention
β”œβ”€β”€ new_ensemble.py                    # Main ensemble model training script
β”œβ”€β”€ random_sampling.py                 # Random sampling for dataset creation
β”œβ”€β”€ Ensemble.ipynb                     # Jupyter notebook for analysis
β”œβ”€β”€ exoplanet_data_clean.csv          # Cleaned dataset
β”œβ”€β”€ merged_full.csv                    # Merged raw dataset
β”œβ”€β”€ best_ensemble_1_stacking_logreg.pkl   # Best stacking model
β”œβ”€β”€ best_ensemble_2_voting_soft.pkl       # Best voting model
β”œβ”€β”€ comprehensive_comparison.png       # Model comparison visualization
β”œβ”€β”€ ensemble_comparison.png            # Ensemble performance comparison
β”œβ”€β”€ data/                              # Split datasets
β”‚   β”œβ”€β”€ train.csv                      # Training set (62.31%)
β”‚   β”œβ”€β”€ validation.csv                 # Validation set (18.85%)
β”‚   β”œβ”€β”€ test.csv                       # Test set (18.85%)
β”‚   β”œβ”€β”€ split_summary.txt              # Split statistics
β”‚   └── split_summary_with_prevention.txt
β”œβ”€β”€ Sample datafiles for testing/      # Sample datasets
β”œβ”€β”€ spaceapps - K2 dataset only/       # K2-specific models
β”œβ”€β”€ spaceapps - Kepler dataset only/   # Kepler-specific models
└── spaceapps - TESS dataset only/     # TESS-specific models

✨ Features

Data Processing

  • Feature Extraction: 18 key features including orbital, transit, planetary, and stellar properties
  • Data Cleaning: Automated removal of rows with excessive missing values
  • Critical Feature Validation: Ensures essential features (label, orbital_period, transit_duration, transit_depth) are present
  • Imputation: Median imputation for missing values
  • Standardization: Feature scaling using StandardScaler

Machine Learning Models

  • Base Models: Random Forest, Gradient Boosting, XGBoost, LightGBM, Extra Trees, AdaBoost, Bagging
  • Neural Networks: Multi-layer perceptron classifiers
  • Ensemble Strategies:
    • Voting Classifiers (Hard & Soft voting)
    • Stacking Classifiers (with RF and LogReg meta-learners)
    • Weighted Voting based on validation performance

Overfitting Prevention

  • SMOTE Balancing: Synthetic Minority Over-sampling Technique
  • Stratified Sampling: Maintains class distribution across splits
  • Larger Validation Sets: 20% validation, 20% test (vs typical 10-15%)
  • Cross-validation: For robust model evaluation

πŸ”§ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager

Install Dependencies

pip install pandas numpy scikit-learn imbalanced-learn matplotlib seaborn xgboost lightgbm joblib

Required Packages

pandas>=1.3.0
numpy>=1.21.0
scikit-learn>=1.0.0
imbalanced-learn>=0.9.0
matplotlib>=3.4.0
seaborn>=0.11.0
xgboost>=1.5.0
lightgbm>=3.3.0
joblib>=1.1.0

πŸ“Š Data Pipeline

Step 1: Data Cleaning

The clean data.py script performs comprehensive data cleaning:

python "clean data.py"

Process:

  1. Loads raw exoplanet data from CSV files
  2. Extracts 18 key features:
    • Label: Confirmed (1) vs False Positive (0)
    • Orbital Parameters: period, transit duration, depth
    • Planetary Properties: radius, equilibrium temperature
    • Stellar Properties: temperature, radius
  3. Validates critical features (label, orbital_period, transit_duration, transit_depth)
  4. Removes rows exceeding missing value threshold (default: 50%)
  5. Exports cleaned dataset to exoplanet_data_clean.csv

Key Parameters:

  • missing_threshold: 0.5 (removes rows with >50% missing values)
  • critical_features: Must be non-null for row retention

Step 2: Data Splitting

The split data.py script creates train/validation/test splits with overfitting prevention:

python "split data.py"

Process:

  1. Loads cleaned dataset
  2. Applies stratified sampling to maintain class distribution
  3. Performs class balancing using SMOTE (Synthetic Minority Over-sampling)
  4. Splits data into:
    • Training: 62.31% (6,778 samples)
    • Validation: 18.85% (2,050 samples)
    • Test: 18.85% (2,050 samples)
  5. Balances training set to 50/50 class distribution
  6. Saves splits to data/ directory

Overfitting Prevention Techniques:

  • βœ… SMOTE balancing (eliminates class imbalance)
  • βœ… Larger validation/test sets (20%/20% vs typical 10-15%)
  • βœ… Stratified sampling (preserves class distribution)
  • βœ… Random state control (reproducible splits)

Class Distribution:

  • Train: 3,389 CONFIRMED | 3,389 FALSE POSITIVE (50/50 balanced)
  • Validation: 921 CONFIRMED | 1,129 FALSE POSITIVE (natural distribution)
  • Test: 920 CONFIRMED | 1,130 FALSE POSITIVE (natural distribution)

Step 3: Random Sampling (Optional)

The random_sampling.py script creates smaller datasets for testing:

python random_sampling.py

Purpose: Generate 20% sample of full dataset for rapid prototyping and testing.

πŸ€– Model Training

Main Training Script

Run the comprehensive ensemble training pipeline:

python new_ensemble.py

Training Process

Phase 1: Base Model Training

The system trains 15+ base classifiers:

  1. Random Forest (n_estimators=200, max_depth=20)
  2. Gradient Boosting (n_estimators=200, learning_rate=0.1)
  3. XGBoost (n_estimators=200, max_depth=7)
  4. LightGBM (n_estimators=200, num_leaves=50)
  5. Extra Trees (n_estimators=200, max_depth=20)
  6. AdaBoost (n_estimators=100)
  7. Bagging Classifier (n_estimators=50)
  8. K-Nearest Neighbors (n_neighbors=5)
  9. Gaussian Naive Bayes
  10. Multi-Layer Perceptron (100, 50 hidden layers)
  11. Decision Tree (max_depth=10)

Phase 2: Ensemble Construction

Voting Classifiers:

  • Hard Voting: Majority vote from top 7 models
  • Soft Voting: Average predicted probabilities from top 7 models
  • Weighted Voting: Validation accuracy-weighted soft voting

Stacking Classifiers:

  • Stacking with Random Forest: RF as meta-learner
  • Stacking with Logistic Regression: LogReg as meta-learner
  • Uses 5-fold cross-validation for meta-features

Phase 3: Evaluation

All models are evaluated on validation and test sets using:

  • Accuracy Score
  • F1 Score (weighted and macro)
  • ROC-AUC Score
  • Confusion Matrix
  • Classification Report (precision, recall, F1 per class)

Model Selection

The system automatically:

  1. Ranks models by validation accuracy
  2. Selects top 2 ensemble models
  3. Saves them as .pkl files for deployment
  4. Generates comparison visualizations

Visualizations

The training process generates:

  • comprehensive_comparison.png: Bar chart comparing all models
  • ensemble_comparison.png: Detailed ensemble method comparison
  • confusion_matrix.png: Per-model confusion matrices
  • roc_curve.png: ROC curves for top models

Dataset-Specific Models

Kepler Dataset Only

  • Location: spaceapps - Kepler dataset only/
  • Best Models: best_ensemble_1_voting_soft.pkl, best_ensemble_2_voting_weighted.pkl
  • Additional: Neural network implementation (neural_network_new.py)

K2 Dataset Only

  • Location: spaceapps - K2 dataset only/
  • Best Models: best_ensemble_1_stacking_rf.pkl, best_ensemble_2_stacking_logreg.pkl
  • Features: Batch prediction capabilities

TESS Dataset Only

  • Location: spaceapps - TESS dataset only/
  • Best Models: tess_best_ensemble_1_voting_weighted.pkl, tess_best_ensemble_2_stacking_rf.pkl

πŸš€ Usage

Complete Pipeline

Run the full pipeline from raw data to trained models:

# Step 1: Clean the data
python "clean data.py"

# Step 2: Split the data with overfitting prevention
python "split data.py"

# Step 3: Train ensemble models
python new_ensemble.py

Using Trained Models

Load and use a saved model for predictions:

import joblib
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer

# Load the best model
model = joblib.load('best_ensemble_1_stacking_logreg.pkl')

# Load new data
new_data = pd.read_csv('new_exoplanet_data.csv')

# Preprocess (same as training)
imputer = SimpleImputer(strategy='median')
scaler = StandardScaler()

# Drop label column if present
X = new_data.drop('label', axis=1, errors='ignore')

# Impute and scale
X_imputed = imputer.fit_transform(X)
X_scaled = scaler.fit_transform(X_imputed)

# Make predictions
predictions = model.predict(X_scaled)
probabilities = model.predict_proba(X_scaled)

# Interpret results
# 0 = FALSE POSITIVE, 1 = CONFIRMED EXOPLANET
print(f"Predicted class: {predictions[0]}")
print(f"Confidence: {probabilities[0].max() * 100:.2f}%")

Jupyter Notebook Analysis

Explore the interactive analysis:

jupyter notebook Ensemble.ipynb

🌐 Web-Based Predictions with Sample Data

Users can test the model using our web interface with the included sample data files. This provides an easy, no-code way to see the model in action.

πŸ“‚ Available Sample Test Files

The Sample datafiles for testing/ folder contains ready-to-use sample datasets:

File Samples Mission(s) Description
Fullymerged_data_sample.csv 2,051 All (Kepler, K2, TESS) Pre-cleaned merged dataset with all features
kepler_exoplanet_sample.csv 1,841 Kepler Kepler mission-specific data with KOI parameters
k2_exoplanet_sample.csv 425 K2 K2 mission data with EPIC identifiers
tess_exoplanet_sample.csv 1,482 TESS TESS mission data with TOI identifiers

πŸ’‘ Example Predictions

Using Fullymerged_data_sample.csv:

πŸ“Š Results Summary:
- Total Samples: 2,051
- Confirmed Exoplanets: 892 (43.5%)
- False Positives: 1,159 (56.5%)
- Model Accuracy: 96.5%

🎨 Advanced: API-Based Predictions

For programmatic access, create a simple Flask API:

api.py:

from flask import Flask, request, jsonify
import joblib
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer

app = Flask(__name__)
model = joblib.load('best_ensemble_1_stacking_logreg.pkl')

@app.route('/predict', methods=['POST'])
def predict():
    file = request.files['file']
    data = pd.read_csv(file)

    X = data.drop('label', axis=1, errors='ignore')
    imputer = SimpleImputer(strategy='median')
    scaler = StandardScaler()
    X_processed = scaler.fit_transform(imputer.fit_transform(X))

    predictions = model.predict(X_processed)
    probabilities = model.predict_proba(X_processed)

    return jsonify({
        'total': len(predictions),
        'confirmed': int(sum(predictions)),
        'predictions': predictions.tolist(),
        'confidences': [float(p.max()) for p in probabilities]
    })

if __name__ == '__main__':
    app.run(debug=True)

Usage:

# Start API
python api.py

# Make prediction request
curl -X POST -F "file=@Sample datafiles for testing/Fullymerged_data_sample.csv" \
     http://localhost:5000/predict

🌍 Deploy Your Web App (Optional)

Deploy for free to share with others:

Streamlit Cloud:

# 1. Push code to GitHub
# 2. Go to share.streamlit.io
# 3. Connect your repo
# 4. Your app will be live at: https://yourapp.streamlit.app

Hugging Face Spaces:

# 1. Create account at huggingface.co
# 2. Create new Space (Streamlit)
# 3. Upload app.py and model files
# 4. Live at: https://huggingface.co/spaces/USERNAME/exoplanet-detector

βœ… Benefits of Web Interface

  • No Coding Required: Simple drag-and-drop interface
  • Instant Results: Get predictions in seconds
  • Visual Feedback: Clear metrics and confidence scores
  • Batch Processing: Analyze thousands of samples at once
  • Accuracy Testing: Compare predictions against true labels
  • Export Results: Download predictions as CSV
  • Mobile Friendly: Access from any device

πŸ“Š Dataset Information

Features Used

Orbital & Transit Parameters (7 features):

  • orbital_period: Orbital Period [days]
  • transit_duration: Transit Duration [hrs]
  • transit_duration_err1, transit_duration_err2: Transit Duration Uncertainties
  • transit_depth: Transit Depth [ppm]
  • transit_depth_err1, transit_depth_err2: Transit Depth Uncertainties

Planetary Properties (4 features):

  • planet_radius: Planetary Radius [Earth radii]
  • planet_radius_err1, planet_radius_err2: Radius Uncertainties
  • equi_temp: Equilibrium Temperature [K]

Stellar Properties (6 features):

  • stellar_temp: Stellar Effective Temperature [K]
  • stellar_temp_err1, stellar_temp_err2: Temperature Uncertainties
  • stellar_radius: Stellar Radius [Solar radii]
  • stellar_radius_err1, stellar_radius_err2: Radius Uncertainties

Target Variable:

  • label: 1 = CONFIRMED, 0 = FALSE POSITIVE

Data Sources

  • Kepler Mission: Original exoplanet hunting mission (2009-2018)
  • K2 Mission: Extended Kepler mission with different targets
  • TESS Mission: Transiting Exoplanet Survey Satellite (2018-present)

Dataset Statistics

  • Total Samples (After Cleaning): ~10,878 entries
  • Training Samples: 6,778 (balanced 50/50)
  • Validation Samples: 2,050 (natural distribution ~45/55)
  • Test Samples: 2,050 (natural distribution ~45/55)
  • Features: 17 (excluding label)
  • Class Balance (Original): ~45% CONFIRMED, ~55% FALSE POSITIVE

πŸ” Key Insights

Why Ensemble Methods?

  1. Reduced Variance: Combining models reduces overfitting risk
  2. Improved Accuracy: Ensemble often outperforms individual models
  3. Robustness: Less sensitive to noise and outliers
  4. Diverse Perspectives: Different algorithms capture different patterns

Overfitting Prevention Strategy

Our multi-layered approach:

  1. βœ… SMOTE Balancing: Prevents bias toward majority class
  2. βœ… 20/20 Val/Test Split: Larger holdout sets catch overfitting early
  3. βœ… Stratified Sampling: Ensures representative class distribution
  4. βœ… Cross-Validation: 5-fold CV in stacking prevents meta-overfitting
  5. βœ… Feature Scaling: Standardization prevents feature dominance
  6. βœ… Ensemble Diversity: Multiple algorithms reduce variance

🀝 Contributing

Contributions are welcome! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is part of the Space Apps Challenge initiative.

πŸ™ Acknowledgments

  • NASA: For providing Kepler, K2, and TESS exoplanet data
  • Space Apps Challenge: For inspiring this project
  • Scikit-learn: For machine learning tools
  • XGBoost & LightGBM: For gradient boosting implementations
  • Imbalanced-learn: For SMOTE implementation

πŸ“ž Contact

For questions or collaboration:


Last Updated: October 2025
Version: 1.0.0
Status: Production Ready πŸš€

About

machine learning models for exoplanet detection

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages