Bag of ML Words

ML = Machine Learning, Music Love, and Miscellaneous things in daily Livings

Python: hstackとvstack

横長(縦長)のarrayを、一つのarrayにくっつけるメソッド。

つまり、list of array --> a long arrayにしてくれる。

 

この意義がわからなかったんだけど、こういう状態で使えるらしい。

例えばclassification問題とかで、データの識別結果を出して、クラスごとにまとまりにデータを並べ替えたい時*1

>>> Z = np.random.random( (10, 20) )
>>> argZ = np.argmax(Z, axis=0) # 20個のデータの識別結果
>>> print argZ
[3 3 7 1 1 2 7 4 2 0 3 1 4 5 8 3 5 6 0 6]
>>> idx0 = np.where(argZ == 0) # クラス0のデータのインデックス
>>> idx1 = np.where(argZ == 1) # クラス1のデータのインデックス
>>> idx2 = np.where(argZ == 2)
>>> list_idx = [] # クラスごとにソートされたアイテムのインデックスがほしい
>>> list_idx.append(idx0)
>>> list_idx.append(idx1)
>>> list_idx.append(idx2)
>>> list_idx # これは、list of arrayed list
[(array([ 9, 18]),), (array([ 3,  4, 11]),), (array([5, 8]),)]
>>> stacked_list_idx = np.hstack(list_idx)
>>> Y = Z[:, stacked_list_idx]

>>> argY = np.argmax(Y, axis=0)
>>> print argY
0 0 1 1 1 2 2



 

 

*1:というかそういう状況だった