3
$\begingroup$

Below is the simpler implementation of early stopping which i came across the book and wanted to try it.

# Implement SGD Classifier

sgd_clf =   SGDClassifier(random_state=42,
                          warm_start=True,
                          n_iter=1,
                          learning_rate='constant',
                          eta0=0.0005)

minimum_val_error = float('inf')
best_epoch = None
best_model = None

for epoch in range(1000):
  sgd_clf.fit(X_train_scaled,y_train)
  predictions = sgd_clf.predict(X_val_scaled)
  error = mean_squared_error(y_val,predictions)
  if error < minimum_val_error:
    minimum_val_error = error
    best_epoch = epoch
    best_model = clone(sgd_clf)

Once the above snippet is executed, best model and best epoch are stored in variable best_model and best_epoch.So, to test the best_model, i ran the below statement.

y_test_predictions = best_model.predict(X_test)

But then i came across the error This SGDClassifier instance is not fitted yet

Any hints on how to solve this, would be greatly helpful. Thanks

$\endgroup$
2
$\begingroup$

It is because clone will only copy the estimator with the same parameters, but not with the attached data. So it results a new estimator that has not been fit on the data. Hence, you couldn't use it to make prediction.

Instead of clone, you can use either pickle or joblib.

1. pickle

import pickle
...

for epoch in range(1000):
    ...
    if error < minimum_val_error:
        best_model = pickle.dumps(sgd_clf)

Later if you want to use the stored model:

sgd_clf2 = pickle.loads(best_model)
y_test_predictions = sgd_clf2.predict(X_test)

2. joblib

You can also use joblib, and store the model to the disk.

from sklearn.externals import joblib
...

joblib.dump(sgd_clf, 'filename.joblib') 

To use the stored model

clf = joblib.load('filename.joblib')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.