11

DataFrame でのPandasペアワイズ相関は、多くの場合に役立ちます。ただし、私の特定のケースでは、パンダによって提供されていないメソッド ((pearson、kendall、または spearman) 以外のものを使用して、2 つの列を相関させたいと考えています。この場合に使用する相関関数を明示的に定義することは可能ですか?

私が望む構文は次のようになります。

def my_method(x,y): return something
frame.corr(method=my_method)
4

2 に答える 2

1

あらゆる種類のパフォーマンスのためにcythonでこれを行う必要があります(cythonizable関数を使用)

l = len(df.columns)
results = np.zeros((l,l))
for i, ac in enumerate(df):
    for j, bc in enumerate(df):
           results[j,i] = func(ac,bc)
results = DataFrame(results,index=df.columns,columns=df.columns)
于 2013-08-14T14:47:39.917 に答える
0

DataFrame.corr() のドキュメントを確認してください

Parameters
----------
    method : {'pearson', 'kendall', 'spearman'} or callable
        * pearson : standard correlation coefficient
        * kendall : Kendall Tau correlation coefficient
        * spearman : Spearman rank correlation
        * callable: callable with input two 1d ndarrays
            and returning a float. Note that the returned matrix from corr
            will have 1 along the diagonals and will be symmetric
            regardless of the callable's behavior
            .. versionadded:: 0.24.0

DataFrame.corrwith() もチェックしてください

警告: これは対称相関行列を計算します。CramrsV ですが、この方法は TheilsU やその他の非対称相関行列には適していません。

于 2019-12-29T18:30:49.753 に答える