Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Efficient Data Preprocessing with Pipelines | Section
Machine Learning Foundations with Scikit-Learn

bookEfficient Data Preprocessing with Pipelines

Veeg om het menu te tonen

With the ability to transform columns separately using the make_column_transformer function, the next step is to build pipelines. A pipeline is a container that organizes preprocessing steps and applies them sequentially.

A pipeline in Scikit-learn can be created using either the Pipeline class constructor or the make_pipeline function from the sklearn.pipeline module. This course will focus on make_pipeline, as it is simpler to apply.

You just need to pass all the transformers as arguments to a function. Creating pipelines is that simple.

However, when you call the .fit_transform(X) method on the Pipeline object, it applies .fit_transform(X) to every transformer inside the pipeline, so if you want to treat some columns differently, then you should use a ColumnTransformer and pass it to make_pipeline().

Build a pipeline using the same file as in the previous chapter. The pipeline should include encoders for categorical features along with SimpleImputer. Since the dataset contains both nominal and ordinal features, use a ColumnTransformer to process them separately.

1234567891011121314151617
import pandas as pd from sklearn.compose import make_column_transformer from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder from sklearn.impute import SimpleImputer from sklearn.pipeline import make_pipeline df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/exams.csv') # Making a column transformer edu_categories = ['high school', 'some high school', 'some college', "associate's degree", "bachelor's degree", "master's degree"] ct = make_column_transformer( (OrdinalEncoder(categories=[edu_categories]), ['parental level of education']), (OneHotEncoder(), ['gender', 'race/ethnicity', 'lunch', 'test preparation course']), remainder='passthrough' ) # Making a Pipeline pipe = make_pipeline(ct, SimpleImputer(strategy='most_frequent')) print(pipe.fit_transform(df))
copy
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 19

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Sectie 1. Hoofdstuk 19
some-alt