質問

DBSCAN(Scikit Learn Implementation)と位置データを使用してクラスタしようとしています。私のデータはNPアレイ形式ですが、DBSCANをHAVERSINE式で使用するには、距離行列を作成する必要があります。これを実行しようとすると、次のエラーが発生しました(モジュールの「呼び出し可能なエラー」)オンラインで読んでいるものから、これはインポートエラーですが、私は私の場合ではないと確信しています。私は自分のhaversineの距離数式を作成しましたが、私はエラーがこれではないと確信しています。

これは私の入力データ、NPアレイ(resultArray)です。

[[ 53.3252628   -6.2644198 ]
[ 53.3287395   -6.2646543 ]
[ 53.33321202  -6.24785807]
[ 53.3261015   -6.2598324 ]
[ 53.325291    -6.2644105 ]
[ 53.3281323   -6.2661467 ]
[ 53.3253074   -6.2644483 ]
[ 53.3388147   -6.2338417 ]
[ 53.3381102   -6.2343826 ]
[ 53.3253074   -6.2644483 ]
[ 53.3228188   -6.2625379 ]
[ 53.3253074   -6.2644483 ]]
.

で、エラーが発生しているコードの行です。

distance_matrix = sp.spatial.distance.squareform(sp.spatial.distance.pdist
(ResultArray,(lambda u,v: haversine(u,v))))
.

これはエラーメッセージです:

File "Location.py", line 48, in <module>
distance_matrix = sp.spatial.distance.squareform(sp.spatial.distance.pdist
(ResArray,(lambda u,v: haversine(u,v))))
File "/usr/lib/python2.7/dist-packages/scipy/spatial/distance.py", line 1118, in pdist
dm[k] = dfun(X[i], X[j])
File "Location.py", line 48, in <lambda>
distance_matrix = sp.spatial.distance.squareform(sp.spatial.distance.pdist
(ResArray,(lambda u,v: haversine(u,v))))
TypeError: 'module' object is not callable
.

SCIPYをSPとしてインポートします。(SPとしてのインポートSCIPY)

役に立ちましたか?

解決

@Tommasof回答を参照してください。この回答は間違っています.pdistカスタム距離関数を選択できます。正しい答えとして選択されていなくても答えを削除します。

scipypdistでは、カスタム距離関数を渡すことができません。 docs あなたはいくつかのオプションを持っていますが、ハーバーサイドの距離はサポートされているメトリックのリスト内ではありません。

(MATLAB pdistは、このオプションをサポートしていますが、ここでの

計算を「手動」、すなわちループで行う必要があります。このようなものは機能します。

from numpy import array,zeros

def haversine(lon1, lat1, lon2, lat2):
    """  See the link below for a possible implementation """
    pass

#example input (your's, truncated)
ResultArray = array([[ 53.3252628, -6.2644198 ],
                     [ 53.3287395  , -6.2646543 ],
                     [ 53.33321202 , -6.24785807],
                     [ 53.3253074  , -6.2644483 ]])

N = ResultArray.shape[0]
distance_matrix = zeros((N, N))
for i in xrange(N):
    for j in xrange(N):
        lati, loni = ResultArray[i]
        latj, lonj = ResultArray[j]
        distance_matrix[i, j] = haversine(loni, lati, lonj, latj)
        distance_matrix[j, i] = distance_matrix[i, j]

print distance_matrix
[[ 0.          0.38666203  1.41010971  0.00530489]
 [ 0.38666203  0.          1.22043364  0.38163748]
 [ 1.41010971  1.22043364  0.          1.40848782]
 [ 0.00530489  0.38163748  1.40848782  0.        ]]
.

参照のために、HaversideのPythonの実装が見つかりますこちら

他のヒント

SCIPYを使用すると、このリンクと便宜上報告されています:

Y = pdist(X, f)
.
Computes the distance between all pairs of vectors in X using the user supplied 2-arity function f. For example, Euclidean distance between the vectors could be computed as follows:

dm = pdist(X, lambda u, v: np.sqrt(((u-v)**2).sum()))
.

ここでは、このコードのマイバージョンはこのコードからインスパイアされたコードにインスパイアされています。リンク:

from numpy import sin,cos,arctan2,sqrt,pi # import from numpy
# earth's mean radius = 6,371km
EARTHRADIUS = 6371.0

def getDistanceByHaversine(loc1, loc2):
    '''Haversine formula - give coordinates as a 2D numpy array of
    (lat_denter link description hereecimal,lon_decimal) pairs'''
    #      
    # "unpack" our numpy array, this extracts column wise arrays
    lat1 = loc1[1]
    lon1 = loc1[0]
    lat2 = loc2[1]
    lon2 = loc2[0]
    #
    # convert to radians ##### Completely identical
    lon1 = lon1 * pi / 180.0
    lon2 = lon2 * pi / 180.0
    lat1 = lat1 * pi / 180.0
    lat2 = lat2 * pi / 180.0
    #
    # haversine formula #### Same, but atan2 named arctan2 in numpy
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2.0))**2
    c = 2.0 * arctan2(sqrt(a), sqrt(1.0-a))
    km = EARTHRADIUS * c
    return km
.

と次の方法で呼び出す:

D = spatial.distance.pdist(A, lambda u, v: getDistanceByHaversine(u,v))
.

マトリックスAは、マトリックスAは経度値と2列目として緯度値が10進数で表されている。

SCIPYを使用して距離行列を事前計算せずにScikit-LearnのDBSCANおよびHAVERSINEメトリックを使用して空間緯度経度データをクラスター化できます。

db = DBSCAN(eps=2/6371., min_samples=5, algorithm='ball_tree', metric='haversine').fit(np.radians(coordinates))
.

これは、このチュートリアルから来ています Scikit-Learn DBSCAN を使用した空間データのクラスタリング。特に、epsの値が2kmでは6371(kmの地球の半径)で割ってラジアンに変換することに注意してください。また、.fit()は、ハワビリスメトリックのラジアン単位で座標を取ります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top