Beispiel #1
0
    def fit_local(self, X, Y=None):
        """Fitting and generating the local space.
    
        Parameters
        ----------
        X : matrix of shape = [n_samples, n_features]
        (i.e., the feature matrix)

        Y : matrix of shape = [n_samples, n_outputs]
        (i.e., the label/output matrix)
        """
        
        if self.method == 'rf':
            local = RandomForestRegressor(n_estimators=self.n_est,max_features='sqrt',max_depth=None, min_samples_leaf=self.stop_crit,random_state=0)
            print("Basic model: Random Forest \n")
        else:
            local = ExtraTreesRegressor(n_estimators=self.n_est,max_features='sqrt',max_depth=None, min_samples_leaf=self.stop_crit,random_state=0)
            print("Basic model: Extremely Randomized Trees \n")

        if Y is None:
            local.fit(X,X)
            print("Unsupervised learning \n")
        else:
            local.fit(X,Y)
            print("Supervised learning \n")
            
        treepath = local.decision_path(X)[0]
        w = treepath.sum(0)
        wlog = np.log(w.astype(float))+0.00001            
        local.cw =  np.power(wlog,-1)            
        treepath = treepath.multiply(local.cw).toarray().astype(float)
#        treepath = treepath.toarray().astype(float)
        local.ind = np.where(w<(X.shape[0]*self.dw))[1]
#        treepath = np.delete(treepath,local.ind,axis=1)
        treepath = treepath[:,local.ind]
        
        local.pca = PCA(self.dim)
        local.treepath = local.pca.fit_transform(treepath)
        
        return local