Search code examples
pythonimporterrorstatsmodels

cannot import name 'OLS' from 'statsmodels.formula.api'


I have this code:

from statsmodels.formula.api import OLS

The error is as follows:

ImportError: cannot import name 'OLS' from 'statsmodels.formula.api'

I tried updating statsmodels, but it does not work


Solution

  • The formula API always uses lower case since these are functions and not classes, and so you can use

    from statsmodels.formula.api import ols
    

    or the more canonical

    import statsmodels.formula.api as smf
    smf.ols("y ~ x", df)