Code
machine-learning
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
num_features = ["age", "income"]
cat_features = ["city"]
preprocess = ColumnTransformer([
("num", Pipeline([
("imputer", SimpleImputer(strategy="median")),
("scaler", StandardScaler()),
]), num_features),
("cat", Pipeline([
("imputer", SimpleImputer(strategy="most_frequent")),
("onehot", OneHotEncoder(handle_unknown="ignore")),
]), cat_features),
])
pipe = Pipeline([
("preprocess", preprocess),
("clf", RandomForestClassifier(n_estimators=100, random_state=42)),
])
# Treats the whole pipeline as a single estimator
pipe.fit(X_train, y_train)
score = pipe.score(X_test, y_test)
print(score)