I have a question that is similar (but not the same) to this one: The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (33,) + inhomogeneous part
When I run this code:
#Creating spectrograms
import librosa
def prepare_audio(audio_path):
list_matrices = []
y,sr = librosa.load(audio_path,sr=22050)
D = np.abs(librosa.stft(y))**2
S = librosa.feature.melspectrogram(S=D, sr=sr)
list_matrices.append(S)
return list_matrices
all_X_data = []
targets = []
#Loading files into spectrogram and putting the result in a matrix
from pathlib import Path
path = '/Users/xyz/Desktop/Audio_Samplepack'
pathlist = Path(path).glob('**/*.wav')
for path in pathlist:
path_in_str = str(path)
audio = prepare_audio(path_in_str)
all_X_data += audio
targets += ([0]*len(audio))
#Train_test_split with above data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(np.array(all_X_data),
np.array(targets),
test_size=0.33,
random_state=42)
For using np.array(all_X_data)
I get the same error as in the above mentioned question, that is:
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (128,182) + inhomogeneous part.
Which is totally correct, as my matrix all_X_data should have 182 lines with 128 columns. But the error suggests that there are elements inside all_X_data that are shorter or longer than 128 which surprises me tbh as they are all preprocessed by the librosa.melspectrogram function, so I thought they'd always all be 128 in width by default.
I checked their length individually with:
for element in all_X_data:
print(len(element))
But the result was 128 for all the 182 elements inside all_X_data: 128 128 128 . . . 128
I thought about an automatic solution to this so I added this code afterwards:
print(len(all_X_data))
for element in all_X_data:
if len(element) != 128:
all_X_data.remove(element)
print(len(all_X_data))
Resulting in: 182 182
Which suggests that the code didn't remove any of the 182 elements and that all of them should be 128 in width. So that actually confuses me as the error claims that there are elements inside the matrix that do not actually match the width of 128 ... Does anyone know what could be the problem here?
I've read questions like this one: Is there a simple way to delete a list element by value?
So don't get me wrong, I am not asking you how to detect or even remove elements of a list - the question here is why can't I find any of those elements that ought to be removed? Where is the inhomogeneous part? I can't find it