Identical section banners and shared descriptions in both scripts;
ochre DIFF comments (Python side only, R holds a blank #)
explain what changes; red = mlfeatures gap.
Both ran. StepOrderNorm and StepPca run inside the recipe; PC1-4 variance = 59.44/24.42/6.95/5.44% (R: 59.4/24.4/…).
# Setup -------------------------------------------------------------------- # Load the stack. No estimator anywhere: the recipe is the product. # # library(tidymodels) tidymodels_prefer() library(beans) library(bestNormalize) # 3-way split -------------------------------------------------------------- # 75 / 12.5 / 12.5 train / validation / test, stratified by class: # extraction is judged on the validation set, honestly. # set.seed(1601) bean_split <- initial_validation_split(beans, strata = class, prop = c(0.75, 0.125)) bean_train <- training(bean_split) bean_validation <- validation(bean_split) bean_test <- testing(bean_split) # Base recipe -------------------------------------------------------------- # zv guard, tame the skew, shared scale: PCA's preconditions generate # the step ordering. fit() ~ prep(); transform() ~ bake(). # bean_rec <- recipe(class ~ ., data = bean_train) %>% step_zv(all_numeric_predictors()) %>% step_orderNorm(all_numeric_predictors()) %>% step_normalize(all_numeric_predictors()) bean_rec_trained <- prep(bean_rec) bake(bean_rec_trained, new_data = bean_validation) %>% head() # PCA ---------------------------------------------------------------------- # Four components, estimated on training data only, scored on validation. # # # bean_pca_rec <- bean_rec_trained %>% step_pca(all_numeric_predictors(), num_comp = 4, id = "pca") %>% prep() bake(bean_pca_rec, new_data = bean_validation) %>% head() # Loadings + variance ------------------------------------------------------ # The no-model deliverables: top loadings and percent variance. # # # tidy(bean_pca_rec, id = "pca") %>% group_by(component) %>% slice_max(abs(value), n = 5) tidy(bean_pca_rec, id = "pca", type = "variance")
# Setup -------------------------------------------------------------------- # Load the stack. No estimator anywhere: the recipe is the product. # DIFF: sklearn appears only for the split helper, not for a model. # DIFF: beans ships in mlfeatures.data. import polars as pl from sklearn.model_selection import train_test_split from mlfeatures import (Recipe, StepDropZeroVariance, StepNormalize, StepOrderNorm, StepPca) from mlfeatures import Selectors as s from mlfeatures.data import beans # 3-way split -------------------------------------------------------------- # 75 / 12.5 / 12.5 train / validation / test, stratified by class: # extraction is judged on the validation set, honestly. # DIFF: initial_validation_split -> two chained train_test_splits. bean_train, rest = train_test_split( beans, train_size=0.75, stratify=beans["class"], random_state=1601) bean_validation, bean_test = train_test_split( rest, train_size=0.50, stratify=rest["class"], random_state=1601) # Base recipe -------------------------------------------------------------- # zv guard, tame the skew, shared scale: PCA's preconditions generate # the step ordering. fit() ~ prep(); transform() ~ bake(). # DIFF: StepOrderNorm = step_orderNorm (bestNormalize's ORQ transform). bean_rec = ( Recipe(data=bean_train, target="class") .add(StepDropZeroVariance(s.numeric_features())) .add(StepOrderNorm(s.numeric_features())) .add(StepNormalize(s.numeric_features())) ) fitted = bean_rec.fit(bean_train) fitted.transform(bean_validation).head() # PCA ---------------------------------------------------------------------- # Four components, estimated on training data only, scored on validation. # DIFF: StepPca runs inside the recipe, so transform() returns the scores. # DIFF: R pipes a NEW step onto the already-prep()ed recipe and re-preps # (incremental fit); mlfeatures recipes are declared whole and fit once. pca_rec = bean_rec.add(StepPca(s.numeric_features(), num_comp=4)) pca_fitted = pca_rec.fit(bean_train) scores = pca_fitted.transform(bean_validation) scores.head() # Loadings + variance ------------------------------------------------------ # The no-model deliverables: top loadings and percent variance. # DIFF: tidy(id = "pca", type = "variance") -> score variance over total variance. # DIFF (GAP): no public accessor for a fitted step's loadings yet, so the # top-loadings table has no mlfeatures equivalent. total_var = (fitted.transform(bean_train).drop("class") .select(pl.sum_horizontal(pl.all().var())).item()) pca_fitted.transform(bean_train).select( pl.col("^PC[0-9]+$").var() / total_var * 100)