Skip to content

About

Repository for project - Stellar Classification with random forest. Results will be compared with KNN classifier.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

Stellar Object Classification with Random Forest

Automatic classification of astronomical objects — galaxies, quasars (QSO), and stars — using supervised machine learning on photometric data from the Sloan Digital Sky Survey (SDSS).


Project Overview

Modern sky surveys such as SDSS record millions of spectra every year, making manual classification infeasible at scale. This project builds a machine learning pipeline that automatically distinguishes between three types of celestial objects based on their photometric and spectroscopic properties:

Class Description
GALAXY A large, gravitationally bound system of stars, dust, and dark matter
QSO (Quasar) An extremely luminous active galactic nucleus, resembling a star but cosmologically distant
STAR A gravitationally bound plasma sphere emitting stable electromagnetic radiation

The primary classifier is Random Forest, with k-Nearest Neighbors (k-NN) used as a baseline for comparison. Both models are tuned using Grid Search with cross-validation.


Repository Structure

RF_Stellar_Classification/
├── data/
│   ├── star_classification.csv          # Full dataset (100 000 instances) — gitignored
│   └── star_classification_specimen.csv # Sample of 20 rows for inspection
├── charts/
│   ├── rf_confusion_matrix.png
│   ├── rf_roc_curve.png
│   ├── rf_feature_importance.png
│   ├── knn_confusion_matrix.png
│   └── knn_roc_curve.png
├── read_data.py                         # Data loading, feature engineering, encoding
├── rf.py                                # Random Forest training + Grid Search
├── knn.py                               # k-NN training + Grid Search
├── result_charts.py                     # Chart generation (confusion matrix, ROC, feature importance)
└── results.txt                          # Console output with metrics from both models

.csv files are gitignored, can be downloaded from here: https://www.kaggle.com/datasets/fedesoriano/stellar-classification-dataset-sdss17


Dataset

  • Source: Stellar Classification Dataset (SDSS) — Kaggle / SDSS DR17
  • Total instances: 100 000
  • Train / Test split: 80 000 / 20 000 (80% / 20%, random_state=42)
  • Original features: 18 columns (17 input + 1 target class)

Class distribution (test set)

Class Count
GALAXY 11 860
STAR 4 343
QSO 3 797

Feature Engineering & Preprocessing

Instrumental columns with no astrophysical relevance to classification were removed:

obj_ID, run_ID, rerun_ID, cam_col, field_ID, spec_obj_ID, plate, MJD, fiber_ID

Four colour index features were engineered as differences between photometric filter magnitudes. Colour indices represent the true colour and temperature of an object independently of its distance:

u_g = u - g
g_r = g - r
r_i = r - i
i_z = i - z

The target variable class was encoded with LabelEncoder (GALAXY=0, QSO=1, STAR=2). No missing values were found in the dataset — no imputation was required.

Note: StandardScaler was applied only for k-NN (distance-based). Random Forest is scale-invariant and was trained on raw feature values.


Models

Random Forest (primary classifier)

Ensemble of decision trees using bagging and random feature subsets. Final prediction by majority vote:

$$\hat{y} = \text{mode}(h_1(x), h_2(x), \ldots, h_T(x))$$

Grid Search — cv=3, n_jobs=-1:

Hyperparameter Values searched Best
n_estimators 100, 200 100
max_depth None, 15, 25 25
min_samples_split 2, 5 5

k-NN (baseline)

Non-parametric distance-based classifier. Grid Search — cv=5, scoring='accuracy':

Hyperparameter Values searched Best
n_neighbors 3, 5, 7, 9, 13, 15 5
weights uniform, distance distance
metric euclidean, manhattan manhattan

Results

Overall metrics (test set, weighted average)

Model Accuracy Precision Recall F1-Score
Random Forest 0.98 0.98 0.98 0.98
k-NN 0.95 0.95 0.95 0.94

Random Forest — per-class breakdown

Class Precision Recall F1-Score Support
GALAXY 0.98 0.99 0.98 11 860
QSO 0.97 0.93 0.95 3 797
STAR 0.99 1.00 1.00 4 343

ROC — Area Under the Curve (One-vs-Rest)

Model AUC GALAXY AUC QSO AUC STAR
Random Forest 0.995 0.993 1.000
k-NN 0.975 0.971 0.990

Key observations

  • Stars are classified near-perfectly by RF (only 3 misclassifications out of 4 343). k-NN misclassified 199 stars — 11× worse. Stars have redshift ≈ 0, which RF exploits immediately via feature importance.
  • Quasars are the hardest class (Recall = 0.93 for RF). They are physically active galactic nuclei and share photometric properties with galaxies in certain redshift ranges.
  • redshift is by far the most important feature (~52% of total importance), confirming its astrophysical significance as a distance/class discriminator.

Feature Importance

Feature Importance

Top features by importance:

  1. redshift — ~52% (dominant)
  2. r_i — ~12%
  3. g_r — ~11%
  4. u_g, i_z — ~5% each

Positional coordinates (alpha, delta) carry negligible importance (<1%), confirming that object position on the sky does not determine its class.


Getting Started

Requirements

python >= 3.9
scikit-learn
pandas
numpy
matplotlib
seaborn

Install dependencies:

pip install -r requirements.txt

A requirements.txt can be generated from the virtual environment with pip freeze > requirements.txt.

Running the models

Place the dataset CSV in the data/ directory, then:

# Train and evaluate Random Forest
python rf.py

# Train and evaluate k-NN
python knn.py

Charts are saved automatically to the charts/ directory.

About

Repository for project - Stellar Classification with random forest. Results will be compared with KNN classifier.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages