Improve the features available to your model, Remove some of the NOISE present in the data.
- In audio data, a common way to do this is to smooth the data and then rectify it so that the total amount of sound energy over time is more distinguishable.
# Rectify the audio signal
audio_rectified = audio.apply(np.abs)
You can also calculate the absolute value of each time point. This is also called Rectification because you ensure that all time points are positive.
Smooth your data by taking the
rolling mean in a window of say 50 samples
audio_rectified_smooth = audio_rectified.rolling(50).mean()
- Calculating the envelope of each sound and smoothing it will eliminate much of the noise and you have a cleaner signal.
Calculate a spectrogram of sound(i.e combining of windows Fourier transforms). This describes what spectral content (e.g., low and high pitches) are present in the sound over time. there is a lot more information in a spectrogram compared to a raw audio file. By computing the spectral features, you have a much better idea of what's going on.
This is how you can calculate the STFT
# Calculating the STFT
# Import the functions we'll use for the STFT
from librosa.core import stft, amplitude_to_db
from librosa.display import specshow
# Calculate our STFT
HOP_LENGTH = 2**4
SIZE_WINDOW = 2**7
audio_spec = stft(audio, hop_length=HOP_LENGTH, n_fft=SIZE_WINDOW)
# Convert into decibels for visualization
spec_db = amplitude_to_db(audio_spec)
specshow(spec_db, sr=sfreq, x_axis='time', y_axis='hz', hop_length=HOP_LENGTH)
Try Spectral feature engineering
you can also perform the Spectral feature engineering on your baby audio data
- since each time series has a different spectral pattern.
- We can calculate these spectral patterns by analyzing the spectrogram.
- For example, spectral bandwidth and spectrum centroids describe where most of the energy is at each moment in time.
# Calculate the spectral centroid and bandwidth for the spectrogram
bandwidths = lr.feature.spectral_bandwidth(S=spec)
centroids = lr.feature.spectral_centroid(S=spec)
# Display these features on top of the spectrogram
ax = specshow(spec, x_axis='time', y_axis='hz', hop_length=HOP_LENGTH)
ax.fill_between(times_spec, centroids - bandwidths / 2,centroids + bandwidths / 2, alpha=0.5)
Now you can Combine spectral(
spec $\rightarrow$ spectral dataframe) and temporal features in a classifier
centroids_all = 
bandwidths_all = 
for spec in spectrograms:
bandwidths = lr.feature.spectral_bandwidth(S=lr.db_to_amplitude(spec))
centroids = lr.feature.spectral_centroid(S=lr.db_to_amplitude(spec))
# Calculate the mean spectral bandwidth
# Calculate the mean spectral centroid
# Create our X matrix
X = np.column_stack([means, stds, maxs, tempo_mean, tempo_max, tempo_std, bandwidths_all, centroids_all])
for your logistic regression models
- One of the ways to improve accuracy is by optimizing the prediction probability cutoff scores generated by your logit model.
- You can Normalize all your features to the same scale before putting them in a machine learning model.
- Look for class imbalance in your data.
- You can Optimize on other Metrics also such as Log Loss and F1-Score.
- Tune the hyperparameters of your model. In the case of LogisticRegression, parameter $C$ is a hyperparameter.