How to get the first canonical correlation from sklearn's CCA module?

后端 未结 2 1853
感情败类
感情败类 2020-12-28 22:38

In scikit-learn for Python, there is a module call cross_decomposition with a canonical correlation analysis (CCA) class. I have been trying to figure out how to give the c

2条回答
  •  梦毁少年i
    2020-12-28 23:13

    Well, with some help looking at the source code in pyrcca I managed to create this snippet of code to get out the first canonical correlation.

    cca = CCA(n_components=1)
    U_c, V_c = cca.fit_transform(U, V)
    
    result = np.corrcoef(U_c.T, V_c.T)[0,1]
    

    Hope this helps someone else.

    Note: The pyrcca package mentioned above runs slightly quicker than sci-kit learn's, due to heavier usage of multi-core processing for anyone who was curious. Also they have implemented kernel CCA unlike sklearn.

提交回复
热议问题