Repository navigation
Code
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.
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)
-
Clone the repository to your local machine:
git clone https://github.com/your-username/your-repository.git
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
fits_dir = '/Users/ronaktoprani/Desktop/internship/fitsWide' label_file = '/Users/ronaktoprani/Desktop/internship/labels/image.txt'
galaxy_images = [] galaxy_labels = []
This section defines file paths and loads FITS image data from specified directories.
Label Encoding python Copy code
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
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
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
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
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
y_pred = clf.predict(X_test_imputed)
accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy)
cm = confusion_matrix(y_test, y_pred) print("Confusion matrix:\n", cm)
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.
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.
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)
-
Clone the repository to your local machine:
git clone https://github.com/your-username/your-repository.git
To run the code, execute the following command:
shell 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
fits_dir = '/Users/ronaktoprani/Desktop/internship/fitsWide' label_file = '/Users/ronaktoprani/Desktop/internship/labels/image.txt'
galaxy_images = [] galaxy_labels = []
This section defines file paths and loads FITS image data from specified directories.
Label Encoding python
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
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
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
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
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
y_pred = clf.predict(X_test_imputed)
accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy)
cm = confusion_matrix(y_test, y_pred) print("Confusion matrix:\n", cm)
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.