Load Data

Create Pipelines

# Create an scaler objectsc=StandardScaler()# Create a pca objectpca=decomposition.PCA()# Create a logistic regression object with an L2 penaltylogistic=linear_model.LogisticRegression()# Create a pipeline of three steps. First, standardize the data.# Second, tranform the data with PCA.# Third, train a logistic regression on the data.pipe=Pipeline(steps=[('sc',sc),('pca',pca),('logistic',logistic)])

Create Parameter Space

# Create a list of a sequence of integers from 1 to 30 (the number of features in X + 1)n_components=list(range(1,X.shape[1]+1,1))# Create a list of values of the regularization parameterC=np.logspace(-4,4,50)# Create a list of options for the regularization penaltypenalty=['l1','l2']# Create a dictionary of all the parameter options # Note has you can access the parameters of steps of a pipeline by using '__’parameters=dict(pca__n_components=n_components,logistic__C=C,logistic__penalty=penalty)

Conduct Parameter Optmization With Pipeline

# View The Best Parametersprint('Best Penalty:',clf.best_estimator_.get_params()['logistic__penalty'])print('Best C:',clf.best_estimator_.get_params()['logistic__C'])print('Best Number Of Components:',clf.best_estimator_.get_params()['pca__n_components'])