JoeyC
JoeyC

Reputation: 824

scikit-learn pipelines: Normalising after PCA produces undesired random results

I am running a pipeline that normalises the inputs, runs PCA, normalises PCA factors before finally running a logistic regression.

However, I am getting variable results on the confusion matrix I produce.

I am finding that, if I remove the 3rd step ("normalise_pca" ), my results are constant.

I have set random_state=0 for all the pipeline steps I can. Any idea why I am getting variable results?

def exp2_classifier(X_train, y_train):

    estimators = [('robust_scaler', RobustScaler()), 
                  ('reduce_dim', PCA(random_state=0)), 
                  ('normalise_pca', PowerTransformer()), #I applied this as the distribution of the PCA factors were skew
                  ('clf', LogisticRegression(random_state=0, solver="liblinear"))] 
                #solver specified here to suppress warnings, it doesn't seem to effect gridSearch
    pipe = Pipeline(estimators)

    return pipe

exp2_eval = Evaluation().print_confusion_matrix
logit_grid = Experiment().run_experiment(asdp.data, "heavy_drinker", exp2_classifier, exp2_eval);

Upvotes: 0

Views: 245

Answers (1)

Venkatachalam
Venkatachalam

Reputation: 16966

I am not able to reproduce your error. I have tried other sample dataset from sklearn but got consistent results for multiple runs. Hence, the variance may not be due to normalize_pca

from sklearn import datasets
from sklearn.metrics import confusion_matrix
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import RobustScaler,PowerTransformer
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression

cancer = datasets.load_breast_cancer()
X = cancer.data
y = cancer.target

from sklearn.model_selection import train_test_split

X_train, X_eval, y_train, y_eval = train_test_split(X, y, test_size=0.2, random_state=42)

estimators = [('robust_scaler', RobustScaler()), 
              ('reduce_dim', PCA(random_state=0)), 
              ('normalise_pca', PowerTransformer()), #I applied this as the distribution of the PCA factors were skew
              ('clf', LogisticRegression(random_state=0, solver="liblinear"))] 
            #solver specified here to suppress warnings, it doesn't seem to effect gridSearch
pipe = Pipeline(estimators)

pipe.fit(X_train,y_train)

print('train data :')
print(confusion_matrix(y_train,pipe.predict(X_train)))
print('test data :')
print(confusion_matrix(y_eval,pipe.predict(X_eval)))

output:

train data :
[[166   3]
 [  4 282]]
test data :
[[40  3]
 [ 3 68]]

Upvotes: 1

Related Questions