cluster.py 文件源码

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

项目:PyMaid 作者: schlegelp 项目源码 文件源码
def cluster(self, method='ward'):
        """ Cluster distance matrix. This will automatically be called when
        attribute linkage is requested for the first time.

        Parameters
        ----------
        method :    str, optional
                    Clustering method (see scipy.cluster.hierarchy.linkage 
                    for reference)
        """

        # First, convert similarity matrix to distance matrix
        if self.mat_type != 'distance':            
            if isinstance( self.mat, pd.DataFrame ):                
                self.dist_mat = ( self.mat.as_matrix() - self.mat.max().max() ) * -1
            else:
                self.dist_mat = ( self.mat - self.mat.max() ) * -1
        else:
            if isinstance( self.mat, pd.DataFrame ):
                self.dist_mat = self.mat.as_matrix()
            else:
                self.dist_mat = self.mat

        # Second, convert into condensed distance matrix - otherwise clustering
        # thinks we are passing observations instead of final scores
        self.condensed_dist_mat = scipy.spatial.distance.squareform( self.dist_mat, checks=False )

        self.linkage = scipy.cluster.hierarchy.linkage(self.condensed_dist_mat, method=method)

        # Save method in case we want to look it up later
        self.method = method

        module_logger.info('Clustering done using method "{0}"'.format(method) )
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号