Linear Regression on Pandas DataFrame using Sklearn ( IndexError: tuple index out of range)

后端 未结 5 2215
孤街浪徒
孤街浪徒 2020-12-08 15:14

I\'m new to Python and trying to perform linear regression using sklearn on a pandas dataframe. This is what I did:

data = pd.read_csv(\'xxxx.csv\')
<         


        
5条回答
  •  暖寄归人
    2020-12-08 15:36

    I post an answer that addresses exactly the error that you got:

    IndexError: tuple index out of range

    Scikit-learn expects 2D inputs. Just reshape the X and Y.

    Replace:

    X=data['c1'].values # this  has shape (XXX, ) - It's 1D
    Y=data['c2'].values # this  has shape (XXX, ) - It's 1D
    linear_model.LinearRegression().fit(X,Y)
    

    with

    X=data['c1'].values.reshape(-1,1) # this  has shape (XXX, 1) - it's 2D
    Y=data['c2'].values.reshape(-1,1) # this  has shape (XXX, 1) - it's 2D
    linear_model.LinearRegression().fit(X,Y)
    

提交回复
热议问题