示例#1
0
def calcHrespForMergePairs(resp, Data, mPairIDs, returnVec=1):
    ''' Calculate resp entropy terms for all candidate merge pairs

    Returns
    ---------
    Hresp : 2D array, size K x K
    or 
    Hresp : 1D array, size M
        where each entry corresponds to one merge pair in mPairIDs
    '''
    if hasattr(Data, 'word_count') and resp.shape[0] == Data.word_count.size:
        if mPairIDs is None:
            Hmat = -1 * calcRlogRdotv_allpairs(resp, Data.word_count)
        else:
            Hmat = -1 * calcRlogRdotv_specificpairs(
                resp, Data.word_count, mPairIDs)
    else:
        if mPairIDs is None:
            Hmat = -1 * calcRlogR_allpairs(resp)
        else:
            Hmat = -1 * calcRlogR_specificpairs(resp, mPairIDs)
    if returnVec:
        if Hmat.ndim == 2:
            Hvec = np.zeros(len(mPairIDs))
            for ii, (kA, kB) in enumerate(mPairIDs):
                Hvec[ii] = Hmat[kA, kB]
        else:
            Hvec = Hmat
        return Hvec
    else:
        return Hmat
示例#2
0
def calcHrespForSpecificMergePairs(LP, Data, mPairIDs):
    ''' Calculate resp entropy terms for all candidate merge pairs

    Returns
    ---------
    Hresp : 1D array, size M
        where each entry corresponds to one merge pair in mPairIDs
    '''
    assert mPairIDs is not None
    if 'resp' in LP:
        N = LP['resp'].shape[0]
        if hasattr(Data, 'word_count') and N == Data.word_count.size:
            m_Hresp = -1 * calcRlogRdotv_specificpairs(
                LP['resp'], Data.word_count, mPairIDs)
        else:
            m_Hresp = -1 * calcRlogR_specificpairs(LP['resp'], mPairIDs)
    else:
        if LP['nnzPerRow'] == 1:
            return None
        N = LP['spR'].shape[0]
        if hasattr(Data, 'word_count') and N == Data.word_count.size:
            m_Hresp = calcSparseMergeRlogRdotv(
                spR_csr=LP['spR'],
                nnzPerRow=LP['nnzPerRow'],
                v=Data.word_count,
                mPairIDs=mPairIDs)
        else:
            m_Hresp = calcSparseMergeRlogR(
                spR_csr=LP['spR'],
                nnzPerRow=LP['nnzPerRow'],
                mPairIDs=mPairIDs)
    assert m_Hresp.size == len(mPairIDs)
    return m_Hresp
示例#3
0
    def calcElogqZForMergePairs(self, resp, Data, mPairIDs):
        ''' Calculate resp entropy terms for all candidate merge pairs

        Returns
        ---------
        ElogqZ : 2D array, size K x K
    '''
        if hasattr(Data, 'word_count'):
            if mPairIDs is None:
                ElogqZMat = calcRlogRdotv_allpairs(resp, Data.word_count)
            else:
                ElogqZMat = calcRlogRdotv_specificpairs(
                    resp, Data.word_count, mPairIDs)
        else:
            if mPairIDs is None:
                ElogqZMat = calcRlogR_allpairs(resp)
            else:
                ElogqZMat = calcRlogR_specificpairs(resp, mPairIDs)
        return ElogqZMat