Comparison semi-supervised models


from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.semi_supervised import LabelPropagation
from sklearn.semi_supervised import LabelSpreading
from sklearn.semi_supervised import SelfTrainingClassifier
from sklearn.svm import SVC

# Load the Iris dataset
iris = load_iris()
X, y = iris.data, iris.target

# Create a semi-supervised learning setting (only 20 labeled samples)
X_labeled, X_unlabeled, y_labeled, _ = train_test_split(X, y, train_size=20, stratify=y)

# Function to evaluate the classifier's performance
def evaluate_classifier(clf, X_test, y_test):
    y_pred = clf.predict(X_test)
    return accuracy_score(y_test, y_pred)

# Define classifiers to compare
label_propagation_clf = LabelPropagation()
label_spreading_clf = LabelSpreading()
svc = SVC(probability=True, gamma="auto")
self_training_clf = SelfTrainingClassifier(svc)

# Train classifiers using labeled and unlabeled data
label_propagation_clf.fit(X_labeled, y_labeled)
label_spreading_clf.fit(X_labeled, y_labeled)
self_training_clf.fit(X, y)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Evaluate the classifiers on the test set
label_propagation_acc = evaluate_classifier(label_propagation_clf, X_test, y_test)
label_spreading_acc = evaluate_classifier(label_spreading_clf, X_test, y_test)
self_training_acc = evaluate_classifier(self_training_clf, X_test, y_test)

# Print the results
print("Label Propagation Accuracy:", label_propagation_acc)
print("Label Spreading Accuracy:", label_spreading_acc)
print("Self-Training Accuracy:", self_training_acc)
      

Results:


Label Propagation Accuracy: 0.9777777777777777
Label Spreading Accuracy: 0.9777777777777777
Self-Training Accuracy: 1.0
      

Explanations:

Label Propagation and Label Spreading: These are traditional semi-supervised algorithms that use the labeled data to propagate labels to unlabeled data points. They assume smoothness in the data distribution. They are straightforward to use and often perform well with limited labeled data.

Self-Training: This method uses a classifier to label the unlabeled data and then includes the confident predictions in the labeled data. It iteratively trains the model with this expanded dataset. Self-training can work well when the classifier is relatively confident in its predictions.

Choosing the most appropriate algorithm depends on several factors, such as the amount of labeled data available, the quality of the labeled data, and the characteristics of the dataset. Since the Iris dataset is relatively small and simple, traditional semi-supervised methods like Label Propagation and Label Spreading might perform well with the limited labeled data available. However, this is not a definitive conclusion, and empirical experimentation is necessary to determine the best-performing algorithm for this specific dataset.