0
$\begingroup$

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
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)

#Doing a linear regression
lm=LinearRegression()
lm.fit(X_train,y_train)

#Predicting the height of Sons
y_test=lm.predict(X_test)
print(y_test)
$\endgroup$
2
$\begingroup$

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)
$\endgroup$
  • $\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 '19 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$ – Gyan Ranjan Sep 13 '19 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$ – Gyan Ranjan Sep 13 '19 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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