Esempio n. 1
0
def _distSpearmanW_MA(x, y, w):
    """if any of x,y,w is a MA array containing masked values
    """
    x = MA.asarray(x)
    y = MA.asarray(y)
    w = MA.asarray(w)
    assert MA.rank(x) == MA.rank(y) == MA.rank(w) == 1
    cond = MA.logical_not(
        MA.logical_or(MA.logical_or(MA.getmaskarray(x), MA.getmaskarray(y)),
                      MA.getmaskarray(w)))
    # with MA use compress before tolist() !
    rankx = Numeric.array(statc.rankdata(MA.compress(cond, x).tolist()))
    ranky = Numeric.array(statc.rankdata(MA.compress(cond, y).tolist()))
    return distPearsonW(rankx, ranky, MA.compress(cond, w))
Esempio n. 2
0
def distEuclidean(x, y):
    """normalized euclidean distance
    """
    x = MA.asarray(x)
    y = MA.asarray(y)
    assert MA.rank(x) == MA.rank(y) == 1
    sumWeights = MA.add.reduce(
        MA.logical_not(MA.logical_or(
            MA.getmaskarray(x), MA.getmaskarray(y))).astype(Numeric.Float))
    return MA.sqrt(MA.add.reduce((x - y)**2) / sumWeights)
Esempio n. 3
0
def distManhattan(x, y):
    """normalized Manhattan distance
    """
    x = MA.asarray(x)
    y = MA.asarray(y)
    assert MA.rank(x) == MA.rank(y) == 1
    sumWeights = MA.add.reduce(
        MA.logical_not(MA.logical_or(
            MA.getmaskarray(x), MA.getmaskarray(y))).astype(Numeric.Float))
    return MA.add.reduce(MA.absolute(x - y)) / sumWeights
Esempio n. 4
0
def distSpearman(x, y):
    """distance corresponding to 1 - spearman's correlation coefficient for arrays x,y
    returns distance: 1 - spearman_r
    """
    x = MA.asarray(x)
    y = MA.asarray(y)
    assert MA.rank(x) == MA.rank(y) == 1
    cond = MA.logical_not(MA.logical_or(MA.getmaskarray(x),
                                        MA.getmaskarray(y)))
    return 1 - statc.spearmanr(
        MA.compress(cond, x).tolist(),
        MA.compress(cond, y).tolist())[0]
Esempio n. 5
0
def distPearsonW(x, y, w):
    """weighted distance corresponding to 1 - pearson's correlation coefficient for arrays x,y and weights w
    returns distance: 1 - pearson_r
    """
    #TINY = 1.0e-20
    # ones for non-masked places at x,y and w
    x = MA.asarray(x)
    y = MA.asarray(y)
    w = MA.asarray(w)
    assert MA.rank(x) == MA.rank(y) == MA.rank(w) == 1
    mask = MA.logical_or(MA.logical_or(MA.getmaskarray(x), MA.getmaskarray(y)),
                         MA.getmaskarray(w))
    # set mask to w that is equal to the mask from x, y and w
    w = MA.masked_array(w, mask=mask)
    n_w_mean = MA.add.reduce(w)  # n * mean(w)
    x_w = x * w  # x * w
    y_w = y * w  # y * w
    x_wmean = MA.divide(MA.add.reduce(x_w), n_w_mean)  # weighted_mean(x)
    y_wmean = MA.divide(MA.add.reduce(y_w), n_w_mean)  # weighted_mean(x)
    r_num = MA.add.reduce(x * y * w) - n_w_mean * x_wmean * y_wmean
    r_den = MA.sqrt((MA.add.reduce(x_w * x) - n_w_mean * x_wmean**2) *
                    (MA.add.reduce(y_w * y) - n_w_mean * y_wmean**2))
    return 1 - MA.divide(r_num, r_den)