GapAnalysis.py
def fit(self, X):
        """"Fit clustering_method to original data and to noise samples."""
        rgen = np.random.RandomState(self.random_state)
        self.wk_ = np.zeros(self.max_clusters)
        self.wk_null_ = np.zeros(self.max_clusters)
        self.wk_null_stdev_ = np.zeros(self.max_clusters)
        self.gap_ = np.zeros(self.max_clusters)
        self.gap_stdev_ = np.zeros(self.max_clusters)

        # Generate results for original dataset
        for idx, n in enumerate(range(1,self.max_clusters+1)):
            if self.cov_type is not None:
                cm = self.cluster_method(n_components=n,
                                         covariance_type=self.cov_type)
                cm.fit(X)
                self.wk_[idx] = cm.bic(X)
            else:
                cm = self.cluster_method(n_clusters=n)
                cm.fit(X)
                self.wk_[idx] = np.log(cm.inertia_)


        # Generate results for noise samples
        for idx, n in enumerate(range(1,self.max_clusters+1)):
            temp_wk = np.zeros(self.num_samples)
            for i in range(self.num_samples):
                if self.cov_type is not None:
                    cm = self.cluster_method(n_components=n,
                                             covariance_type=self.cov_type)
                    X_sample = self.generate_sample(X, rgen)
                    cm.fit(X_sample)
                    temp_wk[i] = cm.bic(X_sample)
                else:
                    cm = self.cluster_method(n_clusters=n)
                    cm.fit(self.generate_sample(X, rgen))
                    temp_wk[i] = cm.inertia_


            if self.cov_type is not None:
                self.wk_null_[idx] = np.mean(temp_wk)
                self.wk_null_stdev_[idx] = np.std(temp_wk)
                temp_gap = temp_wk - self.wk_[idx]
            else:
                self.wk_null_[idx] = np.mean(np.log(temp_wk))
                self.wk_null_stdev_[idx] = np.std(np.log(temp_wk))
                temp_gap = np.log(temp_wk) - self.wk_[idx]
            self.gap_[idx] = np.mean(temp_gap)
            self.gap_stdev_[idx] = (np.std(temp_gap) *
                                    np.sqrt(1+(1/self.num_samples)))
Fit clustering_method to original data and to noise samples.
Fit clustering_method to original data and to noise samples.
Fit clustering_method to original data and to noise samples .
def fit(self, X):
        rgen = np.random.RandomState(self.random_state)
        self.wk_ = np.zeros(self.max_clusters)
        self.wk_null_ = np.zeros(self.max_clusters)
        self.wk_null_stdev_ = np.zeros(self.max_clusters)
        self.gap_ = np.zeros(self.max_clusters)
        self.gap_stdev_ = np.zeros(self.max_clusters)
        for idx, n in enumerate(range(1,self.max_clusters+1)):
            if self.cov_type is not None:
                cm = self.cluster_method(n_components=n,
                                         covariance_type=self.cov_type)
                cm.fit(X)
                self.wk_[idx] = cm.bic(X)
            else:
                cm = self.cluster_method(n_clusters=n)
                cm.fit(X)
                self.wk_[idx] = np.log(cm.inertia_)
        for idx, n in enumerate(range(1,self.max_clusters+1)):
            temp_wk = np.zeros(self.num_samples)
            for i in range(self.num_samples):
                if self.cov_type is not None:
                    cm = self.cluster_method(n_components=n,
                                             covariance_type=self.cov_type)
                    X_sample = self.generate_sample(X, rgen)
                    cm.fit(X_sample)
                    temp_wk[i] = cm.bic(X_sample)
                else:
                    cm = self.cluster_method(n_clusters=n)
                    cm.fit(self.generate_sample(X, rgen))
                    temp_wk[i] = cm.inertia_
            if self.cov_type is not None:
                self.wk_null_[idx] = np.mean(temp_wk)
                self.wk_null_stdev_[idx] = np.std(temp_wk)
                temp_gap = temp_wk - self.wk_[idx]
            else:
                self.wk_null_[idx] = np.mean(np.log(temp_wk))
                self.wk_null_stdev_[idx] = np.std(np.log(temp_wk))
                temp_gap = np.log(temp_wk) - self.wk_[idx]
            self.gap_[idx] = np.mean(temp_gap)
            self.gap_stdev_[idx] = (np.std(temp_gap) *
                                    np.sqrt(1+(1/self.num_samples)))
def fit ( self , X ) : rgen = np . random . RandomState ( self . random_state ) self . wk_ = np . zeros ( self . max_clusters ) self . wk_null_ = np . zeros ( self . max_clusters ) self . wk_null_stdev_ = np . zeros ( self . max_clusters ) self . gap_ = np . zeros ( self . max_clusters ) self . gap_stdev_ = np . zeros ( self . max_clusters ) for idx , n in enumerate ( range ( 1 , self . max_clusters + 1 ) ) : if self . cov_type is not None : cm = self . cluster_method ( n_components = n , covariance_type = self . cov_type ) cm . fit ( X ) self . wk_ [ idx ] = cm . bic ( X ) else : cm = self . cluster_method ( n_clusters = n ) cm . fit ( X ) self . wk_ [ idx ] = np . log ( cm . inertia_ ) for idx , n in enumerate ( range ( 1 , self . max_clusters + 1 ) ) : temp_wk = np . zeros ( self . num_samples ) for i in range ( self . num_samples ) : if self . cov_type is not None : cm = self . cluster_method ( n_components = n , covariance_type = self . cov_type ) X_sample = self . generate_sample ( X , rgen ) cm . fit ( X_sample ) temp_wk [ i ] = cm . bic ( X_sample ) else : cm = self . cluster_method ( n_clusters = n ) cm . fit ( self . generate_sample ( X , rgen ) ) temp_wk [ i ] = cm . inertia_ if self . cov_type is not None : self . wk_null_ [ idx ] = np . mean ( temp_wk ) self . wk_null_stdev_ [ idx ] = np . std ( temp_wk ) temp_gap = temp_wk - self . wk_ [ idx ] else : self . wk_null_ [ idx ] = np . mean ( np . log ( temp_wk ) ) self . wk_null_stdev_ [ idx ] = np . std ( np . log ( temp_wk ) ) temp_gap = np . log ( temp_wk ) - self . wk_ [ idx ] self . gap_ [ idx ] = np . mean ( temp_gap ) self . gap_stdev_ [ idx ] = ( np . std ( temp_gap ) * np . sqrt ( 1 + ( 1 / self . num_samples ) ) )
Fit clustering_method to original data and to noise samples.
Fit clustering_method to original data and to noise samples .
""""Fit clustering_method to original data and to noise samples.""" # Generate results for original dataset
 # Generate results for noise samples

self
X