numpy IndexError:布尔索引与沿着维度0的索引数组不匹配;维度为1,但对应的布尔维度为3301

bkhjykvo  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(326)

我尝试使用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标量

j2cgzkjk

j2cgzkjk1#

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

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

相关问题