9951 explained code solutions for 126 technologies


python-scikit-learnHow to load ML model from file


import pickle
from sklearn import datasets, linear_model

X, y = datasets.load_diabetes(return_X_y=True)
model = pickle.load(open('/tmp/model.ml', 'rb'))

y_pred = model.predict(X)ctrl + c
from sklearn import

import module from scikit-learn

datasets.load_diabetes

loads sample diabetes database

pickle.load(

loads data from given file

open(

opens file descriptor (for reading in our case)

/tmp/model.ml

path to file to load model from

.predict(

predict target variable based on given features dataset


Usage example

import pickle
from sklearn import datasets, linear_model

X, y = datasets.load_diabetes(return_X_y=True)
model = pickle.load(open('/tmp/model.ml', 'rb'))

y_pred = model.predict(X)

print(model.score(X, y))
output
0.506799948108825