[Python] Multinomial regression with statsmodels.formula.api is not working for me

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    Whenever I try to build a multinomial regression using the R-style formula approach in statsmodels in Python, I get ValueError: endog has evaluated to an array with multiple columns...

    Here's a minimal test case:

    import pandas as pd

    import statsmodels.formula.api as smf

    data = pd.DataFrame({'choice': ['A', 'B', 'A', 'C', 'B', 'C', 'A', 'B', 'C', 'A'], 'feature1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'feature2': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]})

    model = smf.mnlogit(formula='choice ~ feature1 + feature2', data=data).fit()


    This is with Python 3 and recent versions of pandas and statsmodels. I have tried various transformations to the y-variable (set it to type category, use a number instead, etc) but nothing has helped.

    Any advice appreciated - thank you

    Continue reading...

Compartilhe esta Página