Datalens.ai
← All algorithms

PREPROCESSING

MultiLabelBinarizer

Transform between iterable of iterables and a multilabel format.

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 — MultiLabelBinarizer
# 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)
# Encode customers' overlapping membership tags.
model = preprocessing.MultiLabelBinarizer()
result = model.fit_transform(customer_data['tags'])
assert result.shape == (len(customer_data['tags']), 2) and set(model.classes_) == {'member','online'}
# Asserted result matches the expected behaviour.
print("Expected output verified")