[Python] Scikit-Learn Classifier model returns all zeroes

Discussão em 'Python' iniciado por Stack, Junho 17, 2025.

  1. Stack

    Stack Membro Participativo

    So, I'm trying to train a RandomForestClassifier Model. However, when I train it, it gives me all zeroes. And, I really can't seem to understand why. The dataset is HUGE (closer to like 75,0000 rows), so, I am a bit lost. Here is the code. :

    import pandas as pd
    import matplotlib.pyplot as plt
    import numpy as np

    df = pd.read_csv("train.csv")
    X_train = df.iloc[:, 1:-1].values
    y_train = df.iloc[:, [-1]].values

    df = pd.read_csv("test.csv")
    X_test = df.iloc[:, 1:].values

    from sklearn.impute import SimpleImputer
    imputer = SimpleImputer(missing_values=np.nan, strategy = "most_frequent")
    imputer.fit(X_train[:, :])
    X_train[:, :] = imputer.transform(X_train[:, :])
    X_test[:, :] = imputer.transform(X_test[:, :])

    int_features = []
    categorical_features = []
    for i in range(len(X_train[0])) :
    if type(X_train[0]) == int or type(X_train[0]) == float :
    int_features.append(i)
    elif type(X_train[0]) == str :
    categorical_features.append(i)

    from sklearn.compose import ColumnTransformer
    from sklearn.preprocessing import OneHotEncoder
    ct_x = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), categorical_features)], remainder='passthrough')
    X_train = np.array(ct_x.fit_transform(X_train))
    X_test = np.array(ct_x.transform(X_test))

    ct_y = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])], remainder='passthrough', sparse_threshold=0)
    y_train = np.array(ct_y.fit_transform(y_train))

    from sklearn.preprocessing import StandardScaler
    sc = StandardScaler()
    X_train[:, int_features] = sc.fit_transform(X_train[:, int_features])
    X_test[:, int_features] = sc.transform(X_test[:, int_features])

    from sklearn.ensemble import RandomForestClassifier
    classifier = RandomForestClassifier(n_estimators=500, max_depth=25, random_state=42)
    classifier.fit(X_train, y_train)

    y_pred = classifier.predict(X_test)

    #print(y_pred)
    # Access the OneHotEncoder
    ohe = ct_y.named_transformers_['encoder']

    # Apply inverse_transform
    inverse_transformed_data = ohe.inverse_transform(y_pred)

    #print(inverse_transformed_data)
    #print(inverse_transformed_data)


    Basically, this model has to predict which fertilizer to use based on the data, ie, how good the soil is, etc. I onehotencoded the y_train, because, it containes all the ferilizer names that the model has to predict. However, y_pred just gives me all zeroes, and I don't know why!

    Thanks you for any helps or advices you can give.

    Continue reading...

Compartilhe esta Página