Data Science: Transforms¶
In [2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [3]:
df = pd.read_csv ('~/work/sonam-dendup/datasets/StudentsPerformance.csv')
In [4]:
df2 = df[['reading score', 'math score']].copy()
df2.head(2)
Out[4]:
| reading score | math score | |
|---|---|---|
| 0 | 72 | 72 |
| 1 | 90 | 69 |
In [5]:
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import pandas as pd
# Select numeric features (excluding target 'mathscore' if predicting)
features = df2.select_dtypes(include=[np.number]).columns
x = df2[features].values
# Standardize (Crucial for PCA)
x_scaled = StandardScaler().fit_transform(x)
# Apply PCA (e.g., reduce to 2 components for visualization)
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x_scaled)
# Results
print(f"Explained Variance Ratio: {pca.explained_variance_ratio_}")
Explained Variance Ratio: [0.90878983 0.09121017]
In [6]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
# 1. Select numeric features related to math performance
# Ex: ['math_test', 'homework_score', 'attendance', 'study_hours']
features = df2.select_dtypes(include=[np.number]).columns
x = df2[features].values
# 2. Standardization (Crucial: PCA is sensitive to scale)
# Standardizes to mean=0 and variance=1
x_scaled = StandardScaler().fit_transform(x)
# 3. Apply PCA
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x_scaled)
# 4. Create a DataFrame for results
pca_df = pd.DataFrame(data=principalComponents,
columns=['PC1', 'PC2'])
In [7]:
# Plotting the 2D Projection
plt.figure(figsize=(8, 6))
sns.scatterplot(x='PC1', y='PC2', data=pca_df, alpha=0.7)
plt.axhline(0, color='gray', linestyle='--')
plt.axvline(0, color='gray', linestyle='--')
plt.title('PCA of Math Score Data')
plt.xlabel(f'PC1 ({pca.explained_variance_ratio_[0]*100:.1f}%)')
plt.ylabel(f'PC2 ({pca.explained_variance_ratio_[1]*100:.1f}%)')
plt.show()
Fast Fourier Transform (FFT)¶
In [8]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq
# 1. Prepare the signal
# Remove mean to focus on variations (DC offset removal)
scores = df2['math score'].values
scores_detrended = scores - np.mean(scores)
N = len(scores)
# 2. Compute FFT
yf = fft(scores_detrended)
xf = fftfreq(N, 1) # Assumes 1 sample per unit of time (e.g., 1 test per month)
# 3. Plot Magnitude Spectrum
plt.figure(figsize=(8, 5))
plt.plot(xf[:N//2], 2.0/N * np.abs(yf[:N//2])) # Plot positive frequencies
plt.title('Frequency Domain: Math Score Periodicities')
plt.xlabel('Frequency (Cycles per unit time)')
plt.ylabel('Magnitude (Strength of Cycle)')
plt.grid(False)
plt.show()