Table.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:LTTL 作者: axanthos 项目源码 文件源码
def to_association_matrix(self, bias='none', progress_callback=None):
        """Return a table with Markov associativities between columns
        (cf. Bavaud & Xanthos 2005, Deneulin et al. 2014)
        """
        freq = self.to_numpy()
        total_freq = freq.sum()
        sum_col = freq.sum(axis=0)
        sum_row = freq.sum(axis=1)
        exchange = np.dot(
            np.transpose(freq),
            np.dot(
                np.diag(1 / sum_row),
                freq
            )
        ) / total_freq
        if bias == 'frequent':
            output_matrix = exchange
        elif bias == 'none':
            sqrt_pi_inv = np.diag(1 / np.sqrt(sum_col / total_freq))
            output_matrix = np.dot(sqrt_pi_inv, np.dot(exchange, sqrt_pi_inv))
        else:
            pi_inv = np.diag(1 / (sum_col / total_freq))
            output_matrix = np.dot(pi_inv, np.dot(exchange, pi_inv))
        col_ids = self.col_ids
        values = dict()
        for col_id_idx1 in xrange(len(col_ids)):
            col_id1 = col_ids[col_id_idx1]
            values.update(
                dict(
                    (
                        (col_id1, col_ids[i]),
                        output_matrix[col_id_idx1, i]
                    )
                    for i in xrange(len(col_ids))
                )
            )
            if progress_callback:
                progress_callback()
        new_header_row_id = (
            self.header_row_id[:-2]
            + "2"
            + self.header_row_id[-2:]
        )
        return (
            PivotCrosstab(
                self.col_ids[:],
                self.col_ids[:],
                values,
                new_header_row_id,
                self.header_row_type,
                self.header_row_id,
                self.header_row_type,
                col_type=self.col_type.copy(),
            )
        )
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号