The Problem

When using fit of the scikit learn CCA classifier it won't allow me to use arrays as features. The error ValueError: Found array with dim 3. Estimator expected <= 2. can be produced whit the following code

from sklearn.cross_decomposition import CCA
    CCA_model = CCA(n_components = 3, max_iter=20000)
    input_arr = [[[k*-1+j*-i*-1 for k in range(125)] for j in range(2)] for i in range(189)]
    input_arr = np.array(input_arr)
    print("INPUT SHAPE:", input_arr.shape)
    input_lbl = [[(-(-1+(-1)**(1+k+j)))/2 for k in range(3)] for j in range(189)]
    input_lbl = np.array(input_lbl)
    print("LABEL SHAPE:", input_lbl.shape)

    model = CCA_model.fit(input_arr, input_lbl)

>>INPUT SHAPE: (189, 2, 125)
>>LABEL SHAPE: (189, 3)

The question

Why is it so, shouldn't it be allowed to use arrays as single features? Is there any parameter I need to modify to do this?


1 Answer 1


No, coding-wise, it cannot use multi-dimensional arrays as single features. You can flatten the array though. Essentially, you want to reshape your input to be (189, 250).

  • $\begingroup$ Yeah, thanks! That's what I suspected... but it would make sense theoretically right, or not even? $\endgroup$
    – mgmussi
    Apr 21, 2021 at 15:47
  • $\begingroup$ Theoretically, CCA is agnostic regarding the structure of the variables. It doesn't matter if you are thinking of the variables as 1D vectors, 2D arrays, multi-dimensional tensors, CCA is just seeking to find linear combinations that maximize correlation of two sets of random variables, it doesn't utilize any structural information. So, there potentially is a way to write out all the theory of CCA treating features as arrays, but this is typically not how the standard theory is laid out. It is much more simple and straightforward to treat the features as 1D vectors. $\endgroup$
    – James
    Apr 21, 2021 at 23:15
  • $\begingroup$ Thank you for taking the time to explain. It was really clarifying. $\endgroup$
    – mgmussi
    Apr 22, 2021 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.