(self, X, y=None, features=None, retrain=False)
return self
Fit to the training dataset, storing information that will be needed for the transform dataset.
        Return the Preprocessor object.
        Optionally re-initizialise the object by passing retrain=True, and resending the features dataframe
Fit to the training dataset, storing information that will be needed for the transform dataset.
        Return the Preprocessor object.
        Optionally re-initizialise the object by passing retrain=True, and resending the features dataframe
Fit to the training dataset storing information that will be needed for the transform dataset . Return the Preprocessor object . Optionally re - initizialise the object by passing retrain = True and resending the features dataframe
def fit(self, X, y=None, features=None, retrain=False):
        """
        Fit to the training dataset, storing information that will be needed for the transform dataset.
        Return the Preprocessor object.
        Optionally re-initizialise the object by passing retrain=True, and resending the features dataframe
        """

        # Reinitialize this Preprocessor instance if required
        if retrain:
            if features is None:
                features = self.features
            
            self.__init__(features)
        
        # Set up an empty data frame for data to be scaled
        scale_df = pd.DataFrame()

        ohe_df = None
        hash_df = None
        cv_df = None
        tfidf_df = None
        text_df = None
        
        if self.ohe:
            # Get a subset of the data that requires one hot encoding
            ohe_df = X[self.ohe_meta.index.tolist()]
                
            # Apply one hot encoding to relevant columns
            ohe_df = pd.get_dummies(ohe_df, columns=ohe_df.columns)
            
            # Keep a copy of the OHE dataframe structure so we can align the transform dataset 
            self.ohe_df_structure = pd.DataFrame().reindex_like(ohe_df)
        
        # Scaling needs to be fit exclusively on the training data so as not to influence the results
        if self.scale:
            # Get a subset of the data that requires scaling
            scale_df = X[self.scale_meta.index.tolist()]
                   
        if self.hash:
            # Get a subset of the data that requires feature hashing
            hash_df = X[self.hash_meta.index.tolist()]
            hash_cols = hash_df.columns

            # Hash unique values for each relevant column and then join to a dataframe for hashed data
            for c in hash_cols:
                unique = self.hasher(hash_df, c, self.hash_meta["strategy_args"].loc[c])
                hash_df = hash_df.join(unique, on=c)
                hash_df = hash_df.drop(c, axis=1)

            # If hashed columns need to be scaled, these need to be considered when setting up the scaler as well    
            if self.scale_hashed:
                if self.scale:
                    scale_df = scale_df.join(hash_df)
                else:
                    scale_df = hash_df 
        
        if self.cv:
            # Get a subset of the data that requires count vectorizing
            cv_df = X[self.cv_meta.index.tolist()]
            cv_cols = cv_df.columns

            # Get count vectors for each relevant column and then join to a dataframe for count vectorized data
            for c in cv_cols:
                unique = self.text_vectorizer(cv_df, c, type="count", **self.cv_meta["strategy_args"].loc[c])
                cv_df = cv_df.join(unique, on=c)
                cv_df = cv_df.drop(c, axis=1)

            # Keep a copy of the count vectorized dataframe structure so we can align the transform dataset 
            self.cv_df_structure = pd.DataFrame().reindex_like(cv_df)

            # If text vector columns need to be scaled, these need to be considered when setting up the scaler as well    
            if self.scale_vectors:
                if self.scale or (self.scale_hashed and self.hash):
                    scale_df = scale_df.join(cv_df)
                else:
                    scale_df = cv_df 

        if self.tfidf:
            # Get a subset of the data that requires tfidf vectorizing
            tfidf_df = X[self.tfidf_meta.index.tolist()]
            tfidf_cols = tfidf_df.columns

            # Get tfidf vectors for each relevant column and then join to a dataframe for tfidf vectorized data
            for c in tfidf_cols:
                unique = self.text_vectorizer(tfidf_df, c, type="tfidf", **self.tfidf_meta["strategy_args"].loc[c])
                tfidf_df = tfidf_df.join(unique, on=c)
                tfidf_df = tfidf_df.drop(c, axis=1)

            # Keep a copy of the tfidf vectorized dataframe structure so we can align the transform dataset 
            self.tfidf_df_structure = pd.DataFrame().reindex_like(tfidf_df)
            
            # If text vector columns need to be scaled, these need to be considered when setting up the scaler as well    
            if self.scale_vectors:
                if self.scale or (self.scale_hashed and self.hash) or self.cv:
                    scale_df = scale_df.join(tfidf_df)
                else:
                    scale_df = tfidf_df 
        
        if self.text:
            # Get a subset of the data that requires text similarity OHE
            text_df = X[self.text_meta.index.tolist()]
            text_cols = text_df.columns

            # Get text similarity OHE for each relevant column and then join to a dataframe for text similarity OHE data
            for c in text_cols:
                unique = self.text_similarity(text_df, c)
                text_df = text_df.join(unique, on=c)
                text_df = text_df.drop(c, axis=1)

            # Keep a copy of the text similarity OHE dataframe structure so we can align the transform dataset 
            self.text_df_structure = pd.DataFrame().reindex_like(text_df)

        try:
            if len(scale_df) > 0:
                # Get an instance of the sklearn scaler fit to X
                self.scaler_instance = utils.get_scaler(scale_df, missing=self.missing, scaler=self.scaler, **self.kwargs)

                # Keep a copy of the scaling dataframe structure so we can align the transform dataset 
                self.scale_df_structure = pd.DataFrame().reindex_like(scale_df)
        except AttributeError:
            pass

        # Output information to the terminal and log file if required
        if self.log is not None:
            self._print_log(2, ohe_df=ohe_df, scale_df=scale_df, hash_df=hash_df, cv_df=cv_df, tfidf_df=tfidf_df, text_df=text_df)

        return self
def fit ( self , X , y = None , features = None , retrain = False ) : # Reinitialize this Preprocessor instance if required if retrain : if features is None : features = self . features self . __init__ ( features ) # Set up an empty data frame for data to be scaled scale_df = pd . DataFrame ( ) ohe_df = None hash_df = None cv_df = None tfidf_df = None text_df = None if self . ohe : # Get a subset of the data that requires one hot encoding ohe_df = X [ self . ohe_meta . index . tolist ( ) ] # Apply one hot encoding to relevant columns ohe_df = pd . get_dummies ( ohe_df , columns = ohe_df . columns ) # Keep a copy of the OHE dataframe structure so we can align the transform dataset  self . ohe_df_structure = pd . DataFrame ( ) . reindex_like ( ohe_df ) # Scaling needs to be fit exclusively on the training data so as not to influence the results if self . scale : # Get a subset of the data that requires scaling scale_df = X [ self . scale_meta . index . tolist ( ) ] if self . hash : # Get a subset of the data that requires feature hashing hash_df = X [ self . hash_meta . index . tolist ( ) ] hash_cols = hash_df . columns # Hash unique values for each relevant column and then join to a dataframe for hashed data for c in hash_cols : unique = self . hasher ( hash_df , c , self . hash_meta [ "strategy_args" ] . loc [ c ] ) hash_df = hash_df . join ( unique , on = c ) hash_df = hash_df . drop ( c , axis = 1 ) # If hashed columns need to be scaled, these need to be considered when setting up the scaler as well     if self . scale_hashed : if self . scale : scale_df = scale_df . join ( hash_df ) else : scale_df = hash_df if self . cv : # Get a subset of the data that requires count vectorizing cv_df = X [ self . cv_meta . index . tolist ( ) ] cv_cols = cv_df . columns # Get count vectors for each relevant column and then join to a dataframe for count vectorized data for c in cv_cols : unique = self . text_vectorizer ( cv_df , c , type = "count" , * * self . cv_meta [ "strategy_args" ] . loc [ c ] ) cv_df = cv_df . join ( unique , on = c ) cv_df = cv_df . drop ( c , axis = 1 ) # Keep a copy of the count vectorized dataframe structure so we can align the transform dataset  self . cv_df_structure = pd . DataFrame ( ) . reindex_like ( cv_df ) # If text vector columns need to be scaled, these need to be considered when setting up the scaler as well     if self . scale_vectors : if self . scale or ( self . scale_hashed and self . hash ) : scale_df = scale_df . join ( cv_df ) else : scale_df = cv_df if self . tfidf : # Get a subset of the data that requires tfidf vectorizing tfidf_df = X [ self . tfidf_meta . index . tolist ( ) ] tfidf_cols = tfidf_df . columns # Get tfidf vectors for each relevant column and then join to a dataframe for tfidf vectorized data for c in tfidf_cols : unique = self . text_vectorizer ( tfidf_df , c , type = "tfidf" , * * self . tfidf_meta [ "strategy_args" ] . loc [ c ] ) tfidf_df = tfidf_df . join ( unique , on = c ) tfidf_df = tfidf_df . drop ( c , axis = 1 ) # Keep a copy of the tfidf vectorized dataframe structure so we can align the transform dataset  self . tfidf_df_structure = pd . DataFrame ( ) . reindex_like ( tfidf_df ) # If text vector columns need to be scaled, these need to be considered when setting up the scaler as well     if self . scale_vectors : if self . scale or ( self . scale_hashed and self . hash ) or self . cv : scale_df = scale_df . join ( tfidf_df ) else : scale_df = tfidf_df if self . text : # Get a subset of the data that requires text similarity OHE text_df = X [ self . text_meta . index . tolist ( ) ] text_cols = text_df . columns # Get text similarity OHE for each relevant column and then join to a dataframe for text similarity OHE data for c in text_cols : unique = self . text_similarity ( text_df , c ) text_df = text_df . join ( unique , on = c ) text_df = text_df . drop ( c , axis = 1 ) # Keep a copy of the text similarity OHE dataframe structure so we can align the transform dataset  self . text_df_structure = pd . DataFrame ( ) . reindex_like ( text_df ) try : if len ( scale_df ) > 0 : # Get an instance of the sklearn scaler fit to X self . scaler_instance = utils . get_scaler ( scale_df , missing = self . missing , scaler = self . scaler , * * self . kwargs ) # Keep a copy of the scaling dataframe structure so we can align the transform dataset  self . scale_df_structure = pd . DataFrame ( ) . reindex_like ( scale_df ) except AttributeError : pass # Output information to the terminal and log file if required if self . log is not None : self . _print_log ( 2 , ohe_df = ohe_df , scale_df = scale_df , hash_df = hash_df , cv_df = cv_df , tfidf_df = tfidf_df , text_df = text_df ) return self