jupyter nbconvert --to terminal dsml2/26.1_Self_Training.ipynb
MDN-118:08

26.1 Self Training

26.1_Self_Training.ipynb · 73 KB · scarica
In [1]:
from sklearn.datasets import load_iris
from sklearn.utils.random import sample_without_replacement

X, y = load_iris(return_X_y=True, as_frame=True)
rnd = sample_without_replacement(120,60)
y_ssl = y
y_ssl[rnd] = -1
In [4]:
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
plt.scatter(X['sepal length (cm)'], X['petal length (cm)'], c=y_ssl, cmap='Set1', alpha=1)
plt.show()
In [5]:
from sklearn.svm import SVC
from sklearn.semi_supervised import SelfTrainingClassifier

svc = SVC(probability=True, gamma="auto")
self_training_model = SelfTrainingClassifier(base_estimator=svc)
In [7]:
self_training_model.fit(X, y_ssl)
y_pred = self_training_model.predict(X)

plt.figure(figsize=(6, 3))
plt.scatter(X['sepal length (cm)'], X['petal length (cm)'], c=y_pred, alpha=1)
plt.show()
<Figure size 1000x500 with 0 Axes>