【机器学习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)
相关推荐
OG one.Z3 小时前
06_决策树
算法·决策树·机器学习
番茄寿司4 小时前
具身智能六大前沿创新思路深度解析
论文阅读·人工智能·深度学习·计算机网络·机器学习
rengang664 小时前
25-TensorFlow:概述Google开发的流行机器学习框架
人工智能·机器学习·tensorflow
Gitpchy5 小时前
Day 23 机器学习管道 pipeline
python·机器学习
彩云回7 小时前
堆叠泛化(Stacking)
人工智能·机器学习·1024程序员节
晚霞apple8 小时前
Graph + Agents 融合架构:2025年七大创新路径
论文阅读·人工智能·深度学习·神经网络·机器学习
纪伊路上盛名在8 小时前
如何批量获取蛋白质序列的所有结构域(domain)数据-2
数据库·人工智能·机器学习·统计·计算生物学·蛋白质
浣熊-论文指导8 小时前
人工智能与生物医药融合六大创新思路
论文阅读·人工智能·深度学习·计算机网络·机器学习
yubo05098 小时前
自动化模型学习器——autoGluon
机器学习·自动化
东经116度10 小时前
权重初始化方法详解
深度学习·机器学习·xavier初始化·全零初始化·随机初始化·he初始化