非常に大きなデータセットでいくつか問題があります。構造化配列内のエントリを検索/置換する確実で迅速な方法を見つける必要があります。すべてのエントリをループせずに解決策を探しています。Cには高速なソリューションがあることは知っていますが、Pythonでそのためにアプローチする方法がわかりません。また、まさにその目的のための numpy 関数があるのだろうかと思います!
Python 2.7.13 と numpy 1.12.1 を使用しています!
タスク:の中心のリストから孤立したハロイドをdata_centrals
見つけて、孤立
したすべての位置を の位置に設定します。data_orphan
data_centrals
import numpy as np
data = Structured array:
class: ndarray
shape: (189258912,)
dt = [('hostid', '<u8'), ('z_pos', '<f8'), ('x_pos', '<f8'),
('y_pos', '<f8'), ('haloid', '<u8'), ('orphan', 'i1')]
編集済み: 200 個のオブジェクトを含むデータのサブサンプルは、 ここからダウンロードできます。その構造はdtによって与えられます: 最初の列--> hostid、2 番目の --> z_posなど。Python シェルまたはスクリプトにそのままコピー/貼り付けできます ...
以下に、位置を設定するためのコードを示します。
質問:のすべてのエントリをループすることなく、ハロイドを検索して位置を設定するスマートな方法はありますdata_orphan
か?
data_centrals=data[np.where(data['haloid']==data['hostid'])] # (111958237,)
data_orphans=data[np.where(data['orphan']==2)] # (61870681,)
a=0
while a<len(data_orphans):
#check where in data_centrals the haloid of the orphan can be found
position=np.where(data_centrals['haloid']==data_orphans['haloid'][a])
#find the position of data_orphan['haloid'][a] in data
position_data=np.where(data['hostid']==data_orphans['hostid'][a])
#set the positions
data['x_pos'][int(position_data[0])]=data_centrals['x_pos'][int(position[0])]
data['y_pos'][int(position_data[0])]=data_centrals['y_pos'][int(position[0])]
data['z_pos'][int(position_data[0])]=data_centrals['z_pos'][int(position[0])]
a+=1