I'm trying to train a model for speaker identification using MFCC and HMM, during run time I got the following error
line 55, in <module>
X = X_train[y_train == speaker]
IndexError: boolean index did not match indexed array along dimension 0; dimension is 1 but corresponding boolean dimension is 3301
which is this line of code
X = X_train[y_train == speaker]
I tried printing the shape of the array to get a better understanding of the problem and getting this results
(1, 3301)
(3301,)
[False False False ... True True True]
but I can't seem to wrap my head around on how to solve it. Any tips and help is greatly appreciated.
here is my code block regarding said issue
import os
import numpy as np
import scipy.io.wavfile as wav
from python_speech_features import mfcc
from hmmlearn import hmm
import matplotlib.pyplot as plt
# Define the number of MFCC features and HMM states
num_features = 13
num_states = 5
# Define the directory containing the training data
data_dir = "speech-data"
# Define the names of the speaker subdirectories in the training data directory
speakers = ["speaker1", "speaker2"]
# Create a list to hold the training feature vectors and labels
X_train = []
y_train = []
# Loop over the speaker subdirectories
for speaker in speakers:
# Get the path to the speaker's training subdirectory
train_dir = os.path.join(data_dir, speaker, "train")
# Loop over the audio files in the speaker's training subdirectory
for file in os.listdir(train_dir):
# Get the path to the audio file
file_path = os.path.join(train_dir, file)
# Load the audio signal
rate, signal = wav.read(file_path)
# Extract MFCC features from the signal
features = mfcc(signal, rate, numcep=num_features)
# Append the features and label to the training lists
X_train.append(features)
y_train.append(speaker)
# Convert the training data to numpy arrays
X_train = np.array([X_train], dtype=object)
y_train = np.array(y_train)
print(X_train.shape)
print(y_train.shape)
print(y_train == speaker)
# Create an HMM for each speaker
models = []
for speaker in speakers:
# Get the training feature vectors and labels for the current speaker
X = X_train[y_train == speaker]
# Create an HMM for the current speaker
model = hmm.GaussianHMM(n_components=num_states)
model.fit(X)
# Add the HMM to the list of models
models.append(model)
I tried reshaping the array but to no avail of fixing the error. I was expecting that by reshaping the array based on the dimensions of my samples would be better.
so update, i tried changing the code based on what Chrysophylaxs said and changed it from this
# Convert the training data to numpy arrays
X_train = np.array([X_train], dtype=object)
y_train = np.array(y_train)
print(X_train.shape)
print(y_train.shape)
print(y_train == speaker)
# Create an HMM for each speaker
models = []
for speaker in speakers:
# Get the training feature vectors and labels for the current speaker
X = X_train[y_train == speaker]
to this
# Convert the training data to numpy arrays
X_train = np.array([X_train], dtype=object)
y_train = np.array(y_train)
print(X_train.shape)
print(y_train.shape)
print(y_train == speaker)
# Create an HMM for each speaker
models = []
for speaker in speakers:
# Get the training feature vectors and labels for the current speaker
X = X_train[:, y_train == speaker]
now i am getting a type error, mainly this one
TypeError: only size-1 arrays can be converted to Python scalars