【转】距离相关系数的python实现

距离相关系数的python实现

觉得有用的话,欢迎一起讨论相互学习~

我的微博我的github我的B站

转载自:https://blog.csdn.net/jiaoaodechunlv/article/details/80655592

最近在做特征选择,要考量几个特征的相关性,想找这个方法的描述,发现很难在网页上搜到。以下为整合的:

[11] 王黎明, 吴香华, 赵天良,等. 基于距离相关系数和支持向量机回归的PM_(2.5)浓度滚动统计预报方案[J]. 环境科学学报, 2017,37(4):1268-1276.(我是从这篇论文上找的,维基百科上有更细致的,可惜我看不下去啊)

下为python程序:

原文:https://gist.github.com/satra/aa3d19a12b74e9ab7941

from scipy.spatial.distance import pdist, squareform
import numpy as np

from numbapro import jit, float32

def distcorr(X, Y):
    """ Compute the distance correlation function
    
    >>> a = [1,2,3,4,5]
    >>> b = np.array([1,2,9,4,4])
    >>> distcorr(a, b)
    0.762676242417
    """
    X = np.atleast_1d(X)
    Y = np.atleast_1d(Y)
    if np.prod(X.shape) == len(X):
        X = X[:, None]
    if np.prod(Y.shape) == len(Y):
        Y = Y[:, None]
    X = np.atleast_2d(X)
    Y = np.atleast_2d(Y)
    n = X.shape[0]
    if Y.shape[0] != X.shape[0]:
        raise ValueError('Number of samples must match')
    a = squareform(pdist(X))
    b = squareform(pdist(Y))
    A = a - a.mean(axis=0)[None, :] - a.mean(axis=1)[:, None] + a.mean()
    B = b - b.mean(axis=0)[None, :] - b.mean(axis=1)[:, None] + b.mean()
    
    dcov2_xy = (A * B).sum()/float(n * n)
    dcov2_xx = (A * A).sum()/float(n * n)
    dcov2_yy = (B * B).sum()/float(n * n)
    dcor = np.sqrt(dcov2_xy)/np.sqrt(np.sqrt(dcov2_xx) * np.sqrt(dcov2_yy))
    return dcor
posted @ 2021-11-06 15:05  WUST许志伟  阅读(762)  评论(0编辑  收藏  举报