【机器学习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)
相关推荐
AI周红伟13 分钟前
All in Token, 移动,电信,联通,阿里,百度,华为,字节,Token石油战争,Token经济,百度要“重写”AI价值度量
大数据·人工智能·机器学习·百度·copilot·openclaw
AI周红伟13 分钟前
Token经济学:AI时代的新货币战争,All in Token, 新时代的石油战争,华为,阿里,百度,字节的石油战争
大数据·人工智能·机器学习·百度·华为·copilot·openclaw
YUDAMENGNIUBI1 小时前
day20_逻辑回归
算法·机器学习·逻辑回归
tzc_fly7 小时前
AnisoAlign:各向异性模态对齐
人工智能·深度学习·机器学习
极客老王说Agent8 小时前
2026供应链智变:实在Agent供应链库存预测助手核心能力与配置深度教程
人工智能·机器学习·ai·chatgpt
沪漂阿龙9 小时前
面试题:训练-蒸馏详解——知识蒸馏、Teacher-Student、强弱蒸馏、Qwen3 强到弱蒸馏流程全解析
人工智能·深度学习·机器学习
xiaoxiaoxiaolll13 小时前
金属结构疲劳寿命预测与健康监测技术
人工智能·算法·机器学习
云和数据.ChenGuang14 小时前
FastText的核心优势
人工智能·深度学习·机器学习·数据挖掘·边缘计算
一切皆是因缘际会14 小时前
本地大模型轻量化部署
大数据·人工智能·机器学习·架构