I have built a classifier and I would like to save it for future use. The classifier includes different algorithms (logistic regression, naive bayes, svm):
X, y = tfidf(df, ngrams = 1)
X, y = under_sample.fit_resample(X, y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=40)
df_result = df_result.append(training_naive(X_train, X_test, y_train, y_test), ignore_index = True)
df_result = df_result.append(training_logreg(X_train, X_test, y_train, y_test), ignore_index = True)
df_result = df_result.append(training_svm(X_train, X_test, y_train, y_test), ignore_index = True)
this is the last step in my code, where I compare the different algorithm. training_svm/logreg and naive are functions. training_svm, for example, is defined as follows:
def training_svm(X_train_log, X_test_log, y_train_log, y_test_log):
folds = StratifiedKFold(n_splits = 3, shuffle = True, random_state = 40)
clf = svm.SVC(kernel='linear') # Linear Kernel
clf.fit(X_train_log, y_train_log)
res = pd.DataFrame(columns = ['Preprocessing', 'Model', 'Precision', 'Recall', 'F1-score', 'Accuracy'])
y_pred = clf.predict(X_test_log)
f1 = f1_score(y_pred, y_test_log, average = 'weighted')
pres = precision_score(y_pred, y_test_log, average = 'weighted')
rec = recall_score(y_pred, y_test_log, average = 'weighted')
acc = accuracy_score(y_pred, y_test_log)
res = res.append({'Model': f'SVM', 'Precision': pres,
'Recall': rec, 'F1-score': f1, 'Accuracy': acc}, ignore_index = True)
return res
Since I would like to use and test it with new data, I was wondering how to save it and re-use it. I would say I should do something like this
import pickle
# save
with open('model.pkl','wb') as f:
pickle.dump(clf,f)
# load
with open('model.pkl', 'rb') as f:
clf2 = pickle.load(f)
clf2.predict(X[0:1])
Can you please explain how to extend it to my project?