This example shows how to train a model first, then improve it by retraining with new data using incremental learning.
from sklearn.linear_model import SGDClassifier
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
# Initial training data
X_train, y_train = make_classification(n_samples=100, n_features=5, random_state=1)
# New data for retraining
X_new, y_new = make_classification(n_samples=20, n_features=5, random_state=2)
# Create model and train initially
model = SGDClassifier(max_iter=1000, tol=1e-3, random_state=42)
model.fit(X_train, y_train)
# Predict and check accuracy before retraining
y_pred_before = model.predict(X_new)
acc_before = accuracy_score(y_new, y_pred_before)
# Incremental retraining with new data
model.partial_fit(X_new, y_new, classes=[0,1])
# Predict and check accuracy after retraining
y_pred_after = model.predict(X_new)
acc_after = accuracy_score(y_new, y_pred_after)
print(f"Accuracy before retraining: {acc_before:.2f}")
print(f"Accuracy after retraining: {acc_after:.2f}")