I just trained my first model in Python 3.7/scikitlearn (Linear Regression) (well I copied most of the code but its something ^^).

Now I want to actually Use the model. Specifically its about sons heights incorrelating to their fathers. So I now want to enter a new Father-height and get a predictions for its sons height.

How could something like this look like?

I read about "Pickle" to save a model and use it later, seems awsome but how would I use such a saved model?

If anybody can give me a simple answer or even just a link to atutorial would be great. Below is a piece of "my" code just for some context.

#Spliting the data into test and train data

#Doing a linear regression

#Predicting the height of Sons

1 Answer 1


You have your model saved as the variable lm. You can use the lm.predict(X_test) for any other test scenario. Note that your X_test should be similar to your X_train meaning if you have transformations made on your X_train, you need to do similar transformation on X_test too. You can use pickle in the following way

import pickle
filename = 'model.pckl'
pickle.dump(model, open(filename, 'wb'))

#To load the model from disk, use this
model = pickle.load(open(filename, 'rb'))
prediction = model.predict(X_test)
  • $\begingroup$ Awsome, Thanks. Quiet easy actually ^^. I cant abvote yet but I "accepted" your anwer. (The link is perfect, very easy to understand) I assume that Pickle is the standard next to anybody uses for storing a model? just to get started with the right tools ^^ $\endgroup$
    – G.M
    Sep 6, 2019 at 8:05
  • $\begingroup$ Yes. One other way to go is to dump the model into a json and also dump the trained weights . While loading the model, you need to then define the model architecture and also load the weights after compiling. You can find lot of links explaining it. $\endgroup$ Sep 13, 2019 at 7:27
  • $\begingroup$ machinelearningmastery.com/save-load-keras-deep-learning-models Have a look here for the json way of saving the model $\endgroup$ Sep 13, 2019 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.