Skip to content

response.tca_model

Classes

TCAModel

TCAModel(kernel_type='rbf', kernel_param=1.0, dim=20, mu=0.5)

Transfer Component Analysis (TCA) for domain adaptation.

TCA finds a latent space where source and target domain distributions are similar.

Parameters:

Name Type Description Default
kernel_type (linear, rbf, poly)

Type of kernel function to use: - 'linear': Linear kernel (dot product) - 'rbf': Radial Basis Function (Gaussian) kernel - 'poly': Polynomial kernel

'linear'
kernel_param float

Parameter for kernel function: - For 'rbf': gamma parameter (inverse of kernel width) - For 'poly': degree of polynomial

1.0
dim int

Dimensionality of the latent space (number of components to keep)

20
mu float

Regularization parameter for numerical stability

0.5

Attributes:

Name Type Description
W ndarray of shape (n_samples, dim) or None

Learned transformation matrix. None before fitting.

X_train ndarray of shape (n_samples, n_features) or None

Training data stored for transformation. None before fitting.

Source code in pydmoo/response/tca_model.py
def __init__(self, kernel_type='rbf', kernel_param=1.0, dim=20, mu=0.5):
    # For the TCA parameters, we set the Gaussian kernel function to the default value and the expected dimensionality
    # was set to be 20. The value of μ was set to 0.5.
    self.kernel_type = kernel_type
    self.kernel_param = kernel_param
    self.dim = dim
    self.mu = mu
    self.W = None
    self.X_train = None

Functions

fit
fit(Xs, Xt)

Fit TCA model to source and target data.

Parameters:

Name Type Description Default
Xs ndarray of shape (ns_samples, n_features)

Source domain feature matrix

required
Xt ndarray of shape (nt_samples, n_features)

Target domain feature matrix

required

Returns:

Name Type Description
self object

Returns the instance itself.

Source code in pydmoo/response/tca_model.py
def fit(self, Xs, Xt):
    """Fit TCA model to source and target data.

    Parameters
    ----------
    Xs : ndarray of shape (ns_samples, n_features)
        Source domain feature matrix
    Xt : ndarray of shape (nt_samples, n_features)
        Target domain feature matrix

    Returns
    -------
    self : object
        Returns the instance itself.
    """
    # Stack source and target data vertically
    X = np.vstack((Xs, Xt))
    n = X.shape[0]  # Total number of samples
    ns, nt = Xs.shape[0], Xt.shape[0]  # Number of source/target samples

    # Compute kernel matrix K using selected kernel function
    K = self._kernel(X, X)
    self.X_train = X  # Store for transform phase

    # Construct MMD (Maximum Mean Discrepancy) matrix
    # This matrix encodes the distribution difference between domains
    M = np.zeros((n, n))
    M[:ns, :ns] = 1/(ns*ns)  # Source-source block
    M[ns:, ns:] = 1/(nt*nt)  # Target-target block
    M[:ns, ns:] = M[ns:, :ns] = -1/(ns*nt)  # Cross blocks

    # Centering matrix H = I - 1/n * 11^T
    # Projects data onto the space orthogonal to the vector of ones
    H = np.eye(n) - np.ones((n, n))/n

    # Solve generalized eigenvalue problem:
    # (K(M)K + μI)^-1 K(H)K w = λw
    # Using pseudo-inverse for numerical stability
    A = np.linalg.pinv(K @ M @ K + self.mu*np.eye(n)) @ K @ H @ K

    # Compute eigenvalues and eigenvectors
    # eigh returns eigenvalues in ascending order
    eigvals, eigvecs = eigh(A)

    # Select top 'dim' eigenvectors corresponding to largest eigenvalues
    # [::-1] reverses order to get descending eigenvalues
    self.W = eigvecs[:, np.argsort(eigvals)[-self.dim:][::-1]]

    return self
transform
transform(X)

Transform data using learned TCA components.

Parameters:

Name Type Description Default
X ndarray of shape (n_samples, n_features)

Data to transform

required

Returns:

Name Type Description
X_transformed ndarray of shape (n_samples, dim)

Data projected to the latent space

Raises:

Type Description
ValueError

If fit() hasn't been called before transform()

Source code in pydmoo/response/tca_model.py
def transform(self, X):
    """Transform data using learned TCA components.

    Parameters
    ----------
    X : ndarray of shape (n_samples, n_features)
        Data to transform

    Returns
    -------
    X_transformed : ndarray of shape (n_samples, dim)
        Data projected to the latent space

    Raises
    ------
    ValueError
        If fit() hasn't been called before transform()
    """
    if self.W is None:
        raise ValueError("Model not fitted yet. Call fit() first.")

    # Compute kernel between new data and training data
    K = self._kernel(X, self.X_train)

    # Project to latent space: X' = K * W
    return K @ self.W