Skip to content
Ronak Toprani edited this page Sep 14, 2023 · 4 revisions

Galactic Component Mapping with Machine Learning

This project focuses on applying machine learning techniques to map galactic components in the M74 galaxy using astronomical FITS image data. The code presented here demonstrates the entire pipeline, including data loading, preprocessing, feature extraction, training a support vector machine (SVM) classifier, and evaluating the model's performance.

Table of Contents

Prerequisites

Ensure you have the following requirements:

  • Python 3.x
  • Required Python libraries (numpy, matplotlib, astropy, skimage, scikit-learn)
  • FITS image data in the specified directory (fits_dir)
  • Label file containing image labels (label_file)

Getting Started

  1. Clone the repository to your local machine:

    git clone https://github.com/your-username/your-repository.git
    
    

Usage

To run the code, execute the following command:

shell Copy code python galactic_mapping.py This will load the FITS image data, preprocess it, train an SVM classifier, and evaluate its performance. The accuracy and confusion matrix will be displayed in the console, and a confusion matrix plot will be generated.

Code Structure The code is organized as follows:

Data Loading python Copy code

Define file paths

fits_dir = '/Users/ronaktoprani/Desktop/internship/fitsWide' label_file = '/Users/ronaktoprani/Desktop/internship/labels/image.txt'

Loading image data

galaxy_images = [] galaxy_labels = []

Load multiple FITS files separately and append them to the list

This section defines file paths and loads FITS image data from specified directories.

Label Encoding python Copy code

Encode labels using LabelEncoder

label_encoder = LabelEncoder() galaxy_labels_encoded = label_encoder.fit_transform(galaxy_labels) The labels are encoded using LabelEncoder to prepare them for machine learning.

Feature Extraction python Copy code

Extract HOG features from images

max_len = 0 for image in galaxy_images: hog_feature = feature.hog(image, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), transform_sqrt=True, feature_vector=True) This section extracts Histogram of Oriented Gradients (HOG) features from the images.

Data Preprocessing python Copy code

Pad feature vectors with zeros so that they all have the same length

for i in range(len(galaxy_features)): hog_feature = galaxy_features[i] new_feature = np.zeros(max_len) new_feature[:hog_feature.size] = hog_feature galaxy_features[i] = new_feature Feature vectors are padded with zeros to ensure uniform length, and the data is split into training and testing sets.

Handling Missing Values python Copy code

Handle missing values using SimpleImputer

imputer = SimpleImputer(strategy='mean') X_train_imputed = imputer.fit_transform(X_train) X_test_imputed = imputer.transform(X_test) Missing values in the dataset are imputed using SimpleImputer.

Training the Classifier python Copy code

Train an SVM classifier

clf = SVC(kernel='rbf', random_state=42) clf.fit(X_train_imputed, y_train) An SVM classifier with an RBF kernel is trained on the data.

Model Evaluation python Copy code

Predict labels for test set

y_pred = clf.predict(X_test_imputed)

Calculate accuracy score

accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy)

Calculate confusion matrix

cm = confusion_matrix(y_test, y_pred) print("Confusion matrix:\n", cm)

Decode labels

decoded_labels = label_encoder.inverse_transform(range(len(label_encoder.classes_))) The classifier's accuracy and confusion matrix are computed and displayed, and the labels are decoded.

Conclusion This documentation provides an overview of the code structure and its functionality. You can use it as a guide to understand and use the code effectively for galactic component mapping with machine learning.

javascript Copy code

Please replace your-username and your-repository with your actual GitHub username and repository name. This documentation should provide a comprehensive explanation of your code's functionality and usage.