Extract More Than Two Dimensions via Python: sklearn.cross_decomposition import CCA & transform

Viewed 31

I am very interested in using Python to extract 3-4 Dimensions via Canonical Correlation Analyses. I am pasting my very basic code below, and it appears to always default to only extracting two Dimensions even though each of my input arrays are 10,000+ X 3. Even if I have 4 columns for my X & Y matrices it always gives just two Dimensions - was hoping for three and eventually four as I add many more raw Features to my X and Y arrays. Trying to keep simple for now. Could part of my problem also be that some of my Field Names have spaces in them too?

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

data = "G:\Shared drives\Data Intelligence\ZF\Segmentation/Data.csv"
df = pd.read_csv(data)
df.head()
print(df.columns)

X = df[['Altcurr Ext Stk Sales Cc',\
        'Altcurr Ext Dss Sales Cc',\
        'LBM Sales']]
X.head()
X_mc = (X-X.mean())/(X.std())
X_mc.head()

Y = df[['Primary_Supplier_0_org1',\
        'Primary_Supplier_1_org2',\
        'Primary_Supplier_2_TV']]
Y.head()

Y_mc = (Y-Y.mean())/(Y.std())
Y_mc.head()

from sklearn.cross_decomposition import CCA

ca = CCA()
ca.fit(X_mc, Y_mc)
X_c, Y_c = ca.transform(X_mc, Y_mc)
1 Answers

By default the CCA() function sets , you can check out the documentation :

Parameters:
n_components int, default=2
Number of components to keep. Should be in [1, min(n_samples, n_features, n_targets)].

For your dataset, X and Y both have 3 columns, so you can go up to n_components = 3 . Using an example dataset :

from sklearn.datasets import make_blobs
from sklearn.cross_decomposition import CCA
X, _ = make_blobs(n_samples=10000, centers=3, n_features=6,random_state=0)

y = X[:,3:]
X = X[:,:3]

ca = CCA(n_components = 3)
ca.fit(X, y)
X_c, Y_c = ca.transform(X, y)

print(X_c.shape)
(10000, 3)
print(Y_c.shape)
(10000, 3)
Related