IndexError: 0维布尔索引不匹配;维数为1,但对应的布尔维数为3301



我正在尝试使用MFCC和HMM训练扬声器识别模型,在运行时我得到了以下错误

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

也就是这行代码

X = X_train[y_train == speaker]

我试着打印数组的形状,以便更好地理解这个问题,并得到这个结果

(1, 3301)
(3301,)
[False False False ...  True  True  True]

但我似乎不知道如何解决它。任何提示和帮助都是非常感谢的。

这是我关于上述问题的代码块

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)

我尝试重塑数组,但无法修复错误。我期望通过基于我的样本的尺寸重塑数组会更好。

所以更新,我试着改变代码基于什么Chrysophylaxs说和改变它从这个

# 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]

# 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]

现在我得到了一个类型错误,主要是这个

TypeError:只有size-1的数组可以转换为Python标量

找到了我的问题的答案,只需要重塑我的数组和填充我的数组与零创建一个相等大小的数组,并为我的X添加这行代码

X = X.reshape(-1, X.shape[-1])

最新更新