Rodney
Rodney

Reputation: 171

In sklearn regression, is there a command to return residuals for all records?

I know this is an elementary question, but I'm not a python programmer. I have an app that is using the sklearn kit to run regressions on a python server.

Is there a simple command which will return the predictions or the residuals for each and every data record in the sample?

Upvotes: 17

Views: 29562

Answers (2)

Bane
Bane

Reputation: 504

In sklearn to get predictions use .predict(x)

modelname.fit(xtrain, ytrain)
prediction = modelname.predict(x_test)
residual = (y_test - prediction)

If you are using an OLS stats model

OLS_model = sm.OLS(y,x).fit()  # training the model
predicted_values = OLS_model.predict()  # predicted values
residual_values = OLS_model.resid # residual values

Upvotes: 27

Amadeus
Amadeus

Reputation: 355

One option is to use fit() to get predictions and residual is simply the difference between the actual value and predictions.

Upvotes: 4

Related Questions