DIMENSIONALITY
CCA
Canonical Correlation Analysis, also known as "Mode B" PLS.
Example of usage
Reduce multivariate customer records into lower-dimensional components using CCA.
Domains of usage
Customer analytics · Feature representation · Exploratory data analysis
Usage / limitations
+Use when: Explore a compact representation of multiple customer measurements using CCA.
−Limitations: Results depend on scaling, sample size and algorithm-specific assumptions; fitted coordinates are not automatically causal or predictive.
Mathematical formula
max corr(Xa, Yb)
The method transforms the input feature matrix into a compact representation; the numerical assertion checks the fitted embedding on the example data.
Sample dataShow
"{'variable': 'customer_behaviour', 'domain': 'Retail customer behaviour analytics', 'feature_names': ['weekday_orders', 'weekend_orders', 'average_basket_value', 'discount_redemptions', 'loyalty_visits', 'returns'], 'records': [{'weekday_orders': 2.8, 'weekend_orders': 5.0, 'average_basket_value': 1.5, 'discount_redemptions': 3.0, 'loyalty_visits': 4.0, 'returns': 1.0}, {'weekday_orders': 2.71, 'weekend_orders': 5.72, 'average_basket_value': 2.0, 'discount_redemptions': 3.61, 'loyalty_visits': 4.51, 'returns': 1.36}, {'weekday_orders': 2.94, 'weekend_orders': 6.44, 'average_basket_value': 2.5, 'discount_redemptions': 3.41, 'loyalty_visits': 5.02, 'returns': 1.72}, {'weekday_orders': 3.17, 'weekend_orders': 5.97, 'average_basket_value': 2.04, 'discount_redemptions': 4.02, 'loyalty_visits': 4.27, 'returns': 2.08}, {'weekday_orders': 3.4, 'weekend_orders': 6.69, 'average_basket_value': 2.54, 'discount_redemptions': 3.82, 'loyalty_visits': 4.78, 'returns': 1.44}, {'weekday_orders': 4.91, 'weekend_orders': 6.22, 'average_basket_value': 3.04, 'discount_redemptions': 4.43, 'loyalty_visits': 5.29, 'returns': 1.8}, {'weekday_orders': 4.34, 'weekend_orders': 6.94, 'average_basket_value': 2.58, 'discount_redemptions': 4.23, 'loyalty_visits': 4.54, 'returns': 2.16}, {'weekday_orders': 4.57, 'weekend_orders': 6.47, 'average_basket_value': 3.08, 'discount_redemptions': 4.84, 'loyalty_visits': 5.05, 'returns': 2.52}, {'weekday_orders': 4.8, 'weekend_orders': 7.19, 'average_basket_value': 3.58, 'discount_redemptions': 4.64, 'loyalty_visits': 5.56, 'returns': 1.88}, {'weekday_orders': 5.51, 'weekend_orders': 7.91, 'average_basket_value': 3.12, 'discount_redemptions': 4.44, 'loyalty_visits': 4.81, 'returns': 2.24}, {'weekday_orders': 6.54, 'weekend_orders': 7.44, 'average_basket_value': 3.62, 'discount_redemptions': 5.05, 'loyalty_visits': 5.32, 'returns': 2.6}, {'weekday_orders': 5.97, 'weekend_orders': 8.16, 'average_basket_value': 4.12, 'discount_redemptions': 4.85, 'loyalty_visits': 5.83, 'returns': 2.96}, {'weekday_orders': 6.2, 'weekend_orders': 7.69, 'average_basket_value': 3.66, 'discount_redemptions': 5.46, 'loyalty_visits': 5.08, 'returns': 2.32}, {'weekday_orders': 6.91, 'weekend_orders': 8.41, 'average_basket_value': 4.16, 'discount_redemptions': 5.26, 'loyalty_visits': 5.59, 'returns': 2.68}, {'weekday_orders': 7.14, 'weekend_orders': 7.94, 'average_basket_value': 4.66, 'discount_redemptions': 5.87, 'loyalty_visits': 6.1, 'returns': 3.04}, {'weekday_orders': 8.17, 'weekend_orders': 8.66, 'average_basket_value': 4.2, 'discount_redemptions': 5.67, 'loyalty_visits': 5.35, 'returns': 3.4}], 'outcome_names': ['next_month_spend', 'customer_tenure'], 'outcomes': [{'next_month_spend': 35.0, 'customer_tenure': 10.0}, {'next_month_spend': 40.4, 'customer_tenure': 12.15}, {'next_month_spend': 45.8, 'customer_tenure': 14.3}, {'next_month_spend': 51.2, 'customer_tenure': 16.45}, {'next_month_spend': 47.0, 'customer_tenure': 18.6}, {'next_month_spend': 52.4, 'customer_tenure': 13.25}, {'next_month_spend': 57.8, 'customer_tenure': 15.4}, {'next_month_spend': 63.2, 'customer_tenure': 17.55}, {'next_month_spend': 59.0, 'customer_tenure': 19.7}, {'next_month_spend': 64.4, 'customer_tenure': 21.85}, {'next_month_spend': 69.8, 'customer_tenure': 16.5}, {'next_month_spend': 75.2, 'customer_tenure': 18.65}, {'next_month_spend': 71.0, 'customer_tenure': 20.8}, {'next_month_spend': 76.4, 'customer_tenure': 22.95}, {'next_month_spend': 81.8, 'customer_tenure': 25.1}, {'next_month_spend': 87.2, 'customer_tenure': 19.75}]}"
Code example (Python)
# DataLens.ai sample input
# Domain: Retail customer behaviour analytics
customer_behaviour = {'variable': 'customer_behaviour', 'domain': 'Retail customer behaviour analytics', 'feature_names': ['weekday_orders', 'weekend_orders', 'average_basket_value', 'discount_redemptions', 'loyalty_visits', 'returns'], 'records': [{'weekday_orders': 2.8, 'weekend_orders': 5.0, 'average_basket_value': 1.5, 'discount_redemptions': 3.0, 'loyalty_visits': 4.0, 'returns': 1.0}, {'weekday_orders': 2.71, 'weekend_orders': 5.72, 'average_basket_value': 2.0, 'discount_redemptions': 3.61, 'loyalty_visits': 4.51, 'returns': 1.36}, {'weekday_orders': 2.94, 'weekend_orders': 6.44, 'average_basket_value': 2.5, 'discount_redemptions': 3.41, 'loyalty_visits': 5.02, 'returns': 1.72}, {'weekday_orders': 3.17, 'weekend_orders': 5.97, 'average_basket_value': 2.04, 'discount_redemptions': 4.02, 'loyalty_visits': 4.27, 'returns': 2.08}, {'weekday_orders': 3.4, 'weekend_orders': 6.69, 'average_basket_value': 2.54, 'discount_redemptions': 3.82, 'loyalty_visits': 4.78, 'returns': 1.44}, {'weekday_orders': 4.91, 'weekend_orders': 6.22, 'average_basket_value': 3.04, 'discount_redemptions': 4.43, 'loyalty_visits': 5.29, 'returns': 1.8}, {'weekday_orders': 4.34, 'weekend_orders': 6.94, 'average_basket_value': 2.58, 'discount_redemptions': 4.23, 'loyalty_visits': 4.54, 'returns': 2.16}, {'weekday_orders': 4.57, 'weekend_orders': 6.47, 'average_basket_value': 3.08, 'discount_redemptions': 4.84, 'loyalty_visits': 5.05, 'returns': 2.52}, {'weekday_orders': 4.8, 'weekend_orders': 7.19, 'average_basket_value': 3.58, 'discount_redemptions': 4.64, 'loyalty_visits': 5.56, 'returns': 1.88}, {'weekday_orders': 5.51, 'weekend_orders': 7.91, 'average_basket_value': 3.12, 'discount_redemptions': 4.44, 'loyalty_visits': 4.81, 'returns': 2.24}, {'weekday_orders': 6.54, 'weekend_orders': 7.44, 'average_basket_value': 3.62, 'discount_redemptions': 5.05, 'loyalty_visits': 5.32, 'returns': 2.6}, {'weekday_orders': 5.97, 'weekend_orders': 8.16, 'average_basket_value': 4.12, 'discount_redemptions': 4.85, 'loyalty_visits': 5.83, 'returns': 2.96}, {'weekday_orders': 6.2, 'weekend_orders': 7.69, 'average_basket_value': 3.66, 'discount_redemptions': 5.46, 'loyalty_visits': 5.08, 'returns': 2.32}, {'weekday_orders': 6.91, 'weekend_orders': 8.41, 'average_basket_value': 4.16, 'discount_redemptions': 5.26, 'loyalty_visits': 5.59, 'returns': 2.68}, {'weekday_orders': 7.14, 'weekend_orders': 7.94, 'average_basket_value': 4.66, 'discount_redemptions': 5.87, 'loyalty_visits': 6.1, 'returns': 3.04}, {'weekday_orders': 8.17, 'weekend_orders': 8.66, 'average_basket_value': 4.2, 'discount_redemptions': 5.67, 'loyalty_visits': 5.35, 'returns': 3.4}], 'outcome_names': ['next_month_spend', 'customer_tenure'], 'outcomes': [{'next_month_spend': 35.0, 'customer_tenure': 10.0}, {'next_month_spend': 40.4, 'customer_tenure': 12.15}, {'next_month_spend': 45.8, 'customer_tenure': 14.3}, {'next_month_spend': 51.2, 'customer_tenure': 16.45}, {'next_month_spend': 47.0, 'customer_tenure': 18.6}, {'next_month_spend': 52.4, 'customer_tenure': 13.25}, {'next_month_spend': 57.8, 'customer_tenure': 15.4}, {'next_month_spend': 63.2, 'customer_tenure': 17.55}, {'next_month_spend': 59.0, 'customer_tenure': 19.7}, {'next_month_spend': 64.4, 'customer_tenure': 21.85}, {'next_month_spend': 69.8, 'customer_tenure': 16.5}, {'next_month_spend': 75.2, 'customer_tenure': 18.65}, {'next_month_spend': 71.0, 'customer_tenure': 20.8}, {'next_month_spend': 76.4, 'customer_tenure': 22.95}, {'next_month_spend': 81.8, 'customer_tenure': 25.1}, {'next_month_spend': 87.2, 'customer_tenure': 19.75}]}
# DataLens.ai example
# Domain: Retail customer behaviour analytics.
# Reduce customer features while preserving the structure captured by this method.
import numpy as np
from sklearn.cross_decomposition import CCA
# Extract named features from the sample input.
feature_names = customer_behaviour["feature_names"]
X = np.array([[record[key] for key in feature_names] for record in customer_behaviour["records"]], dtype=float)
outcome_names = customer_behaviour["outcome_names"]
Y = np.array([[record[key] for key in outcome_names] for record in customer_behaviour["outcomes"]], dtype=float)
model = CCA(n_components=2, max_iter=500)
# Fit the two aligned views of the same customers.
model.fit(X, Y)
embedding = model.transform(X, Y)[0]
# Verify every customer has the expected number of finite components.
assert embedding.shape == (len(X), 2), embedding.shape
assert np.isfinite(embedding).all(), "Non-finite embedding values"
assert np.isclose(np.linalg.norm(embedding), 3.2550510593, rtol=0.05, atol=1e-5), "Unexpected embedding magnitude"
print("Expected output verified")
# DataLens.ai example
# Domain: Retail customer behaviour analytics.
# Reduce customer features while preserving the structure captured by this method.
import numpy as np
from sklearn.cross_decomposition import CCA
# Extract named features from the sample input.
feature_names = customer_behaviour["feature_names"]
X = np.array([[record[key] for key in feature_names] for record in customer_behaviour["records"]], dtype=float)
outcome_names = customer_behaviour["outcome_names"]
Y = np.array([[record[key] for key in outcome_names] for record in customer_behaviour["outcomes"]], dtype=float)
model = CCA(n_components=2, max_iter=500)
# Fit the two aligned views of the same customers.
model.fit(X, Y)
embedding = model.transform(X, Y)[0]
# Verify every customer has the expected number of finite components.
assert embedding.shape == (len(X), 2), embedding.shape
assert np.isfinite(embedding).all(), "Non-finite embedding values"
assert np.isclose(np.linalg.norm(embedding), 3.2550510593, rtol=0.05, atol=1e-5), "Unexpected embedding magnitude"
print("Expected output verified")