Search code examples
python-3.xlinear-regressionsklearn-pandas

"fit() missing 1 required positional argument: 'y'" error


I have been attempting to use sklearn to create some test data for a linear regression model. The error I am getting is 'fit() missing 1 required positional argument: 'y''

from sklearn.model_selection import train_test_split

X = df[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
       'Avg. Area Number of Bedrooms', 'Area Population']]

y = df['Price']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=101)

from sklearn.linear_model import LinearRegression

lm = LinearRegression

lm.fit(X_train,y_train)

I have tried looking at this link 'https://stackoverflow.com/questions/35996970/typeerror-fit-missing-1-required-positional-argument-y' but I cannot fix it.


Solution

  • Try

    from sklearn.model_selection import train_test_split
    
    X = df[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
           'Avg. Area Number of Bedrooms', 'Area Population']]
    
    y = df['Price']
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=101)
    
    from sklearn.linear_model import LinearRegression
    
    lm = LinearRegression()
    
    lm.fit(X_train,y_train)
    

    you forgot () after lm = LinearRegression