kalelinear is a Python library for learning harmonized or individualized models from multi-source/multi-view data in linear or reproducing kernel Hilbert spaces (RKHS). It provides NumPy-based methods for leveraging related data distributions and structural assumptions, including transfer learning, domain adaptation, manifold regularization, and group-aware learning, through a scikit-learn style API.
The package is part of the PyKale ecosystem and focuses on linear and kernel methods for data characterized by covariates (e.g., domain labels, group labels, side information), unlabeled target samples, or tensor structures.
- Transformer models for learning feature embeddings:
- Estimator models for classification:
- NumPy-compatible inputs and outputs.
- scikit-learn style
fit,transform,predict,fit_transform, andfit_predictworkflows where applicable. - Optional covariate encoding for categorical domain or group labels.
Install the released package from PyPI:
pip install kalelinearInstall from a local checkout for development:
pip install -e ".[dev]"kalelinear requires Python 3.10 or later. Core dependencies include:
import numpy as np
from kalelinear.transformer import TCA
X = np.array(
[
[-2.0, -1.8],
[-1.8, -2.1],
[1.9, 1.7],
[2.1, 2.0],
[-1.4, -1.2],
[-1.2, -1.1],
[1.2, 1.1],
[1.4, 1.3],
]
)
domain_labels = np.array([0, 0, 0, 0, 1, 1, 1, 1])
transformer = TCA(n_components=2)
z = transformer.fit_transform(X, covariates=domain_labels, target_covariate=1)
z_source = z[domain_labels == 0]
z_target = z[domain_labels == 1]TCA, JDA, and BDA take domain labels through covariates. They do not accept
separate source and target arrays; stack samples into one array and use
target_covariate to identify the target domain.
import numpy as np
from kalelinear.transformer import MIDA
x = np.random.default_rng(0).normal(size=(8, 4))
y = np.array([0, 0, 1, 1, 0, 0, 1, 1])
domains = np.array(["source", "source", "source", "source", "target", "target", "target", "target"])
transformer = MIDA(n_components=2, covariate_encoder="onehot")
z = transformer.fit_transform(x, y=y, covariates=domains)For ARSVM and ARRLS, pass all source and target samples in x, labels for the
source samples in y, and a covariate vector identifying the target domain.
import numpy as np
from kalelinear.estimator import ARSVM
x = np.array(
[
[-2.2, -1.9],
[-1.9, -2.1],
[1.8, 2.1],
[2.0, 1.9],
[-1.4, -1.2],
[-1.1, -1.3],
[1.3, 1.1],
[1.5, 1.2],
]
)
source_labels = np.array([0, 0, 1, 1])
domains = np.array([0, 0, 0, 0, 1, 1, 1, 1])
x_target = x[domains == 1]
clf = ARSVM()
clf.fit(x, source_labels, covariates=domains, target_covariate=1)
y_pred = clf.predict(x_target)LapSVM and LapRLS can use labeled source samples together with unlabeled target samples. The labels array may contain only the labeled source examples.
import numpy as np
from kalelinear.estimator import LapSVM
x_source = np.array([[-2.0, -1.8], [-1.8, -2.1], [1.9, 1.7], [2.1, 2.0]])
ys = np.array([0, 0, 1, 1])
x_target = np.array([[-1.4, -1.2], [-1.2, -1.1], [1.2, 1.1], [1.4, 1.3]])
x_train = np.vstack((x_source, x_target))
clf = LapSVM(kernel="linear")
clf.fit(x_train, ys)
y_pred = clf.predict(x_target)from kalelinear.transformer import BDA, JDA, MIDA, MPCA, TCA
from kalelinear.estimator import ARRLS, ARSVM, CoIRLS, CoIRSVM, GSDA, LapRLS, LapSVMFrom the root of the repository, run the following commands in your terminal:
-
Install pre-commit hooks (only required once):
pre-commit install
-
Run pre-commit checks for code style and formatting on all files:
pre-commit run --all-files
-
Run test cases to verify functionality:
pytest
-
Build the documentation:
pip install -r docs/requirements.txt sphinx-build -b html docs/source docs/build/html
[1] Lu, H., Plataniotis, K.N. and Venetsanopoulos, A.N., 2008. MPCA: Multilinear principal component analysis of tensor objects. IEEE Transactions on Neural Networks, 19(1), pp.18-39.
[2] Pan, S.J., Tsang, I.W., Kwok, J.T. and Yang, Q., 2011. Domain adaptation via transfer component analysis. IEEE Transactions on Neural Networks, 22(2), p.199-210.
[3] Long, M., Wang, J., Ding, G., Sun, J. and Yu, P.S., 2013. Transfer feature learning with joint distribution adaptation. In Proceedings of the IEEE International Conference on Computer Vision (pp. 2200-2207).
[4] Wang, J., Chen, Y., Hao, S., Feng, W. and Shen, Z., 2017, November. Balanced distribution adaptation for transfer learning. In 2017 IEEE International Conference on Data Mining (ICDM) (pp. 1129-1134). IEEE.
[5] Yan, K., Kou, L. and Zhang, D., 2017. Learning domain-invariant subspace using domain features and independence maximization. IEEE Transactions on Cybernetics, 48(1), pp.288-299.
[6] Belkin, M., Niyogi, P. and Sindhwani, V., 2006. Manifold regularization: A geometric framework for learning from labeled and unlabeled examples. Journal of Machine Learning Research, 7(11).
[7] Long, M., Wang, J., Ding, G., Pan, S.J. and Yu, P.S., 2013. Adaptation regularization: A general framework for transfer learning. IEEE Transactions on Knowledge and Data Engineering, 26(5), pp.1076-1089.
[8] Zhou, S., Li, W., Cox, C. and Lu, H., 2020, April. Side information dependence as a regularizer for analyzing human brain conditions across cognitive experiments. In Proceedings of the AAAI Conference on Artificial Intelligence (Vol. 34, No. 04, pp. 6957-6964).
[9] Zhou, S., 2022. Interpretable Domain-Aware Learning for Neuroimage Classification (Doctoral dissertation, University of Sheffield).
[10] Zhou, S., Luo, J., Jiang, Y., Wang, H., Lu, H. and Gong, G., 2025. Group-specific discriminant analysis enhances detection of sex differences in brain functional network lateralization. GigaScience, 14, p.giaf082.
- POT: Python Optimal Transport
- Everything about Transfer Learning
- ADA: Another Domain Adaptation library
- Domain Adaptation and Transfer Learning Repositories
- Library of transfer learners and domain-adaptive classifiers
- domain-adaptation-toolbox
- Domain-Adaptations
kalelinear is released under the MIT License. See LICENSE for details.
