【机器学习sklearn实战】逻辑回归(Logistic regression)

官网教程:logistic-regression --- scikit-learn 1.5.1 documentation

一 导入包

python 复制代码
# 导入包
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

二 数据加载

python 复制代码
# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target

三 数据划分

python 复制代码
# 将数据划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

四 模型创建

python 复制代码
# 创建逻辑回归模型实例
logistic_regression = LogisticRegression(max_iter=10, random_state=42)

五 模型训练

python 复制代码
# 预测测试集上的标签
y_pred = logistic_regression.predict(X_test)

六 模型评估

python 复制代码
# 输出预测准确率
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.4f}")

# 输出详细的分类报告
report = classification_report(y_test, y_pred)
print("Classification Report:")
print(report)

# 查看模型系数
coefficients = logistic_regression.coef_
print("Coefficients:")
print(coefficients)

# 查看截距
intercept = logistic_regression.intercept_
print("Intercept:")
print(intercept)
相关推荐
人工智能培训6 小时前
大模型与传统小模型、传统NLP模型的核心差异解析
人工智能·深度学习·神经网络·机器学习·生成对抗网络
砥锋8 小时前
纯NumPy手写两层GCN:从零开始理解图神经网络核心思想
机器学习
Larcher8 小时前
🔥 告别抓瞎:用 Claude Code (cc) 优雅接手与维护已有项目
javascript·机器学习·前端框架
大模型最新论文速读9 小时前
PreFT:只在 prefill 时使用 LoRA,推理速度翻倍效果不降
论文阅读·人工智能·深度学习·机器学习·自然语言处理
AI算法沐枫10 小时前
大模型 | 大模型之机器学习基本理论
人工智能·python·神经网络·学习·算法·机器学习·计算机视觉
larance11 小时前
[菜鸟教程] 机器学习教程第六课-机器学习基础术语
人工智能·机器学习
cxr82811 小时前
数据驱动的AI逆向材料设计:体系、方法与突破路径
人工智能·机器学习·智能体·逆向合成·材料设计合成·蜂群理论
Project_Observer11 小时前
使用Zoho Projects AI自动项目管理
大数据·数据库·人工智能·深度学习·机器学习·深度优先
前端若水12 小时前
【无标题】
java·人工智能·python·机器学习
吃好睡好便好12 小时前
在Matlab中绘制阶梯图
开发语言·人工智能·学习·算法·机器学习·matlab