sslearn
Semi-Supervised Learning Library (sslearn)

The sslearn library is a Python package for machine learning over Semi-supervised datasets. It is an extension of scikit-learn.
Installation
Dependencies
- joblib >= 1.2.0
- numpy >= 1.23.3
- pandas >= 1.4.3
- scikit_learn >= 1.2.0
- scipy >= 1.10.1
- statsmodels >= 0.13.2
- pytest = 7.2.0 (only for testing)
pip installation
It can be installed using Pypi:
pip install sslearn
Citing
@article{sslearn2025garrido,
title = {SSLearn: A Semi-Supervised Learning library for Python},
journal = {SoftwareX},
volume = {29},
pages = {102024},
year = {2025},
issn = {2352-7110},
doi = {https://doi.org/10.1016/j.softx.2024.102024},
author = {José L. Garrido-Labrador and Jesús M. Maudes-Raedo and Juan J. Rodríguez and César I. García-Osorio},
}
Fundings
The research carried out for the development of this software has been partially funded by the Junta de Castilla y León (project BU055P20), by the Ministry of Science and Innovation of Spain (projects PID2020-119894GB-I00 and TED 2021-129485B-C43) and by the project AIM-LAC (EP/S023992 /1). The author has been a beneficiary of the predoctoral scholarship from the Ministry of Education of the Junta de Castilla y León EDU/875/2021.
Examples
In this page we provide some code examples to show how to use the library. Also, exists a Jupyter Notebook with the same examples to run in Google Colab.
Datasets manipulation
SSLearn include tools for loading csv and dat (KEEL) datasets. Also, it provides a function to generate a semi-supervised dataset from a labeled dataset.
Load a CSV or a DAT (KEEL) dataset
from sslearn.datasets import read_csv, read_keel
# The CSV file must have the target column in the last position by default.
X_iris, y_iris = read_csv('iris.csv', format='numpy') # Format can be 'numpy' or 'pandas'
# The DAT file must have the target column in the first position by default. Also, the instances with class "unlabeled" will change to -1.
X_iris, y_iris = read_keel('iris.dat', format='numpy')
Generate a unsupervised dataset from a labeled dataset
To test the semi-supervised algorithms, we need to generate a dataset with some instances without labels. The library provides a function to do this.
Exists two ways to generate the dataset:
- Randomly select some instances to be unlabeled giving a percentage of labeled instances.
- Generate folds where one fold is the labeled dataset and the others are unlabeled.
from sslearn.model_selection import artificial_ssl_dataset
# Using the previous dataset "X_iris" and "y_iris"
X_train, y_train, X_u, y_y = artificial_ssl_dataset(X_iris, y_iris, label_rate=0.2, random_state=42) # 80% of the instances are unlabeled
# or if you want also get the indexes of the instances
X_train, y_train, X_u, y_y, idx_label, idx_u = artificial_ssl_dataset(X_iris, y_iris, label_rate=0.2, random_state=42, indexes=True)
artificial_ssl_dataset supports stratify and shuffle as it works in train_test_split from sklearn.model_selection. Also, a minimum number of instances per class can be set with the parameter force_minimum.
The other way to generate the dataset is using the StratifiedKFoldSS class.
from sslearn.model_selection import StratifiedKFoldSS
skss = StratifiedKFoldSS(n_splits=10, shuffle=True, random_state=42) # 10 folds, that implies 10% of the instances are labeled
for X_train, y_train, label_indices, unlabel_indices in skss.split(X_iris, y_iris):
# It works like the StratifiedKFold from sklearn.model_selection
pass # Here you can use the labeled and unlabeled instances
X_train and y_train are the set ready to be used in a semi-supervised algorithm, with the "-1" in the target column for the unlabeled instances.
Wrappers
The wrappers are the most widely used algorithms in the semi-supervised learning field. The library includes the most popular ones. The algorithms included are:
SelfTraining: Self-training algorithm for one classifier.Setred: Self-training with data amendment.CoTraining: The co-training algorithm for two views, supports multi-learning and requires two views.Rasco: Random subspace co-training, supports multi-learning.RelRasco: Relevant random subspace co-training, supports multi-learning.DemocraticCoLearning: Democratic co-learning algorithm, only for multi-learning.CoForest: Co-training version of Random Forest.CoTrainingByCommittee: Co-training by committee algorithm, a.k.a. CoBagging or CoBoosting depending on the base classifier in the literature.TriTraining: Tri-training algorithm, supports multi-learning.DeTriTraining: Tri-training with data amendment, supports multi-learning.
The wrappers follow the same structure as the classifiers in scikit-learn. Use fit to train the model and predict to predict the target column. Also, the score method is available to get the accuracy of the model.
# Using the previous dataset "X_train" and "y_train"
from sslearn.wrapper import CoForest
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn
# Load dataset
X_iris_train, y_iris_train, X_iris_U, y_iris_U = artificial_ssl_dataset(X_iris, y_iris, label_rate=0.1, random_state=42)
# Create the wrapper
coforest = CoForest(base_estimator=DecisionTreeClassifier(random_state=10), random_state=10)
# Train
coforest.fir(X_iris_train, y_iris_train)
# Predict
y_pred = coforest.predict(X_iris_U)
# Score
accuracy = accuracy_score(y_iris_U, y_pred)
Each wrapper has its parameters, some of them need different views, have specific parameters, or need a kind of base classifiers. The documentation of each wrapper explains the parameters and the requirements and provides examples of how to use them.
The wrappers that supports multi-learning can recive differents algorithms as base classifiers. The base classifiers must be a list of classifiers.
from sslearn.wrapper import DemocraticCoLearning
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
# Load dataset
X_iris_train, y_iris_train, X_iris_U, y_iris_U = artificial_ssl_dataset(X_iris, y_iris, label_rate=0.1, random_state=42)
# Create the wrapper
democratic = DemocraticCoLearning(base_estimator=[DecisionTreeClassifier(random_state=10), KNeighborsClassifier(), GaussianNB()], random_state=10)
# Train
democratic.fit(X_iris_train, y_iris_train)
The multiview adapters
CoTraining is the only wrapper that supports different views. But, the library provides an adapter to provide a different view to the other wrappers. The adapters are in the sslearn.subview module. Supports classification and regression problems.
That adapter create a subview for the dataset, and when the wrapper train the base classifiers, each will be trained with the view defined in the adapter. It supports three modes of create subviews:
regex: The columns that match the regex will be in the subview.columns: The columns that are in the list will be in the subview.include: The columns whose names are in the list will be in the subview.
from sslearn.subview import SubViewClassifier
from sklearn.tree import DecisionTreeClassifier
# Load dataset
X_breast, y_breast = read_csv('breast-cancer.csv', format='numpy')
X_breast_train, y_breast_train, X_breast_U, y_breast_U = artificial_ssl_dataset(X_breast, y_breast, label_rate=0.1, random_state=42)
# Create the the adapter
subview1 = SubViewClassifier(DecisionTreeClassifier(random_state=10), "breast", mode='include') # The columns `breast` and `breast-quad`
subview2 = SubViewClassifier(DecisionTreeClassifier(random_state=10), ["age", "menopause", "irradiant"], mode='columns') # The columns `age`, `menopause`, and `irradiant`
subview3 = SubViewClassifier(DecisionTreeClassifier(random_state=10), ".*", mode='regex') # All the columns
# Create a TriTraining wrapper with the subviews
tritraining = TriTraining(base_estimator=[subview1, subview2, subview3], random_state=10)
The wrappers that support multi-learning can be used with the subview adapters. The adapters are in the sslearn.subview module.
Comparison example
A comparison example is available in the Jupyter Notebook. Here is a snippet of the code with the dataset already loaded.
The propose of this code is compare the accuracy of the algorithms in a semi-supervised dataset. It is used a 10-fold cross-validation to get the accuracy of each algorithm, and with the 10%, 20%, 30%, and 40% of the instances labeled.
# The dataset is already loaded, the models are built and the results objects have been created.
# First statified k-fold
skf = StratifiedKFold(n_splits=10)
for train_index, test_index in skf.split(X, y):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
for i, lr in enumerate([0.1, 0.2, 0.3, 0.4]):
# Create the semi-supervised dataset
X_ss, y_ss, _, _ = artificial_ssl_dataset(X_train, y_train, lr, 1)
for name, model in models.items():
# Fit the model
model.fit(X_ss, y_ss)
# Score the results and save it
results[name][i].append(model.score(X_test, y_test))
The average results are:
| 10% | 20% | 30% | 40% | |
|---|---|---|---|---|
| Self-Training | 89.99% | 90.52% | 91.05% | 89.81% |
| Setred | 88.76% | 90.86% | 90.86% | 91.04% |
| Co-Training | 90.69% | 92.27% | 91.75% | 91.75% |
| Co-Training by Committee | 91.22% | 92.62% | 91.75% | 91.75% |
| Democratic Co-Learning | 91.92% | 92.80% | 93.68% | 94.20% |
| RASCO | 90.86% | 91.74% | 94.38% | 93.33% |
| RelRASCO | 90.87% | 93.15% | 92.45% | 93.50% |
| CoForest | 91.39% | 92.80% | 92.63% | 92.45% |
| TriTraining | 91.56% | 90.68% | 91.04% | 91.75% |
| DeTriTraining | 85.24% | 85.06% | 85.06% | 85.24% |
Restricted set classification
The RSC algorithms support datasets with pairwise constraints. The library provides the WhoIsWhoClassifier, the feature_fusion and probability_fusion methods.
WhoIsWhoClassifier is a wrapper that uses the RSC algorithms and supports only cannot-link constraints. The feature_fusion and probability_fusion methods supports both constraints but only in prediction time. All algorithms are in the sslearn.restriced module.
The complete example is avaliable in the Jupyter Notebook. Here is a snippet of the code with the dataset and the constraints already loaded.
wiw = WhoIsWhoClassifier(base_estimator=DecisionTreeClassifier(random_state=10),
method="hungarian") # The method can ben `hungarian` and `greedy`
wiw.fit(X_train, y_train, cannot_link_train)
y_predicted = wiw.predict(X_test, cannot_link_test)
For WhoIsWhoClassifier the cannot-link constraints must be an array that the size is the number of instances in the dataset. Each index is mapped to the instance in the dataset. If two instances or more cannot-link the value must be the same in the array.
For the feature_fusion and probability_fusion methods, the constraints must be in dictionary format. The values are the instances that cannot-link or must-link. The keys represent the group of instances, but normaly is the first instance in the group (to ensure differents groups not have the same key). That methods requires a pre-trained model.
estimator = DecisionTreeClassifier(random_state=10)
estimator.fit(X_train, y_train)
y_predicted_f = feature_fusion(estimator, X_test, must_link_test_dict, cannot_link_test_dict)
y_predicted_p = probability_fusion(estimator, X_test, must_link_test_dict, cannot_link_test_dict)
feature_fusion combine the features of the instances that must-link replacing the features for the average of the features in the group. With probability_fusion the post-probabilities of the instances that must-link are averaged. In both cases it uses the hungarian algorithm to solve the cannot-link constraints.
1# Open README.md and added to __doc__ for 2import os 3if os.path.exists("../README.md"): 4 with open("../README.md", "r") as f: 5 __doc__ = f.read() 6elif os.path.exists("README.md"): 7 with open("README.md", "r") as f: 8 __doc__ = f.read() 9else: 10 __doc__ = "Semi-Supervised Learning (SSL) is a Python package that provides tools to train and evaluate semi-supervised learning models." 11 12__doc__ = __doc__ + "\n.. include:: ../../docs/examples.md\n" 13 14__version__='1.1.0' 15__AUTHOR__="José Luis Garrido-Labrador" # Author of the package 16__AUTHOR_EMAIL__="jlgarrido@ubu.es" # Author's email 17__URL__="https://pypi.org/project/sslearn/"