After training a machine learning model, we often need to save the model so that later we can load the model and use it without training it again. We can use the Python module pickle for that purpose.
Let’s say we are reading the Pima Indians Diabetes dataset. The dataset contains various predictor variables such as the number of pregnancies the patient has had, the BMI, insulin level, age, etc. A machine learning model can learn from the dataset and predict whether the patient has diabetes based on these predictor variables.
Now, let’s say we read the dataset and trained a machine learning model. And after that, we need to save the model for future use. We can use the following Python code for that purpose:
from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression import pandas from pickle import dump data = pandas.read_csv("diabetes.csv") D = data.values X = D[:, :-1] y = D[:, -1] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, shuffle=True, random_state=1) classifier = LogisticRegression(solver="liblinear") classifier.fit(X_train, y_train) dump(classifier, open("diabetes_classification.sav", "wb"))
Here, we are first reading the dataset and then, splitting the columns of the dataset into features and the target variable. The last column of the dataset contains the target variable. So, X here contains all the features, and y contains the target …






0 Comments