다음 코드에서 모양이 'log_specgrams'입니다 (20,1,12060). 모양을 (20, 60, 201, 1)로 변경하고 싶습니다. 그래서 나는 이와 같은 코드를 작성했습니다.3d 배열을 4 차원 배열로 변경 numpy
log_specgrams = np.asarray(log_specgrams).reshape(len(log_specgrams), 60, 201, 1)
하지만 오류가 준 :
Traceback (most recent call last):
File "D:/for-test.py", line 26, in <module>
features = extract_features(parent_dir,sub_dirs)
File "D:/for-test.py", line 17, in extract_features
log_specgrams = np.asarray(log_specgrams).reshape(len(log_specgrams), 60, 201, 1)
File "C:\Users\CHS\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not broadcast input array from shape (12060) into shape (1)
(1, 12060)
전체 코드 :
import glob
import os
import librosa
import numpy as np
def extract_features(parent_dir, sub_dirs, file_ext="*.wav"):
log_specgrams = []
for l, sub_dir in enumerate(sub_dirs):
for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)):
X_in, sample_rate = librosa.load(fn)
melspec = librosa.feature.melspectrogram(y=X_in, sr=sample_rate, n_fft=1024, hop_length=441, n_mels=60)
logmel = librosa.logamplitude(melspec)
logmel = logmel.T.flatten()[:, np.newaxis].T
log_specgrams.append(logmel)
print(np.shape(logmel))
log_specgrams = np.asarray(log_specgrams).reshape(len(log_specgrams), 60, 201, 1)
print(np.shape(log_specgrams))
A = features
return np.array(log_specgrams)
parent_dir = 'Sound-Data_small'
sub_dirs= ['fold1','fold2']
features = extract_features(parent_dir,sub_dirs)
정말 'log_specgrams'의 모양을 변경하려면
(20,1,12060) (20, 60, 201, 1)을 포함한다.
'asarray'에서 오류가 발생하는 것처럼 보입니다. 즉, 다시 모양을 변경하기도 전에 나타납니다. 아마도'log_specgrams'의 내용이 균질하지 않을까요? –
간단한 '모양 변경'이 효과가 있습니까? 작은 크기로 테스트하여 진행 상황을 볼 수 있습니다. 크기 '1'차원의 중요성은 무엇입니까? 왜 플립 위치에? – hpaulj
예. 오류는 asarry에서 발생합니다. 저는 파이썬에 대한 초보자이기 때문에 여러분의 질문을 이해하지 못합니다 (균질?) 더 간단하게 말해 줄 수 있습니까? '균질'이라면 해결책이 있습니까? –