PREPROCESSING
KernelCenterer
Center an arbitrary kernel matrix :math:`K`.
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)
customer_data = {'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']]}
# DataLens.ai example — KernelCenterer
# 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)
# Centre a linear similarity kernel without changing its symmetry.
kernel = X @ X.T
centred = preprocessing.KernelCenterer().fit_transform(kernel)
assert centred.shape == kernel.shape and np.allclose(centred.mean(axis=0),0,atol=1e-9)
# Asserted result matches the expected behaviour.
print("Expected output verified")
# DataLens.ai example — KernelCenterer
# 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)
# Centre a linear similarity kernel without changing its symmetry.
kernel = X @ X.T
centred = preprocessing.KernelCenterer().fit_transform(kernel)
assert centred.shape == kernel.shape and np.allclose(centred.mean(axis=0),0,atol=1e-9)
# Asserted result matches the expected behaviour.
print("Expected output verified")