Automatic classification of astronomical objects — galaxies, quasars (QSO), and stars — using supervised machine learning on photometric data from the Sloan Digital Sky Survey (SDSS).
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.
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
- 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 | Count |
|---|---|
| GALAXY | 11 860 |
| STAR | 4 343 |
| QSO | 3 797 |
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 - zThe 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:
StandardScalerwas applied only for k-NN (distance-based). Random Forest is scale-invariant and was trained on raw feature values.
Ensemble of decision trees using bagging and random feature subsets. Final prediction by majority vote:
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 |
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 |
| 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 |
| 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 |
| Model | AUC GALAXY | AUC QSO | AUC STAR |
|---|---|---|---|
| Random Forest | 0.995 | 0.993 | 1.000 |
| k-NN | 0.975 | 0.971 | 0.990 |
- 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.
redshiftis by far the most important feature (~52% of total importance), confirming its astrophysical significance as a distance/class discriminator.
Top features by importance:
redshift— ~52% (dominant)r_i— ~12%g_r— ~11%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.
python >= 3.9
scikit-learn
pandas
numpy
matplotlib
seabornInstall dependencies:
pip install -r requirements.txtA
requirements.txtcan be generated from the virtual environment withpip freeze > requirements.txt.
Place the dataset CSV in the data/ directory, then:
# Train and evaluate Random Forest
python rf.py
# Train and evaluate k-NN
python knn.pyCharts are saved automatically to the charts/ directory.
