- Learning Data Mining with Python(Second Edition)
- Robert Layton
- 426字
- 2021-07-02 23:40:07
Pipelines
As experiments grow, so does the complexity of the operations. We may split up our dataset, binarize features, perform feature-based scaling, perform sample-based scaling, and many more operations.
Keeping track of these operations can get quite confusing and can result in being unable to replicate the result. Problems include forgetting a step, incorrectly applying a transformation, or adding a transformation that wasn't needed.
Another issue is the order of the code. In the previous section, we created our X_transformed dataset and then created a new estimator for the cross validation.If we had multiple steps, we would need to track these changes to the dataset in code.
Pipelines are a construct that addresses these problems (and others, which we will see in the next chapter). Pipelines store the steps in your data mining workflow. They can take your raw data in, perform all the necessary transformations, and then create a prediction. This allows us to use pipelines in functions such as cross_val_score, where they expect an estimator. First, import the Pipeline object:
fromsklearn.pipeline import Pipeline
Pipelines take a list of steps as input, representing the chain of the data mining application. The last step needs to be an Estimator, while all previous steps are Transformers. The input dataset is altered by each Transformer, with the output of one step being the input of the next step. Finally, we classify the samples by the last step's estimator. In our pipeline, we have two steps:
- Use MinMaxScaler to scale the feature values from 0 to 1
- Use KNeighborsClassifier as the classification algorithms
We then represent each step using a tuple ('name', step). We can then create our pipeline:
scaling_pipeline = Pipeline([('scale', MinMaxScaler()),
('predict', KNeighborsClassifier())])
The key here is the list of tuples. The first tuple is our scaling step and the second tuple is the predicting step. We give each step a name: the first we call scale and the second we call predict, but you can choose your own names. The second part of the tuple is the actual Transformer or estimator object.
Running this pipeline is now very easy, using the cross-validation code from before:
scores = cross_val_score(scaling_pipeline, X_broken, y, scoring='accuracy')
print("The pipeline scored an average accuracy for is {0:.1f}%".format(np.mean(transformed_scores) * 100))
This gives us the same score as before (82.3 percent), which is expected, as we are running exactly the same steps, just with an improved interface.
In later chapters, we will use more advanced testing methods and setting up pipelines is a great way to ensure that the code complexity does not grow unmanageably.
- SPSS數(shù)據(jù)挖掘與案例分析應(yīng)用實(shí)踐
- Mastering Concurrency Programming with Java 8
- 流量的秘密:Google Analytics網(wǎng)站分析與優(yōu)化技巧(第2版)
- Python數(shù)據(jù)分析入門與實(shí)戰(zhàn)
- Offer來(lái)了:Java面試核心知識(shí)點(diǎn)精講(原理篇)
- 趣學(xué)Python算法100例
- Designing Hyper-V Solutions
- Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API
- C++程序設(shè)計(jì)基礎(chǔ)教程
- Python編程與幾何圖形
- Learning Hunk
- 執(zhí)劍而舞:用代碼創(chuàng)作藝術(shù)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)案例教程
- Getting Started with Eclipse Juno
- Internet of Things with ESP8266