2017-12-05 12 views
0

ボールツリーから半径を照会してデータを取得する方法は?例TypeError:unhashable type: 'numpy.ndarray' - ボールツリーから半径を照会してデータフレームからデータを取得する方法は?

from sklearn.neighbors import BallTree 
import pandas as pd 

bt = BallTree(df[['lat','lng']], metric="haversine") 

for idx, row in df.iterrow(): 
    res = df[bt.query_radius(row[['lat','lng']],r=1)] 

のために私は半径r=1であるDFでそれらの行を取得したいです。しかし、それは

行を超える
5183 
(5219, 25) 
5205 
(5219, 25) 
5205 
(5219, 25) 
5221 
(5219, 25) 
Traceback (most recent call last): 
    File "/Users/Chu/Documents/dssg2018/sa4.py", line 45, in <module> 
    df.loc[idx,word]=len(df.iloc[indices[idx]][df[word]==1])/\ 
IndexError: index 5221 is out of bounds for axis 0 with size 5219 

を反復するとき、私は範囲外のインデックスを持って、コードが

bag_of_words = ['beautiful','love','fun','sunrise','sunset','waterfall','relax'] 

for idx,row in df.iterrows(): 
    for word in bag_of_words: 
     if word in row['caption']: 
      df.loc[idx, word] = 1 
     else: 
      df.loc[idx, word] = 0 

bt = BallTree(df[['lat','lng']], metric="haversine") 
indices = bt.query_radius(df[['lat','lng']],r=(float(10)/40000)*360) 

for idx,row in df.iterrows(): 
    for word in bag_of_words: 
     if word in row['caption']: 
      print(idx) 
      print(df.shape) 
      df.loc[idx,word]=len(df.iloc[indices[idx]][df[word]==1])/\ 
          np.max([1,len(df.iloc[indices[idx]][df[word]!=1])]) 
+0

1) ' –

+0

@cᴏʟᴅsᴘᴇᴇᴅいいえ、コンストラクタで渡された配列と同じ2_D配列が必要です。 –

+1

問題はndarrayを入れて行を取得しようとしていますか?私がこれを解決できる方法はありますか? – monotonic

答えて

1

エラーがでないからである最初の答えに続き

TypeError: unhashable type: 'numpy.ndarray' 

型エラーがスローされますBallTreeが返したインデックスは、インデックスに入れるためには適切に使用されません。

は、このようにそれを実行します。

for idx, row in df.iterrows(): 
    indices = bt.query_radius(row[['lat','lng']].values.reshape(1,-1), r=1) 
    res = df.iloc[[x for b in indices for x in b]] 
    # Do what you want to do with res 

この(私たちは一点のみを毎回送信しているので)も行います。

res = df.iloc[indices[0]] 

説明:

私が使用しているがシキット0.20。上に書いたコード:

df[bt.query_radius(row[['lat','lng']],r=1)] 

私のためには機能しませんでした。私はreshape()を使って2次元配列にする必要がありました。 the documentationで述べたように指定された半径r内のインデックスの配列のすぐ

bt.query_radius()戻り配列:

ind : array of objects, shape = X.shape[:-1]

each element is a numpy integer array listing the indices of neighbors of the corresponding point. Note that unlike the results of a k-neighbors query, the returned neighbors are not sorted by distance by default.

そこで、データの実際のインデックスに到達するために2つの配列を反復する必要がありました。

インデックスを取得した後、pandas Dataframeでilocがインデックスを使用してデータにアクセスする方法です。

更新

あなたはbtに個々の点について、それぞれの時間を照会する必要がいけません。すべてのdfを一度に送信して、半径内の点のインデックスを含む2次元配列をそのインデックスで指定された点に戻すことができます。 `bt.query_radius(行[[ 'LAT'、 'LNG'] values.ravel()、R = - 。あなたはポイントの1次元配列を通過しなければならないようなドキュメントによれば、見た目

indices = bt.query_radius(df, r=1) 

for idx, row in df.iterrows(): 
    nearest_points_index = indices[idx] 
    res = df.iloc[nearest_points_index] 
    # Do what you want to do with res 
+0

これがなぜ必要なのかについていくつか説明できますか? – monotonic

+0

@monotonic私は説明を追加しました。まだ明確でない場合は、私にお知らせください。 –

+0

@monotonicインデックスの範囲外エラーに関するコメントを追加したことが分かりました。しかし、私がこのページに来たとき、そこにはありません。問題は解決されていますか? –

関連する問題