Datalens.ai
← All algorithms

PREPROCESSING

FunctionTransformer

Constructs a transformer from an arbitrary callable.

Example of usage

Python API reference included; executable data demonstration pending review.

Usage / limitations

+Use when: Transform inputs for reliable modelling.

−Limitations: Check input requirements, mathematical assumptions and version-specific behaviour before using.

Mathematical formula

See method documentation: implementation and assumptions vary.
Sample dataShow
{
  "variable": "customer_data",
  "domain": "Customer analytics and customer segmentation",
  "feature_names": [
    "monthly_spend_usd",
    "visits_per_month",
    "support_tickets"
  ],
  "records": [
    {
      "monthly_spend_usd": 20,
      "visits_per_month": 2,
      "support_tickets": 0
    },
    {
      "monthly_spend_usd": 40,
      "visits_per_month": 4,
      "support_tickets": 1
    },
    {
      "monthly_spend_usd": 80,
      "visits_per_month": 6,
      "support_tickets": 2
    },
    {
      "monthly_spend_usd": 120,
      "visits_per_month": 8,
      "support_tickets": 3
    },
    {
      "monthly_spend_usd": 160,
      "visits_per_month": 10,
      "support_tickets": 4
    },
    {
      "monthly_spend_usd": 200,
      "visits_per_month": 12,
      "support_tickets": 5
    }
  ],
  "segments": [
    "new",
    "returning",
    "loyal",
    "new",
    "returning",
    "loyal"
  ],
  "channel": [
    "email",
    "search",
    "social",
    "email",
    "search",
    "social"
  ],
  "tags": [
    [
      "online"
    ],
    [
      "online",
      "member"
    ],
    [
      "member"
    ],
    [
      "online"
    ],
    [
      "member"
    ],
    [
      "online",
      "member"
    ]
  ]
}

Code example (Python)

# DataLens.ai example — FunctionTransformer
# Domain: customer analytics and feature engineering.
# Copy input + code + test provides the named input data above.
import numpy as np
from sklearn import preprocessing
# Convert named customer attributes to a feature matrix.
feature_names = customer_data["feature_names"]
X = np.array([[r[name] for name in feature_names] for r in customer_data["records"]], dtype=float)
# Add ten dollars to each monthly-spend observation with a custom function.
transformer = preprocessing.FunctionTransformer(lambda matrix: matrix + 10.0)
result = transformer.transform(X)
assert np.allclose(result, X + 10.0)
# Asserted result matches the expected behaviour.
print("Expected output verified")