Semi-supervised ML Introduction

Semi-supervised machine learning is a type of machine learning where the model learns from a combination of labeled and unlabeled data to make predictions or find patterns.

In traditional supervised learning, the model is given labeled data, which means every data point is accompanied by its corresponding label or category. The model uses this labeled data to learn and make predictions.

In contrast, semi-supervised learning takes advantage of both labeled and unlabeled data. Labeled data provides explicit information about the correct answer or category, while unlabeled data doesn't have this explicit information.

The idea behind semi-supervised learning is that by using a small set of labeled data along with a larger set of unlabeled data, the model can learn more effectively. The model can identify patterns and relationships in the unlabeled data and use them to improve its predictions.

To put it simply, think of semi-supervised learning as a combination of traditional supervised learning with labeled data and unsupervised learning, where the model discovers patterns in unlabeled data. This combination allows the model to make more accurate predictions by leveraging the additional information from the unlabeled data.

Python Example

from sklearn.semi_supervised import LabelPropagation
from sklearn.datasets import make_classification

# Generate a synthetic dataset with 500 labeled samples and 5000 unlabeled samples
X, y = make_classification(n_samples=5500, n_features=10, n_informative=5, n_classes=2,
                n_clusters_per_class=1, weights=[0.1, 0.9], random_state=42)

# Split the data into labeled and unlabeled sets
X_labeled, X_unlabeled = X[:500], X[500:]
y_labeled = y[:500]

# Create a semi-supervised learning model
model = LabelPropagation()

# Fit the model with the labeled and unlabeled data
model.fit(X, y)

# Make predictions on the unlabeled data
y_pred = model.predict(X_unlabeled)

# Evaluate the accuracy of the predictions (assuming you have the ground truth labels for the unlabeled data)
accuracy = (y_pred == y[500:]).mean()
print("Accuracy:", accuracy)

Results:

Accuracy: 1.0

In this example, we use the make_classification function from the sklearn.datasets module to generate a synthetic dataset with 500 labeled samples and 5000 unlabeled samples. We split the data into X_labeled and X_unlabeled for features and y_labeled for labels.

Next, we create a LabelPropagation model from the sklearn.semi_supervised module. We fit the model with the labeled and unlabeled data using the fit method.

After fitting the model, we make predictions on the unlabeled data using the predict method. Finally, we evaluate the accuracy of the predictions by comparing them with the ground truth labels for the unlabeled data.

Please note that the accuracy calculation assumes that you have access to the ground truth labels for the unlabeled data, which is not always the case in real-world scenarios.