[Python] Error in my_regression.fit(X_train, y_train) Expected a 2-dimensional container but...

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

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

    my_data2 = pd.read_csv("heart.csv")

    type(my_data2)

    x = my_data2["trestbps"]
    y = my_data2["chol"]

    plt.xlabel("Resting Blood Pressure")
    plt.ylabel("Cholesterol")

    #Create and Display the Plot

    plt.plot(x,y,"*")
    plt.show()

    from sklearn.model_selection import train_test_split

    X_train, X_test, y_train, y_test = train_test_split(x,y, test_size = 0.20, random_state = 0)

    from sklearn.linear_model import LinearRegression as LNR
    from sklearn.metrics import r2_score
    type(X_train)

    my_regression = LNR()
    my_regression.fit(X_train, y_train) #HERE


    I got "Expected a 2-dimensional container but got <class 'pandas.core.series.Series'> instead. Pass a DataFrame containing a single row (i.e. single sample) or a single column (i.e. single feature) instead."

    Continue reading...

Compartilhe esta Página