当前位置: 首页>>代码示例>>Python>>正文


Python KDTree.two_point_correlation方法代码示例

本文整理汇总了Python中sklearn.neighbors.KDTree.two_point_correlation方法的典型用法代码示例。如果您正苦于以下问题:Python KDTree.two_point_correlation方法的具体用法?Python KDTree.two_point_correlation怎么用?Python KDTree.two_point_correlation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.neighbors.KDTree的用法示例。


在下文中一共展示了KDTree.two_point_correlation方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: two_point

# 需要导入模块: from sklearn.neighbors import KDTree [as 别名]
# 或者: from sklearn.neighbors.KDTree import two_point_correlation [as 别名]
def two_point(data, bins):
    """Two-point correlation function, using Landy-Szalay method

    Parameters
    ----------
    data : array_like
        input data, shape = [n_samples, n_features] (2D ndarray)
    bins : array_like
        bins within which to compute the 2-point correlation.
        shape = Nbins + 1 (1D ndarray)

    Returns
    -------
    corr : ndarray
        the estimate of the correlation function within each bin
        shape = Nbins
    """
    data = np.asarray(data)
    bins = np.asarray(bins)
    rng = check_random_state(None)

    n_samples, n_features = data.shape
    Nbins = len(bins) - 1

    # shuffle around an axis, making background dist.
    data_R = data.copy()
    for i in range(n_features - 1):
        rng.shuffle(data_R[:, i])

    factor = len(data_R) * 1. / len(data)

    # Fast two-point correlation functions added in scikit-learn v. 0.14
    # Makes tree to embed pairwise distances, increasing look-up speed
    KDT_D = KDTree(data)  # actual distances
    KDT_R = KDTree(data_R)  # randomized background distances

    counts_DD = KDT_D.two_point_correlation(data, bins)  # number of points within bins[i] radius
    counts_RR = KDT_R.two_point_correlation(data_R, bins)  # " " for randomized background


    DD = np.diff(counts_DD)  # number of points in a disc from bins[i-1] to bins[i]
    RR = np.diff(counts_RR)  # " " for randomized background

    # make zeros 1 for numerical stability (finite difference problems)
    RR_zero = (RR == 0)  # mask creation
    RR[RR_zero] = 1  # apply update


    counts_DR = KDT_R.two_point_correlation(data, bins)  # cross-correlation betw. actual and random

    DR = np.diff(counts_DR)  # binned cross-corr

    corr = (factor ** 2 * DD - 2 * factor * DR + RR) / RR  # the Landy-Szalay formula

    corr[RR_zero] = np.nan  # back-apply the zeros found in RR

    return corr
开发者ID:tbsexton,项目名称:MatSim,代码行数:59,代码来源:analysis.py

示例2: two_point

# 需要导入模块: from sklearn.neighbors import KDTree [as 别名]
# 或者: from sklearn.neighbors.KDTree import two_point_correlation [as 别名]
def two_point(data, bins, method='standard',
              data_R=None, random_state=None):
    """Two-point correlation function

    Parameters
    ----------
    data : array_like
        input data, shape = [n_samples, n_features]
    bins : array_like
        bins within which to compute the 2-point correlation.
        shape = Nbins + 1
    method : string
        "standard" or "landy-szalay".
    data_R : array_like (optional)
        if specified, use this as the random comparison sample
    random_state : integer, np.random.RandomState, or None
        specify the random state to use for generating background

    Returns
    -------
    corr : ndarray
        the estimate of the correlation function within each bin
        shape = Nbins
    """
    data = np.asarray(data)
    bins = np.asarray(bins)
    rng = check_random_state(random_state)

    if method not in ['standard', 'landy-szalay']:
        raise ValueError("method must be 'standard' or 'landy-szalay'")

    if bins.ndim != 1:
        raise ValueError("bins must be a 1D array")

    if data.ndim == 1:
        data = data[:, np.newaxis]
    elif data.ndim != 2:
        raise ValueError("data should be 1D or 2D")

    n_samples, n_features = data.shape
    Nbins = len(bins) - 1

    # shuffle all but one axis to get background distribution
    if data_R is None:
        data_R = data.copy()
        for i in range(n_features - 1):
            rng.shuffle(data_R[:, i])
    else:
        data_R = np.asarray(data_R)
        if (data_R.ndim != 2) or (data_R.shape[-1] != n_features):
            raise ValueError('data_R must have same n_features as data')

    factor = len(data_R) * 1. / len(data)

    if sklearn_has_two_point:
        # Fast two-point correlation functions added in scikit-learn v. 0.14
        KDT_D = KDTree(data)
        KDT_R = KDTree(data_R)

        counts_DD = KDT_D.two_point_correlation(data, bins)
        counts_RR = KDT_R.two_point_correlation(data_R, bins)

    else:
        warnings.warn("Version 0.3 of astroML will require scikit-learn "
                      "version 0.14 or higher for correlation function "
                      "calculations. Upgrade to sklearn 0.14+ now for much "
                      "faster correlation function calculations.")

        BT_D = BallTree(data)
        BT_R = BallTree(data_R)

        counts_DD = np.zeros(Nbins + 1)
        counts_RR = np.zeros(Nbins + 1)

        for i in range(Nbins + 1):
            counts_DD[i] = np.sum(BT_D.query_radius(data, bins[i],
                                                    count_only=True))
            counts_RR[i] = np.sum(BT_R.query_radius(data_R, bins[i],
                                                    count_only=True))

    DD = np.diff(counts_DD)
    RR = np.diff(counts_RR)

    # check for zero in the denominator
    RR_zero = (RR == 0)
    RR[RR_zero] = 1

    if method == 'standard':
        corr = factor ** 2 * DD / RR - 1
    elif method == 'landy-szalay':
        if sklearn_has_two_point:
            counts_DR = KDT_R.two_point_correlation(data, bins)
        else:
            counts_DR = np.zeros(Nbins + 1)
            for i in range(Nbins + 1):
                counts_DR[i] = np.sum(BT_R.query_radius(data, bins[i],
                                                        count_only=True))
        DR = np.diff(counts_DR)

        corr = (factor ** 2 * DD - 2 * factor * DR + RR) / RR
#.........这里部分代码省略.........
开发者ID:BTY2684,项目名称:astroML,代码行数:103,代码来源:correlation.py

示例3: KDTree

# 需要导入模块: from sklearn.neighbors import KDTree [as 别名]
# 或者: from sklearn.neighbors.KDTree import two_point_correlation [as 别名]
KDT_D = KDTree(Data_D)
KDT_D1 = KDTree(Data_D1)
KDT_R = KDTree(Data_R)
KDT_R1 = KDTree(Data_R1)

Nbins =30
counts_DD1 = np.zeros(Nbins+1)
counts_RR1 = np.zeros(Nbins+1)
counts_DR1 = np.zeros(Nbins+1)
counts_D1R = np.zeros(Nbins+1)
bins = np.arange(0, 30)
print bins

#calculating Two point correlation using Sklearn function

counts_DD1 = KDT_D.two_point_correlation(Data_D, bins)
counts_RR1 = KDT_R.two_point_correlation(Data_R1, bins)
DD1 = np.diff(counts_DD1)
RR1 = np.diff(counts_RR1)
RR1_zero = (RR1 == 0)
RR1[RR1_zero] = 1

#for i in range(Nbins + 1):
#    counts_DR[i] = np.sum(BT_R.query_radius(Data_D, bins[i], count_only=True))

counts_DR1 = KDT_R1.two_point_correlation(Data_D, bins)
counts_D1R = KDT_R.two_point_correlation(Data_D1, bins)

DR1 = np.diff(counts_DR1)
D1R = np.diff(counts_D1R)
开发者ID:Jravis,项目名称:Two_corAndCrossCor,代码行数:32,代码来源:crossCor.py

示例4: len

# 需要导入模块: from sklearn.neighbors import KDTree [as 别名]
# 或者: from sklearn.neighbors.KDTree import two_point_correlation [as 别名]
    factor = len(Data_R1)*(1.0/len(Data_D))
    print factor

    KDT_D = KDTree(Data_D)
    KDT_R = KDTree(Data_R1)

    Nbins =30
    counts_DD = np.zeros(Nbins+1)

    counts_RR = np.zeros(Nbins+1)
    counts_DR = np.zeros(Nbins+1)
    bins = np.arange(0, 30)
    print bins


    counts_DD = KDT_D.two_point_correlation(Data_D, bins)
    #print counts_DD
    counts_RR = KDT_R.two_point_correlation(Data_R1, bins)

    DD = np.diff(counts_DD)
    RR = np.diff(counts_RR)

    RR_zero = (RR == 0)
    RR[RR_zero] = 1

    counts_DR = KDT_R.two_point_correlation(Data_D, bins)

    DR = np.diff(counts_DR)
    corr = (factor**2 * DD - 2 * factor * DR + RR) / RR

    corr[RR_zero] = np.nan
开发者ID:Jravis,项目名称:Two_corAndCrossCor,代码行数:33,代码来源:randSmapl.py


注:本文中的sklearn.neighbors.KDTree.two_point_correlation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。