msaf/pymf/gmap.py
def update_w(self):
        """ compute new W """

        def select_next(iterval):
            """ select the next best data sample using robust map
            or simply the max iterval ... """

            if self._robust_map:
                k = np.argsort(iterval)[::-1]
                d_sub = self.data[:,k[:self._robust_nselect]]
                self.sub.extend(k[:self._robust_nselect])

                # cluster d_sub
                kmeans_mdl = Kmeans(d_sub, num_bases=self._robust_cluster)
                kmeans_mdl.factorize(niter=10)

                # get largest cluster
                h = np.histogram(kmeans_mdl.assigned, range(self._robust_cluster+1))[0]
                largest_cluster = np.argmax(h)
                sel = pdist(kmeans_mdl.W[:, largest_cluster:largest_cluster+1], d_sub)
                sel = k[np.argmin(sel)]
            else:
                sel = np.argmax(iterval)

            return sel

        EPS = 10**-8

        if scipy.sparse.issparse(self.data):
            norm_data = np.sqrt(self.data.multiply(self.data).sum(axis=0))
            norm_data = np.array(norm_data).reshape((-1))
        else:
            norm_data = np.sqrt(np.sum(self.data**2, axis=0))


        self.select = []

        if self._method == 'pca' or self._method == 'aa':
            iterval = norm_data.copy()

        if self._method == 'nmf':
            iterval = np.sum(self.data, axis=0)/(np.sqrt(self.data.shape[0])*norm_data)
            iterval = 1.0 - iterval

        self.select.append(select_next(iterval))


        for l in range(1, self._num_bases):

            if scipy.sparse.issparse(self.data):
                c = self.data[:, self.select[-1]:self.select[-1]+1].T * self.data
                c = np.array(c.todense())
            else:
                c = np.dot(self.data[:,self.select[-1]], self.data)

            c = c/(norm_data * norm_data[self.select[-1]])

            if self._method == 'pca':
                c = 1.0 - np.abs(c)
                c = c * norm_data

            elif self._method == 'aa':
                c = (c*-1.0 + 1.0)/2.0
                c = c * norm_data

            elif self._method == 'nmf':
                c = 1.0 - np.abs(c)

            ### update the estimated volume
            iterval = c * iterval

            # detect the next best data point
            self.select.append(select_next(iterval))

            self._logger.info('cur_nodes: ' + str(self.select))

        # sort indices, otherwise h5py won't work
        self.W = self.data[:, np.sort(self.select)]

        # "unsort" it again to keep the correct order
        self.W = self.W[:, np.argsort(np.argsort(self.select))]
def update_w(self):
        """ compute new W """

        def select_next(iterval):
            """ select the next best data sample using robust map
            or simply the max iterval ... """

            if self._robust_map:
                k = np.argsort(iterval)[::-1]
                d_sub = self.data[:,k[:self._robust_nselect]]
                self.sub.extend(k[:self._robust_nselect])

                # cluster d_sub
                kmeans_mdl = Kmeans(d_sub, num_bases=self._robust_cluster)
                kmeans_mdl.factorize(niter=10)

                # get largest cluster
                h = np.histogram(kmeans_mdl.assigned, range(self._robust_cluster+1))[0]
                largest_cluster = np.argmax(h)
                sel = pdist(kmeans_mdl.W[:, largest_cluster:largest_cluster+1], d_sub)
                sel = k[np.argmin(sel)]
            else:
                sel = np.argmax(iterval)

            return sel

        EPS = 10**-8

        if scipy.sparse.issparse(self.data):
            norm_data = np.sqrt(self.data.multiply(self.data).sum(axis=0))
            norm_data = np.array(norm_data).reshape((-1))
        else:
            norm_data = np.sqrt(np.sum(self.data**2, axis=0))


        self.select = []

        if self._method == 'pca' or self._method == 'aa':
            iterval = norm_data.copy()

        if self._method == 'nmf':
            iterval = np.sum(self.data, axis=0)/(np.sqrt(self.data.shape[0])*norm_data)
            iterval = 1.0 - iterval

        self.select.append(select_next(iterval))


        for l in range(1, self._num_bases):

            if scipy.sparse.issparse(self.data):
                c = self.data[:, self.select[-1]:self.select[-1]+1].T * self.data
                c = np.array(c.todense())
            else:
                c = np.dot(self.data[:,self.select[-1]], self.data)

            c = c/(norm_data * norm_data[self.select[-1]])

            if self._method == 'pca':
                c = 1.0 - np.abs(c)
                c = c * norm_data

            elif self._method == 'aa':
                c = (c*-1.0 + 1.0)/2.0
                c = c * norm_data

            elif self._method == 'nmf':
                c = 1.0 - np.abs(c)

            ### update the estimated volume
            iterval = c * iterval

            # detect the next best data point
            self.select.append(select_next(iterval))

            self._logger.info('cur_nodes: ' + str(self.select))

        # sort indices, otherwise h5py won't work
        self.W = self.data[:, np.sort(self.select)]

        # "unsort" it again to keep the correct order
        self.W = self.W[:, np.argsort(np.argsort(self.select))]
def update_w ( self ) : def select_next ( iterval ) : """ select the next best data sample using robust map
            or simply the max iterval ... """ if self . _robust_map : k = np . argsort ( iterval ) [ : : - 1 ] d_sub = self . data [ : , k [ : self . _robust_nselect ] ] self . sub . extend ( k [ : self . _robust_nselect ] ) # cluster d_sub kmeans_mdl = Kmeans ( d_sub , num_bases = self . _robust_cluster ) kmeans_mdl . factorize ( niter = 10 ) # get largest cluster h = np . histogram ( kmeans_mdl . assigned , range ( self . _robust_cluster + 1 ) ) [ 0 ] largest_cluster = np . argmax ( h ) sel = pdist ( kmeans_mdl . W [ : , largest_cluster : largest_cluster + 1 ] , d_sub ) sel = k [ np . argmin ( sel ) ] else : sel = np . argmax ( iterval ) return sel EPS = 10 ** - 8 if scipy . sparse . issparse ( self . data ) : norm_data = np . sqrt ( self . data . multiply ( self . data ) . sum ( axis = 0 ) ) norm_data = np . array ( norm_data ) . reshape ( ( - 1 ) ) else : norm_data = np . sqrt ( np . sum ( self . data ** 2 , axis = 0 ) ) self . select = [ ] if self . _method == 'pca' or self . _method == 'aa' : iterval = norm_data . copy ( ) if self . _method == 'nmf' : iterval = np . sum ( self . data , axis = 0 ) / ( np . sqrt ( self . data . shape [ 0 ] ) * norm_data ) iterval = 1.0 - iterval self . select . append ( select_next ( iterval ) ) for l in range ( 1 , self . _num_bases ) : if scipy . sparse . issparse ( self . data ) : c = self . data [ : , self . select [ - 1 ] : self . select [ - 1 ] + 1 ] . T * self . data c = np . array ( c . todense ( ) ) else : c = np . dot ( self . data [ : , self . select [ - 1 ] ] , self . data ) c = c / ( norm_data * norm_data [ self . select [ - 1 ] ] ) if self . _method == 'pca' : c = 1.0 - np . abs ( c ) c = c * norm_data elif self . _method == 'aa' : c = ( c * - 1.0 + 1.0 ) / 2.0 c = c * norm_data elif self . _method == 'nmf' : c = 1.0 - np . abs ( c ) ### update the estimated volume iterval = c * iterval # detect the next best data point self . select . append ( select_next ( iterval ) ) self . _logger . info ( 'cur_nodes: ' + str ( self . select ) ) # sort indices, otherwise h5py won't work self . W = self . data [ : , np . sort ( self . select ) ] # "unsort" it again to keep the correct order self . W = self . W [ : , np . argsort ( np . argsort ( self . select ) ) ]
compute new W
compute new W