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

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

我一直在尝试使用 sklearn 为线性回归模型创建一些测试数据。我收到的错误是“fit() 缺少 1 个必需的位置参数:'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)

我试过查看此 link ' 但我无法修复它。

尝试

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)

你在 lm = LinearRegression

之后忘记了 ()

你最后忘记了()。代码应该是,

lm = LinearRegression()

而不是

lm = LinearRegression