2017-03-16 4 views
0

私はPearson相関メトリックでKNNを使用しようとしている行列Xを持っています。ピアソン相関をSklearnメトリックとして使用することは可能ですか?私はpearson_calc機能が間違っていると仮定していますskearnのピアソン相関メトリックを使用することは可能ですか?

pearson_affinity() takes 1 positional argument but 2 were given

:私は次のエラーを取得するようしかし、これは動作しません

def pearson_calc(M): 
    P = (1 - np.array([[pearsonr(a,b)[0] for a in M] for b in M])) 
    return P 

nbrs = NearestNeighbors(n_neighbors=4, metric=pearson_calc) 
nbrs.fit(X) 
knbrs = nbrs.kneighbors(X) 

:私はこのような何かを試してみました。多分、それはa、bパラメ​​ータを必要とし、マトリックスではないでしょう。ここで

答えて

1

は、問題のドキュメントです:

If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them.

はまた、メトリックの有効な値は次のとおりです。

from scikit-learn:

[‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’,‘manhattan’]

from scipy.spatial.distance:

[‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’]

2つのこと:

  • あなたの関数は2を取る必要があります引数(メトリック(距離)を計算するための2つの行。帽子には2つの引数が渡されていました。

  • これを行うことで、scipy.spatial.distance.correlationをメトリックとして使用できます。

    from scipy.spatial.distance import correlation 
    nbrs = NearestNeighbors(n_neighbors=4, metric='correlation') 
    

    ` ソース:sklearn NearestNeighbors

関連する問題