[Python] Using pd.from_dummies to get the values in target variable back (after one-hot...

Discussão em 'Python' iniciado por Stack, Julho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm participating in the curent Kaggle playground series and I decided to use a neural network for the binary classification problem because I am practicing making simple ones. I converted the target variable 'Personality' into 1s and 0s using OHE.

    train2 = pd.get_dummies(train, columns=['Personality'], drop_first=True, dtype=int)


    Then I used the tensorflow-keras library. The output was in decimals (because it's probability) and so I rounded up using:

    pred = np.round(pred)



    then converted the result to int64 and a dataframe with this code:

    df = pd.DataFrame(pred)
    pred_df = df.astype('int64')
    pred_df.head()


    but when I tried to revert the predicted values back to Introvert and Extrovert,

    final = pd.from_dummies(pred_df)


    I got an error.

    This is the full error message ->
    ---------------------------------------------------------------------------
    ValueError Traceback (most recent call last)
    /tmp/ipykernel_635/341381991.py in <cell line: 0>()
    1 #Revert
    ----> 2 final = pd.from_dummies(pred_df)

    /usr/local/lib/python3.11/dist-packages/pandas/core/reshape/encoding.py in from_dummies(data, sep, default_category)
    550 cats.append(default_category[prefix])
    551 else:
    --> 552 raise ValueError(
    553 "Dummy DataFrame contains unassigned value(s); "
    554 f"First instance in row: {assigned.idxmin()}"

    ValueError: Dummy DataFrame contains unassigned value(s); First instance in row: 0


    I'm not sure how to solve this or where I went wrong so that's why I put in as much code as I could. If reverting is impossible and just using a map function then an 'if 1 then Introvert' statement at the end is more plausible then I am willing to do that.

    Thank you.

    Continue reading...

Compartilhe esta Página