devnotes.

Image and Audio Features, and Best Practices

Feature extraction for images

Images are grids of pixels. Feeding raw pixels to a model works for small images, but extracting features usually helps.

ApproachDescription
Raw pixelsFlatten the image into a vector (simple, high-dimensional)
Color histogramsDistribution of colors
Edges (Sobel, Canny)Outlines and shapes
HOGHistogram of Oriented Gradients, good for object shapes
SIFT / ORBKeypoint descriptors robust to scale and rotation
CNN features (transfer learning)Take the output of a late layer of a pretrained network (ResNet, VGG, EfficientNet)
import numpy as np
from skimage import io, color, feature

img = io.imread("photo.jpg")
gray = color.rgb2gray(img)

hog_vec, hog_img = feature.hog(gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), visualize=True)
print("HOG feature vector length:", hog_vec.shape[0])

hist = np.histogram(gray, bins=32, range=(0, 1))[0]      # simple intensity histogram

Feature extraction for audio and signals

FeatureMeaning
MFCCMel-frequency cepstral coefficients, standard for speech
SpectrogramFrequency content over time
ChromaPitch classes, useful for music
Zero-crossing rateHow often the signal changes sign
Statistical featuresMean, variance, skewness, FFT components
import librosa

y, sr = librosa.load("speech.wav", sr=16000)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
features = np.concatenate([mfcc.mean(axis=1), mfcc.std(axis=1)])   # fixed-size vector
print(features.shape)

Pros and cons of feature extraction

AdvantagesDisadvantages
Reduces dimensionality and training timeNew features are harder to interpret
Removes redundancy and noiseSome information can be lost
Helps visualizationLinear methods miss non-linear structure
Handles unstructured data (text, images, audio)Needs scaling and careful tuning

Best practices

  1. Scale features before PCA or LDA.
  2. Fit the extractor on the training data only; use transform on validation and test data.
  3. Choose the number of components using explained variance or cross-validation.
  4. Use t-SNE or UMAP for visualization, not as the main model input.
  5. Compare model performance with and without extraction.
  6. Keep the fitted extractor (joblib) so production data goes through the same transformation.
  7. Combine with feature selection: extract first, then drop weak components if needed.

Summary of the Data Preparation journey

ChapterWhat you learned
IntroductionWhy and how to prepare data, pipelines and leakage
Data CleaningDuplicates, formats, structural errors
Missing ValuesTypes of missingness and imputation
OutliersDetection and treatment
Splitting DataTrain, validation and test sets, cross-validation
Feature SelectionFilter, wrapper and embedded methods
Feature ScalingNormalization and standardization
Feature EngineeringCreating, transforming and encoding features
Feature ExtractionPCA, LDA, text, image and audio features
Imbalanced DataMetrics, resampling, class weights

Key takeaways

  • Feature extraction creates new features; feature selection picks existing ones.
  • PCA (unsupervised) maximizes variance; LDA (supervised) maximizes class separation.
  • Text and images need extraction (TF-IDF, embeddings, CNN features) before modeling.
Image and Audio Features, and Best Practices - Feature Extraction | Data Preparation | DevNotes