You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用LogisticRegression的fit函数未获预期输出,求排查

为什么LogisticRegression的fit方法只输出模型信息?

嘿,这其实不是问题,是scikit-learn的正常设计逻辑哦!让我给你捋清楚:

首先,model.fit(x, y)这个方法的作用是在原地训练你的模型,它的返回值就是训练后的模型对象本身——所以你用print()输出它的时候,只会看到模型的配置参数(比如你设置的solver='liblinear'和random_state=0),这完全符合预期。

你应该是想要查看模型训练后的实际结果,比如系数、预测值或者评估指标吧?那你需要在fit之后调用模型的属性或者其他方法,我给你整理了几个常用的操作:

1. 查看模型的核心参数

训练完成后,模型会生成几个关键属性,比如决策边界的系数和截距:

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)
model.fit(x, y)  # 先完成训练,不需要打印这个返回值

# 输出模型系数和截距
print("模型系数:", model.coef_)
print("模型截距:", model.intercept_)
# 还可以查看模型识别的类别
print("类别:", model.classes_)

2. 生成预测结果

如果你想看看模型对输入x的预测值,用predict()方法:

y_pred = model.predict(x)
print("预测值:", y_pred)
# 对比真实值和预测值
print("真实值 vs 预测值:")
for true, pred in zip(y, y_pred):
    print(f"{true} -> {pred}")

3. 输出分类评估报告

用你导入的confusion_matrix和classification_report来评估模型性能:

print("\n混淆矩阵:")
print(confusion_matrix(y, y_pred))
print("\n分类报告:")
print(classification_report(y, y_pred))

这样修改后,你就能得到训练后模型的具体结果啦,而不是仅仅看到模型的参数信息。

内容的提问来源于stack exchange,提问作者manjeetsingh tanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:12:59