How to get slope of variables and intercept from a stored(serialized using python pickle) ML model

Viewed 101

I have a Python code snippet which loads stored ml model and predicts with new inputs.

mlModel = pickle.load(open('linear_model4.pickle','rb'))
request_body = request.body.decode('utf-8')

parsed_request = makePredictionInput(json.loads(request_body))
rb=json.loads(request_body)
print(parsed_request)


result = mlModel.predict(parsed_request)

It uses 5 inputs for prediction.

Is there a way to get the slopes and intercept from above loaded model sothat i can form the equation as

y = intercept + slope1*variable1+ slope2*variable2+ slope3*variable4+.....
1 Answers

I assume that your model is a linear model given the equation you want to reconstruct. mlModel.coef_ should be what you want. See this for example.

mlModel.intercept_ is the intercept.

coef_ are in the same order as the training data. For example if age is the third column in your training data, the coefficient for age is the third element in mlModel.coef_.

In your case you are only making predictions, but I assume that you know the meaning of the columns in the data you are trying to predict on. This should be the same order as in your training data.

Related