【问题标题】:Error with Python: TypeError: fit() missing 1 required positional argument: 'y'Python 出错:TypeError:fit() 缺少 1 个必需的位置参数:'y'
【发布时间】:2020-07-29 16:34:07
【问题描述】:

每次运行下面的代码时,我都会收到一条错误消息,指出 fit 缺少 1 个必需的位置参数 (y),但我的“y”值已在我的代码中指定。

X_var = myData['PredictorName']
y = myData['TaskOutcome']
LogisticRegression()
LogisticRegression.fit(X_var, y)

我尝试了多种不同的方法将数据拉到 df 之外,但似乎都没有奏效。

这是我的 y.head() 输出(应该是一系列 1 和 0)

0    0
1    1
2    0
3    1
4    0
Name: Task, dtype: int64

【问题讨论】:

  • 你忘了调用构造函数:LogisticRegression.fit(X_var, y) 必须是LogisticRegression().fit(X_var, y)
  • 更好的是,如果您想使用此模型进行任何预测,请将您之前的调用 (LogisticRegression()) 存储到变量中(例如 model = LogisticRegression()),然后适合 model.fit()。跨度>
  • 请提供完整的错误输出,以及minimal reproducible example

标签: python dataframe logistic-regression


【解决方案1】:

您需要将对构造函数的调用分配给一个变量,然后对该变量调用 fit 函数。

X_var = myData['PredictorName']
y = myData['TaskOutcome']
logistic_regression = LogisticRegression()
logistic_regression.fit(X_var, y)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 2020-11-15
    • 2017-05-28
    • 2020-01-06
    • 2020-10-12
    相关资源
    最近更新 更多