Introduction:
Statsmodels is not that famous a library as scikit-learn (sklearn) is. But often a time for statistical analysis, you will want to use statsmodel packages. Also, it is one of the closest resemblance between R packages and python packages. I have been using it on and off for the last year and I find it difficult to remember all the errors, common mistakes, and their necessary solutions. This post is meant to serve as a small directory of statsmodels related errors and mistakes and their possible solutions.
model does not have attribute summary:
Let's say you are training some model with the following sort of code:
model = sm.Logit(Y,X)
And then this error arises, saying that model does not have an attribute summary. Also if you try model.predict(), you will get the error somewhat like 'model do not have the predict method'. The reason being that, once in statsmodels you fit the model, the trained model with all these attributes and methods, is returned as the output from the fit method. So the correction for all such related errors will be
model.fit()
print(model.summary())
predictor = model.fit()
and then you can use this predictor variable as your model object for further actions.So this is how to solve this specific issue.
prediction issue with statsmodels multinomial regression
If you work with scikit learn packages often, then you are habituated with the notion that on calling predict method, it prints only one prediction value par one sample point. But that is not the case with the statsmodel MNLogit. Statsmodel models, in case of classification, returns the probabilities of n classes. If you don't consider that, it will lead you to errors. The solution, in this case, is to take np.argmax() on that probabilities dataframe to find the predicted class. Sample use will be:
model = sm.MNLogit(Y,X)
predictor = model.fit()
probabs = predictor.predict(test_data)
predicted_class = np.argmax(probabs.to_numpy(),axis = 1)
Note that interesting fact here is that we use to_numpy, as the np.argmax function does not work on dataframe but on numpy n-dimensional arrays only. to_numpy is used to turn a dataframe into n-dimensional array.
Missing constant in regressions
In statsmodels, unlike scikit learn the constant is not fitted by default to the model. Therefore, often time, you will end up missing the constant in your regression formulas. If you want to add the constant manually, all you have to do is add a column of constant and then include that as a normal variable. It is similar to writing constant = constant*1 in the model equation.
Comments
Post a Comment