【机器学习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)
相关推荐
cookqq5 小时前
Palantir Foundry 核心建模体系:构建企业级智能知识图谱的基石
人工智能·机器学习·知识图谱·ai编程
gjhave6 小时前
jetson上trtexec模型转换
人工智能·机器学习
叶子Talk9 小时前
OpenAI破解80年数学猜想,AI首次做出原创证明
人工智能·数学·算法·机器学习·ai·openai·ai推理
有为少年9 小时前
Welford算法 | 从单一到批次
大数据·人工智能·深度学习·神经网络·算法·机器学习
恣艺10 小时前
Python 实用工具与机器学习入门:Rich + Tqdm + Faker + Schedule + Scikit-learn
python·机器学习·scikit-learn
隐层漫游者10 小时前
2026年了,你还只会调包?手把手教你K-Means、随机森林、XGBoost与朴素贝叶斯,全网最硬核机器学习实战指南!
机器学习
l1t11 小时前
DeepSeek总结的在 DuckDB 中试驾 Lance 数据湖仓格式
数据库·人工智能·机器学习·duckdb
l1t12 小时前
利用llama-vulkan版本测试腾讯混元Hy-MT2多语言翻译模型
人工智能·机器学习·llama
搞科研的小刘选手13 小时前
【人工智能专题研讨会】第五届人工智能与智能信息处理国际学术会议(AIIIP 2026)
人工智能·神经网络·机器学习·网络安全·数据挖掘·人机交互·信息处理
Sheldon Chao14 小时前
Lecture 7 基于策略梯度的算法
人工智能·算法·机器学习