<jupyter_start><jupyter_text>Saving and Loading Machine Learning Models1. **Finalizing our Model with pickle**2. **Finalizing our Model with Joblib**  1. Finalizing our model with pickle* Pickle is the standard way of serializing objects in python.* We can serialize our machine learning algorithms and save the serialized format to a file.* Later we can load this file to deserialize our model and use it to make new predictions.<jupyter_code># Save Model Using Pickle
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from pickle import dump
from pickle import load

filename = 'pima-indians-diabetes.data.csv'
names = ['preg','plas','pres','skin','test','mass','pedi','age','class']
dataframe = read_csv(filename,names=names)
array = dataframe.values
X = array[:,0:8]
Y = array[:,8]
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.33,random_state=None)

# fit the model
model = LogisticRegression()
model.fit(X,Y)

# save the model to disk
filename = 'finalized_pickle.sav'
dump(model,open(filename,'wb'))

# load the model from disk
loaded_model = load(open(filename,'rb'))
result = loaded_model.score(X_test,Y_test)
print(result)<jupyter_output>0.7401574803149606<jupyter_text>2. Finalizing our Model with Joblib<jupyter_code># Save Model Using joblib
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.externals.joblib import dump
from sklearn.externals.joblib import load

# save the model to disk
filename = 'finalized_joblib.sav'
dump(model,filename)

# load the model from disk
loaded_model = load(filename)
result = loaded_model.score(X_test,Y_test)
print(result)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09282934963868816, 'fraction_numerical': 0.01500833796553641, 'mean_word_length': 6.964601769911504, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '534377', 'license': 'MIT', 'n_tokens_mistral': 585, 'n_tokens_neox': 565, 'n_words': 173, 'path': 'Saving ML Models.ipynb', 'repo_name': 'kuluruvineeth/Machine-Learning-with-Python', 'size': '1799'}