Building machine learning pipelines
The scikit-learn library has provisions to build machine learning pipelines. We just need to specify the functions, and it will build a composed object that makes the data go through the whole pipeline. This pipeline can include functions, such as preprocessing, feature selection, supervised learning, unsupervised learning, and so on. In this recipe, we will be building a pipeline to take the input feature vector, select the top k features, and then classify them using a random forest classifier.
How to do it…
Create a new Python file, and import the following packages:
from sklearn.datasets import samples_generator from sklearn.ensemble import RandomForestClassifier from sklearn.feature_selection import SelectKBest, f_regression from sklearn.pipeline import Pipeline
Let's generate some sample data to play with:
# generate sample data X, y = samples_generator.make_classification( n_informative=4, n_features=20, n_redundant=0, random_state=5)
This line...