2017-10-21 10 views
0

ファイルから翻訳済みの行列を作成しようとしています。ファイルには、次のようになりますし、発症を表し、ピアノのための時間が、私はこの私を行うには、このようなnumpy maxtixを構築する

Event State 
time  Piano state, array of length 88 
... 

のようなピアノの状態表現とイベントの時間にこれを平らにしようとしています

OnsetTime OffsetTime MidiPitch 
0.500004 0.85356  37 
1.20712  1.50441  38 
1.80171  2.0517  39 
... 

ノートオフセットフォローコード

eventArray = np.array([]) 
with open('event_log.txt') as tsv: 
     noteState = [0] * 88 
     iterator = iter(csv.reader(tsv, dialect="excel-tab")) 
     next(iterator) 
     for line in iterator: 
      notePosition = int(float(line[2])) - 21 
      noteState[notePosition] = 1 
      np.concatenate(eventArray, np.array([float(line[0]), noteState])) 
      noteState[notePosition] = 0 
      np.concatenate(eventArray, np.array([float(line[1]), noteState])) 

を構築しかし、私はこれを実行すると、私は次のようなエラー

を取得しています
File "main.py", line 32, in <module> 
    np.concatenate(eventArray, np.array([float(line[0]), noteState])) 
    ValueError: setting an array element with a sequence. 

この行列をどのように構築する必要がありますか?私はnumpyを使用して、必要に応じて行列をスライスして整形し直します。

EDIT

コメントで提案を試みた後、私は今

np.concatenate(eventArray, np.array([[float(line[0])], noteState])) 

を持っており、

Traceback (most recent call last): 
    File "main.py", line 32, in <module> 
    np.concatenate(eventArray, np.array([[float(line[0])], noteState])) 
TypeError: only integer scalar arrays can be converted to a scalar index 
+0

これは微妙ですが、新しいエラーが発生しました。 'TypeError:整数スカラー配列のみスカラーインデックスに変換できます。' – JME

+0

私はそれが間違っていたと思います – GWW

答えて

2

は繰り返しませんconcatenateを行い、次のエラーが表示されます。 np.concatenateは、各ステップごとに新しい配列を返します。配列内の配列は変更されません。

alist = [] 
with open('event_log.txt') as tsv: 
     noteState = [0] * 88 
     iterator = iter(csv.reader(tsv, dialect="excel-tab")) 
     next(iterator) 
     for line in iterator: 
      notePosition = int(float(line[2])) - 21 
      noteState[notePosition] = 1 
      alist.append(np.array([float(line[0]), noteState])) 
      noteState[notePosition] = 0 
      alist.append(np.array([float(line[1]), noteState])) 

これは配列のリストを作成するはずです。これらの配列の長さがすべて同じ場合は、floatの2次元配列を作成する必要があります。長さが異なる場合は、同じ値のフラット(1d)配列を作成することをお勧めします。

私は残りのコードが正しいと仮定しています。

alistを印刷して、値が妥当であることを確認します。

関連する問題