【问题标题】:Not getting any output while trying to run the fit function of LogisticRegression尝试运行 LogisticRegression 的拟合函数时没有得到任何输出
【发布时间】:2021-01-24 12:32:31
【问题描述】:

下面是我的简单代码。

import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix

x = np.arange(10).reshape(-1, 1)
y = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

model = LogisticRegression(solver='liblinear', random_state=0)

print(model.fit(x, y))

我得到的输出是:

LogisticRegression(random_state=0, solver='liblinear')

我已经用其他数据和 PyCharm 尝试过,同样的事情。

我做错了什么?

【问题讨论】:

  • 这能回答你的问题吗? What does "fit" method in scikit-learn do?
  • 这是一个非常低级的问题,OP 可以轻松查看 Sklearn 文档。为什么要留下答案?!添加model.fit(x, y)model.predict(x[:2, :])model.score(x, y)他需要先谷歌!
  • 我投票结束这个问题,因为所描述的行为是预期的和名义上的行为,并且没有任何问题或错误需要纠正或调试。

标签: python machine-learning scikit-learn


【解决方案1】:

Scikit learn 的模型都以类似的方式工作。 fit 方法不返回任何内容,它只更新模型的参数,即它进行训练。 You can see it in the documentation.

如果要返回预测作为示例,则需要使用predict 方法。请注意,您当然应该在拟合模型后使用predict 方法(这对于任何其他需要拟合模型才能有意义的方法都是有效的)。请注意,模型通常还实现了一个 fit_predict 方法,它同时执行了这两个操作。

您的代码可能如下所示:

model = LogisticRegression(solver='liblinear', random_state=0)
model.fit(x, y) #does the learning
predictions = model.predict(x) #getting the predicted values on the training dataset

【讨论】:

  • model.predict(x) 是什么意思,它返回array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1]) ,即x
  • model.predict(x) 返回预测,我们称它们为y_pred。在你的情况下,ypred = y 这意味着模型在训练集上具有 100% 的准确率
猜你喜欢
  • 2020-09-02
  • 2020-04-28
  • 1970-01-01
  • 2021-12-10
  • 2021-07-26
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2015-10-28
相关资源
最近更新 更多