2017-04-20 6 views
0

次のコードから、私はshape(20,1,12060)で 'log_specgrams'を取得しました。 形状を(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)となる。

+0

エラーは 'asarray'で発生するように見えます。つまり、変更する前にエラーが発生しているようです。おそらく 'log_specgrams'の内容は同質ではありませんか? –

+0

シンプルな「変形」は機能しますか?小さなサイズでテストして、何が起こっているかを確認してください。そのサイズ「1」次元の意味は何ですか?なぜポジションのフリップ? – hpaulj

+0

はい。エラーはasarryで発生します。私はPythonの初心者ですので、私はあなたの質問を理解していません(均質?)もっと簡単に教えてもらえますか?それが「均質」ならば、解決策がありますか? –

答えて

0

リシェイプは、入力(20,1,12060)であり、所望の出力が(20, 60, 201, 1)であると仮定すると、計算、すなわち、タプルとして欠落寸法自体

+0

ありがとうございます。残念ながら、私は同じエラーがありました。別の意見がありますか? (solution?) –

+1

'reshape(-1、60、201、1)'は 'None'と同じように動作します。 –

+1

実際、最近のnumpy(1.11.1)の 'None'はエラーを返し、' -1'は期待通りに動作します。 –

0

log_specgrams = np.asarray(log_specgrams).reshape((len(log_specgrams), 60, 201, 1)) 

又は

log_specgrams = np.asarray(log_specgrams).reshape((None, 60, 201, 1)) 

なしパラメータを取り1ディメンションをスワップした場合、次のように正しく動作するはずです。

ランダムデータで
data = np.asarray(log_specgrams) 
data = data.swapaxes(1, 2).reshape(20, 60, 201, 1) 

例:次に

>>> data = np.random.randn(20, 1, 12060) 
>>> data.shape 
(20, 1, 12060) 

>>> data = data.swapaxes(1, 2).reshape(20, 60, 201, 1) 
>>> data.shape 
(20, 60, 201, 1) 

動作は、2つの成分を有することに留意することができます。最初の部分は2番目と3番目の軸をスワイプし、データを(20, 1, 12060)から(20, 12060, 1)に変換します。第2の部分は、第2の軸12060を、サイズが60 x 201の2つの新しい軸で分割します。

これは、異なるサイズの任意の軸で動作しますが、データの並べ替えを必要としない1の軸の場合、data.reshape(20, 60, 201, 1)または@reshapeという@ yarの回答がより単純です。この解決策は軸のサイズが1とは異なる他の問題にも拡張されます。

関連する問題