Principal component analysis (PCA) on astronomical data: interstellar medium (Part I)

Diffuse interstellar bands (DIBs) with central wavelengths at around 5780 and 5797 Angstrom were observed toward stars in the Milky Way, M31, and LMC (Cordiner 2011).
import pandas as pd
df = pd.read_csv('ensor2017.csv', encoding='latin-1')
Original data, taken from Paper 1.
df['n_h2'] = df['n_h2']*10**df['power']
df['n_h'] = (df['n_hi']*10**21) + (2*df['n_h2'])
Additional column of N(H) or the column density of total hydrogen.
from sklearn.preprocessing import StandardScaler
features = ['ebv','n_h']
x = df.loc[:, features].values
x = StandardScaler().fit_transform(x)
The measurements before and after standardization step.
import numpy as np
from sklearn.decomposition import PCA
pca = PCA(n_components=2)principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents,
columns = ['PC1', 'PC2'])
Principal component 1 and principal component 2
eigenvalues = pca.explained_variance_
Array[0] for PC1 and array[1] for PC2.
variation = pca.explained_variance_ratio_
Array[0] for PC1 and array[1] for PC2.
component = pca.components_
The transformation matrix.
principalDf = np.dot(component,x.T)
Transformed data for PC1 and PC2.
import matplotlib.pyplot as plt
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('standarized ebv', fontsize = 15)
ax.set_ylabel('standarized nh', fontsize = 15)
ax.set_ylim(-2,4)
ax.set_xlim(-2,4)
ax.scatter(x[:,0],x[:,1],s = 50)
ax.grid()
Scatter plot of standardized measurements.
import matplotlib.pyplot as plt
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('PC1', fontsize = 15)
ax.set_ylabel('PC2', fontsize = 15)
ax.set_xlim(-3,4.5)
ax.set_ylim(-3,3)
ax.scatter(principalDf.T[:,0],principalDf.T[:,1],s = 50)
ax.grid()
Scatter plot of the principal components.
  • Adding the data used in Paper I primary source.
  • Fixing minor grammatical errors.
  • Readjust the paragraph for continuity.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ade N Istiqomah

Ade N Istiqomah

12 Followers

An astronomer-in-the-making with interest in data of any fields, astronomical observation, and stellar spectra.