For this tutorial, we’ll set up a very basic pipeline that consists of the following sequence:
- Scaler: For pre-processing data, i.e., transform the data to zero mean and unit variance using the StandardScaler().
- Feature selector: Use VarianceThreshold() for discarding features whose variance is less than a certain defined threshold.
- Classifier: KNeighborsClassifier(), which implements the k-nearest neighbor classifier and selects the class of the majority k points, which are closest to the test example.
pipe = Pipeline([
The pipe object is simple to understand. It says, scale first, select features second and classify in the end. Let’s call fit() method of the pipe object on our training data and get the training and test scores.
print('Training set score: ’ + str(pipe.score(X_train,y_train)))
print('Test set score: ’ + str(pipe.score(X_test,y_test)))
Running the example you should see the following:
Training set score: 0.8794642857142857
Test set score: 0.8392857142857143