# Path: malss/rfpimp.py
# def oob_importances(rf, X_train, y_train, n_samples=5000):
#     """
#     Compute permutation feature importances for scikit-learn
#     RandomForestClassifier or RandomForestRegressor in arg rf.
# 
#     Given training X and y data, return a data frame with columns
#     Feature and Importance sorted in reverse order by importance.
#     The training data is needed to compute out of bag (OOB)
#     model performance measures (accuracy or R^2). The model
#     is not retrained.
# 
#     By default, sample up to 5000 observations to compute feature importances.
# 
#     return: A data frame with Feature, Importance columns
# 
#     SAMPLE CODE
# 
#     rf = RandomForestRegressor(n_estimators=100, n_jobs=-1, oob_score=True)
#     X_train, y_train = ..., ...
#     rf.fit(X_train, y_train)
#     imp = oob_importances(rf, X_train, y_train)
#     """
#     if isinstance(rf, RandomForestClassifier):
#         return permutation_importances(rf, X_train, y_train, oob_classifier_accuracy, n_samples)
#     elif isinstance(rf, RandomForestRegressor):
#         return permutation_importances(rf, X_train, y_train, oob_regression_r2_score, n_samples)
#     return None


import numpy as np
import pandas as pd
from sklearn.utils import shuffle as sk_shuffle
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.preprocessing import StandardScaler
from .rfpimp import oob_importances

        Xtrans, col_was_null = self.__impute(Xtrans)
        Xtrans, del_columns = self.__encode(Xtrans)
        if self.standardize:
            Xtrans = self.__standardize(Xtrans)
        
        if self.columns is not None:
            Xtrans = Xtrans[self.columns]

        return Xtrans

    def drop_col(self, mdl):
        print('Start feature selection.')
        X = self.X.copy(deep=True)

        while True:
            col, X = self.__drop_col_sub(X, self.y, mdl)
            if col is None:
                break
        
        if len(X.columns) == len(self.X.columns):
            print('No features dropped.')
        else:
            self.X = X
            self.columns = X.columns

    def __drop_col_sub(self, X, y, rf, k=10, thr=0.0):
        col = None
        rf.fit(X, y)
        np.random.seed(0)

# Path: malss/rfpimp.py
# def oob_importances(rf, X_train, y_train, n_samples=5000):
#     """
#     Compute permutation feature importances for scikit-learn
#     RandomForestClassifier or RandomForestRegressor in arg rf.
# 
#     Given training X and y data, return a data frame with columns
#     Feature and Importance sorted in reverse order by importance.
#     The training data is needed to compute out of bag (OOB)
#     model performance measures (accuracy or R^2). The model
#     is not retrained.
# 
#     By default, sample up to 5000 observations to compute feature importances.
# 
#     return: A data frame with Feature, Importance columns
# 
#     SAMPLE CODE
# 
#     rf = RandomForestRegressor(n_estimators=100, n_jobs=-1, oob_score=True)
#     X_train, y_train = ..., ...
#     rf.fit(X_train, y_train)
#     imp = oob_importances(rf, X_train, y_train)
#     """
#     if isinstance(rf, RandomForestClassifier):
#         return permutation_importances(rf, X_train, y_train, oob_classifier_accuracy, n_samples)
#     elif isinstance(rf, RandomForestRegressor):
#         return permutation_importances(rf, X_train, y_train, oob_regression_r2_score, n_samples)
#     return None


# Path: malss/data.py
import numpy as np
import pandas as pd
from sklearn.utils import shuffle as sk_shuffle
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.preprocessing import StandardScaler
from .rfpimp import oob_importances


        Xtrans, col_was_null = self.__impute(Xtrans)
        Xtrans, del_columns = self.__encode(Xtrans)
        if self.standardize:
            Xtrans = self.__standardize(Xtrans)
        
        if self.columns is not None:
            Xtrans = Xtrans[self.columns]

        return Xtrans

    def drop_col(self, mdl):
        print('Start feature selection.')
        X = self.X.copy(deep=True)

        while True:
            col, X = self.__drop_col_sub(X, self.y, mdl)
            if col is None:
                break
        
        if len(X.columns) == len(self.X.columns):
            print('No features dropped.')
        else:
            self.X = X
            self.columns = X.columns

    def __drop_col_sub(self, X, y, rf, k=10, thr=0.0):
        col = None
        rf.fit(X, y)
        np.random.seed(0)

        imp = oob_importances(rf, X, y, n_samples=len(X))