【机器学习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)
相关推荐
weixin_429630264 小时前
3.49 HVLF:一种跨场景的整体视觉定位框架
深度学习·机器学习·计算机视觉
深圳市机智人激光雷达4 小时前
技术筑牢安全冗余:激光雷达在自动驾驶高阶感知中的底层价值与范式演进
人工智能·安全·机器学习·3d·机器人·自动驾驶·无人机
DreamLife☼8 小时前
OpenBCI-实战五:脑电数据可视化仪表板
人工智能·机器学习·信息可视化·开源硬件·脑机接口·openbci
行业研究员10 小时前
腾讯云AgentMemory产品介绍与核心痛点解决
人工智能·机器学习·腾讯云·agentmem
苏州邦恩精密11 小时前
江苏蔡司3D扫描仪定制厂家:专业三维检测方案助力智能制造升级
人工智能·科技·机器学习·3d·自动化·制造
树谷-胡老师12 小时前
2024年中国大型数据中心空间分布及环境属性数据集
人工智能·机器学习
小马哥crazymxm12 小时前
自动驾驶“跨化身”!Sensor2Sensor用4D高斯泼溅+扩散模型,把网络行车记录仪变成高精度LiDAR真数据
人工智能·机器学习·自动驾驶
大模型最新论文速读12 小时前
UnityMAS-O:专用于多 agent 工作流训练的 RL 框架
论文阅读·人工智能·深度学习·机器学习·自然语言处理
love530love12 小时前
根治 PyTorch CUDA `pynvml` 弃用警告:直接修改 `torch/cuda/__init__.py` 的实践记录
人工智能·pytorch·windows·python·深度学习·机器学习·pynvml
数智工坊14 小时前
周志华《Machine Learning》学习笔记--第六章--支持向量机
笔记·神经网络·学习·算法·机器学习·支持向量机