<jupyter_start><jupyter_text>Stellar ClassificationA classifier which classifies a set of parameters into either of Stars, Galaxies and Quasars. From Sloan Digital Sky Survey DR17Dataset used: Stellar Classification Dataset - SDSS17https://www.kaggle.com/fedesoriano/stellar-classification-dataset-sdss17 ContextIn astronomy, stellar classification is the classification of stars based on their spectral characteristics. The classification scheme of galaxies, quasars, and stars is one of the most fundamental in astronomy. The early cataloguing of stars and their distribution in the sky has led to the understanding that they make up our own galaxy and, following the distinction that Andromeda was a separate galaxy to our own, numerous galaxies began to be surveyed as more powerful telescopes were built. This datasat aims to classificate stars, galaxies, and quasars based on their spectral characteristics. Content of dataThe data consists of 100,000 observations of space taken by the SDSS (Sloan Digital Sky Survey). Every observation is described by 17 feature columns and 1 class column which identifies it to be either a star, galaxy or quasar.1. obj_ID = Object Identifier, the unique value that identifies the object in the image catalog used by the CAS2. alpha = Right Ascension angle (at J2000 epoch)3. delta = Declination angle (at J2000 epoch)4. u = Ultraviolet filter in the photometric system5. g = Green filter in the photometric system6. r = Red filter in the photometric system7. i = Near Infrared filter in the photometric system8. z = Infrared filter in the photometric system9. run_ID = Run Number used to identify the specific scan10. rereun_ID = Rerun Number to specify how the image was processed11. cam_col = Camera column to identify the scanline within the run12. field_ID = Field number to identify each field13. spec_obj_ID = Unique ID used for optical spectroscopic objects (this means that 2 different observations with the same spec_obj_ID must share the output class)14. class = object class (galaxy, star or quasar object)15. redshift = redshift value based on the increase in wavelength16. plate = plate ID, identifies each plate in SDSS17. MJD = Modified Julian Date, used to indicate when a given piece of SDSS data was taken18. fiber_ID = fiber ID that identifies the fiber that pointed the light at the focal plane in each observation Referenceshttps://www.kaggle.com/prasadchaskar/steller-classification-with-97-accuracyhttps://www.kaggle.com/beyzanks/stellar-classification-98-4-acc-100-auc  Notebook Step 1Import libraries and the dataset from the csv(comma separated values) file.<jupyter_code># Importing standard libraries
import pandas as pd
import numpy as np
import seaborn as sns
df = pd.read_csv("star_classification.csv")<jupyter_output><empty_output><jupyter_text>Step 2Exploring data for the values we have and if we any problems associated with the data.<jupyter_code>df.head()
# The columns in our data
df.columns
df.info()
# Checking if we got any missing values in the data.
df.isna().sum()
# Based on data only certain columns are significant. Removed columns are the ones displaying IDs,
# which are of little to no importance in out process of making a model.
stellar_df = df[['alpha', 'delta', 'u', 'g', 'r', 'i', 'z', 'cam_col', 'class', 'redshift', 'plate', 'MJD']]
stellar_df.shape
stellar_df.describe()<jupyter_output><empty_output><jupyter_text>Step 3Visualising the data for understanding how it is spread out by using differnt plots using the seaborn library.<jupyter_code>sns.scatterplot(data=stellar_df, x="alpha", y="redshift", color="orange")
sns.histplot(data=stellar_df, x="delta", color="black")
sns.histplot(data=stellar_df, x="redshift", color="r")<jupyter_output><empty_output><jupyter_text>Step 4Making the training and testing data split based on the 'class' column which is to be predicted.<jupyter_code>stellar_df["class"].value_counts()
# Modifying the class column to be a value rather than a string for the model
stellar_df["class"] = [0 if i=="STAR" else 1 if i=="QSO" else 2 for i in stellar_df["class"]]
# X is the set of features based on which y (the class) is to be predicted/classified.
X = stellar_df.drop('class',axis=1)
y = stellar_df['class']
print(X.shape)
print(y.shape)
# Making the test size to 25% of total dataset and setting random state to 
# a fixed value will guarantee that same sequence of random numbers are generated each time we run the code. 
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 7)<jupyter_output><empty_output><jupyter_text>Step 5Preprocess the data and try different models (Random forest & Support Vector Machine here) while aiming for getting the highest accuracy.<jupyter_code>from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test  = scaler.transform(X_test)
# Random forest classifier
from sklearn.ensemble import RandomForestClassifier

r_forest = RandomForestClassifier()
r_forest.fit(X_train,y_train)

predicted = r_forest.predict(X_test)
score = r_forest.score(X_test, y_test)
rf_score = np.mean(score)
print(f"Accuracy Achieved by Random forest classifier: {rf_score*100} %")
# SVM - Support Vector Machine Classifier
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline

svem = make_pipeline(StandardScaler(), SVC(gamma='auto'))
svem.fit(X_train,y_train)

predicted = svem.predict(X_test)
score = svem.score(X_test, y_test)
svem_score = np.mean(score)
print(f"Accuracy Achieved in SVM: {svem_score*100} %")
# Neural Network
from sklearn.neural_network import MLPClassifier
nn = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1, max_iter=1000)
nn.fit(X_train, y_train)
score = nn.score(X_test, y_test)
nn_score = np.mean(score)
print(f"Accuracy Achieved in Neural Network: {nn_score*100} %")
# Neural Network with different hidden layer size
from sklearn.neural_network import MLPClassifier
nn2 = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(6, 3), random_state=1, max_iter=1000)
nn2.fit(X_train, y_train)
score = nn2.score(X_test, y_test)
nn2_score = np.mean(score)
print(nn2_score*100)
model_scores=[rf_score, nn2_score, svem_score]
for i in model_scores:
    print(f"Score: {i}")<jupyter_output>Score: 0.97916
Score: 0.97156
Score: 0.95876<jupyter_text>ResultWe find that our Random forest classifier performs the best that is 97.92%<jupyter_code>print(X_train[19])
class_dict = {
  0: "STAR",
  1: "QSO",
  2: "GALAXY"
}
prediction = r_forest.predict([X_train[1]])
print(f"Predicted class: {class_dict[prediction[0]]}")
# Taking above example and changing values 
exp_data = [0.5284327 , -1.12519075,  0.19588647,  0.0443259 ,  0.28241633,
        0.14658281,  0.04755141,  0.40811594,  2.92790288, -0.27501299,
        0.11645269]

prediction = r_forest.predict([exp_data])
print(f"Predicted class: {class_dict[prediction[0]]}")<jupyter_output>Predicted class: QSO<jupyter_text>Save and load model<jupyter_code>import pickle
filename = 'rf_model.pkl'
with open(filename, 'wb') as file:
    pickle.dump(r_forest, file)
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.score(X_test, y_test)
print(f"Resultant Accuracy from saved model: {result*100} %")
y_pred=loaded_model.predict(X_test)
y_pred<jupyter_output><empty_output><jupyter_text>Result VisualizationBy Building a confusion Matrix.<jupyter_code>from sklearn.metrics import confusion_matrix

#Generate the confusion matrix
cf_matrix = confusion_matrix(y_test, y_pred)

print(cf_matrix)
ax = sns.heatmap(cf_matrix, annot=True, cmap='Blues')
ax.set_title('Confusion Matrix')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup