sklearn.cross_decomposition.CCA?

class sklearn.cross_decomposition.CCA(n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True)

[源碼]

CCA典型相關分析

CCA繼承mode=“B”和deflation_mode =“canonical”的PLS。

在用戶指南中閱讀更多內容。

參數 說明
n_components int, (default 2)
要保留的組件數。
scale boolean, (default True)
是否縮放數據?
max_iter an integer, (default 500)
NIPALS內部循環的最大迭代次數
tol non-negative real, default 1e-06.
迭代算法中使用的容差。
copy boolean
是否在副本上進行緊縮。 除非您不關心副作用,否則將默認值設為True
屬性 說明
x_weights_ array, [p, n_components]
X的權重向量。
y_weights_ array, [q, n_components]
Y的權重向量。
x_loadings_ array, [p, n_components]
X的加載向量。
y_loadings_ array, [q, n_components]
Y的加載向量。
x_scores_ array, [n_samples, n_components]
X的分數。
y_scores_ array, [n_samples, n_components]
Y的分數。
x_rotations_ array, [p, n_components]
X塊到隱藏的旋轉。
y_rotations_ array, [q, n_components]
Y塊到隱藏的旋轉。
coef_ array of shape (p, q)
線性模型的系數:Y = X coef_ + Err
n_iter_ array-like
每個組件的NIPALS內部循環的迭代次數。

另見:

對于每個分量k,找到使max corr(Xk u,Yk v)最大化的權重u,v,使得|u| = |v| = 1

注意,它僅使得分之間的相關性最大化。

X(Xk + 1)塊的殘差矩陣是通過收縮當前X分數x_score獲得的。

Y(Yk + 1)塊的殘差矩陣是通過收縮當前*Y分數獲得的。

參考

Jacob A. Wegelin. A survey of Partial Least Squares (PLS) methods, with emphasis on the two-block case. Technical Report 371, Department of Statistics, University of Washington, Seattle, 2000.

In french but still a reference: Tenenhaus, M. (1998). La regression PLS: theorie et pratique. Paris: Editions Technic.

示例

>>> from sklearn.cross_decomposition import CCA
>>> X = [[0.0.1.], [1.,0.,0.], [2.,2.,2.], [3.,5.,4.]]
>>> Y = [[0.1-0.2], [0.91.1], [6.25.9], [11.912.3]]
>>> cca = CCA(n_components=1)
>>> cca.fit(X, Y)
CCA(n_components=1)
>>> X_c, Y_c = cca.transform(X, Y)

方法

fit(self, X, Y) 訓練數據
fit_transform(self, X[, y]) 在訓練集上學習并應用降維。
get_params(self[, deep]) 獲取評估器的參數。
inverse_transform(self, X) 將數據轉換回其原始空間。
predict(self, X[, copy]) 應用在訓練集學習到的降維。
score(self, X, y[, sample_weight]) 返回預測的確定系數R^2。
set_params(self,**params) 設置評估器參數。
transform(self, X[, Y, copy]) 應用在訓練集上學習到的降維。
__init__(self, n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True)

[源碼]

初始化self, 請參閱help(type(self))以獲得準確的說明

fit(self, X, Y)

源碼

訓練數據。

參數 說明
X array-like of shape (n_samples, n_features)
訓練向量,其中n_samples是樣本數,n_features是預測變量數。
Y array-like of shape (n_samples, n_targets)
目標向量,其中n_samples是樣本數,n_targets是響應變量數。
fit_transform(self, X, y=None)

[源碼]

在訓練集上學習并應用降維

參數 說明
X array-like of shape (n_samples, n_features)
訓練向量,其中n_samples是樣本數,n_features是預測變量數。
y array-like of shape (n_samples, n_targets)
目標向量,其中n_samples是樣本數,n_targets是響應變量數。
返回值
如果未指定Y,則為x_scores,否則為(x_scores,y_scores)。
get_params(self, deep=True)

[源碼]

獲取此估算器的參數。

參數 說明
deep bool, default=True
如果為True,將返回此估算器和其所包含子對象的參數。
返回值 說明
params mapping of string to any
參數名稱映射到其值。
inverse_transform(self, X)

[源碼]

將數據轉換回其原始空間。

參數 說明
X array-like of shape (n_samples, n_components)
新數據,其中n_samples是樣本數,n_components是pls分量數。
返回值 說明
x_reconstructed array-like of shape (n_samples, n_features)

僅當n_components = n_features時,此轉換才是精確的

predict(self, X, copy=True)

[源碼]

在訓練集上學習到的降維應用

參數 說明
X array-like of shape (n_samples, n_features)
訓練向量,其中n_samples是樣本數,n_features是預測變量數。
copy boolean, default True
是復制X和Y,還是執行就地歸一化。

該調用需要估計p x q矩陣,這在高維空間中可能是個問題。

score(self, X, y, sample_weight=None)

[源碼]

返回預測的確定系數R^2。

系數R^2定義為(1- u/v),其中u是平方的殘差和((y_true-y_pred)** 2).sum(),而v是平方的總和((y_true- y_true.mean())** 2).sum()。 可能的最高得分為1.0,并且可能為負(因為該模型可能會更差)。 不管輸入特征如何,常數模型預測y的期望值,獲得的R^2分數將始終是0.0。

參數 說明
X array-like of shape (n_samples, n_features)
測試樣本。對于某些評估器,可以是預先計算的核矩陣或通用對象列表,shape =(n_samples,n_samples_fitted),其中n_samples_fitted是用于評估器訓練的樣本數。
y array-like of shape (n_samples,) or (n_samples, n_outputs)
X的真實值。
sample_weight array-like of shape (n_samples,), default=None
樣本權重
返回值 說明
score float
self.predict(X)關于y的R^2

在回歸器上調用score時使用的R2得分使用版本0.23中的multioutput ='uniform_average'來保持與r2_score的默認值一致。 這會影響所有多輸出回歸器的評分方法(MultiOutputRegressor除外)。

set_params(self, **params) 

[源碼]

設置此估算器的參數。

該方法適用于簡單的估算器以及嵌套對象(例如管道)。后者具有形式參數<component>__<parameter>,以便可以更新嵌套對象的每個組件。

參數 說明
**params dict
估算器參數。
返回值 說明
self object
估算器實例。
transform(self, X, Y=None, copy=True)

[源碼]

在訓練集上學習到的降維應用

參數 說明
X array-like of shape (n_samples, n_features)
訓練向量,其中n_samples是樣本數,n_features是預測變量數。
Y array-like of shape (n_samples, n_targets)
目標向量,其中n_samples是樣本數,n_targets是響應變量數。
copy boolean, default True
是復制X和Y,還是執行就地歸一化。
返回值說明
如果未指定Y,則為x_scores,否則為(x_scores,y_scores)。

sklearn.cross_decomposition.CCA使用示例?