“ fit()缺少1个必需的位置参数:'y'”错误

时间:2020-06-22 17:59:59

标签: python-3.x linear-regression sklearn-pandas

我一直在尝试使用sklearn为线性回归模型创建一些测试数据。我得到的错误是'fit()缺少1个必需的位置参数:'y'

(remove-them2 '(1 2 3 4 5 6 7) '(1 3 5))
=> '(2 4 6 7)

我尝试查看此链接'https://stackoverflow.com/questions/35996970/typeerror-fit-missing-1-required-positional-argument-y',但无法修复。

2 个答案:

答案 0 :(得分:1)

尝试

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

答案 1 :(得分:-1)

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

lm = LinearRegression()

而不是

lm = LinearRegression