Exemplo n.º 1
0
    def test_expectedTable(self):
        # CrossTable:
        # [2, 0, 3],
        # [2, 1, 0],

        table = CrossTable(self.X, self.Y)
        tab = table.getExpectedTable()
        answer = [[20 / 8.0, 5 / 8.0, 15 / 8.0], [12 / 8.0, 3 / 8.0, 9 / 8.0]]
        np.testing.assert_array_equal(answer, tab)

        tab = table.getExpectedProbtable()
        answer = [[20 / 64.0, 5 / 64.0, 15 / 64.0],
                  [12 / 64.0, 3 / 64.0, 9 / 64.0]]
        np.testing.assert_array_equal(answer, tab)

        np.testing.assert_array_equal(table.getProbCols(),
                                      [4.0 / 8, 1.0 / 8, 3.0 / 8])
        np.testing.assert_array_equal(table.getProbRows(), [5.0 / 8, 3.0 / 8])
Exemplo n.º 2
0
    def test_expectedTable(self):
        # CrossTable:
        # [2, 0, 3],
        # [2, 1, 0],

        table = CrossTable(self.X, self.Y)
        tab = table.getExpectedTable()
        answer = [
            [20/8.0, 5/8.0, 15/8.0],
            [12/8.0, 3/8.0,  9/8.0]
        ]
        np.testing.assert_array_equal(answer, tab)

        tab = table.getExpectedProbtable()
        answer = [
            [20/64.0, 5/64.0, 15/64.0],
            [12/64.0, 3/64.0,  9/64.0]
        ]
        np.testing.assert_array_equal(answer, tab)

        np.testing.assert_array_equal(table.getProbCols(), [4.0/8, 1.0/8, 3.0/8])
        np.testing.assert_array_equal(table.getProbRows(), [5.0/8, 3.0/8])
Exemplo n.º 3
0
def cramer(X, Y):
    '''
    Define Cramer's relationship coefficient of the rasters for discrete values
    Coefficient change between [0, 1]
    0 - no dependence
    1 - full connection
    @param X    First raster's array
    @param Y    Second raster's array
    '''
    table = CrossTable(X, Y)
    rows, cols = table.shape
    t_expect =  table.getExpectedTable()

    # Mask T* to prevent division by zero
    t_expect = np.ma.array(t_expect, mask=(t_expect == 0))
    # chi-square coeff = sum((T-T*)^2/T*)
    x2 = np.sum(np.square(table.T - t_expect)/t_expect)
    # CRAMER CONTINGENCY COEF. = sqrt(chi-square / (total * min(s-1,r-1)))
    # s, r - raster grauations
    Cramer = math.sqrt(x2/(table.n*min(cols-1, rows-1)))

    return Cramer