0
$\begingroup$

I trained and saved a model that should predict a sons hight based on his fathers height. I then saved the model to Pickle. I can now load the model and want to use it but unfortunately a second variable is demanded from me (besides the height of the father) I think I did something wrong when training the model?

I will post the part of the code wher I think the error is in, please ask if you need more.

#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)

#testing the model with an example value
TestValue = 65.0
filename = 'Father_Son_Height_Model.pckl'
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.predict(TestValue)
print(result)

The error message says:

ValueError: Expected 2D array, got scalar array instead:

array=65.0.

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Thanks alot in advance.

$\endgroup$
2
$\begingroup$

You need to use loaded_model.predict(TestValue), not loaded_model.score(TestValue). The latter is for evaluating the models accuracy, and you would also need to pass the true height of the son, which is the y value it's asking for.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks, makes sense, I changed it and the new error (edited in question) appears. Did I train the model wrong or something? Why is he expecting a array? $\endgroup$ – G.M Sep 20 '19 at 7:23
  • 1
    $\begingroup$ @G.M this is scikit-learn being a bit annoying. It needs an array in a specific shape or it won't work. Try loaded_model.predict(np.array(TestValue).reshape(-1, 1)). skl is a great package but it could be more robust to this kind of thing. Make sure to include import numpy as np somewhere $\endgroup$ – Dan Scally Sep 20 '19 at 7:29
  • $\begingroup$ Jesus Christ its working, i cant believe it :D I feel like Dr. Frankenstein now, thanks alot :D I guess this isnt necessary when there is more then one arguement given for testing? so for example if I would have height of father and mother since it would be an arrray already. $\endgroup$ – G.M Sep 20 '19 at 7:38
  • 1
    $\begingroup$ @G.M You're welcome. Yeah exactly; normally you'd be predicting against an array of multiple rows with multiple features and you'd not have hit that second error. $\endgroup$ – Dan Scally Sep 20 '19 at 7:42
  • $\begingroup$ You right I think scikit learn should consider that, it may be rare in the real world but for a learner I would assume it is commen issue to run into. And given python it I would assume its easyly avoidable.... But who cares, I made a machine lerning thingy :D $\endgroup$ – G.M Sep 20 '19 at 7:45

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.