【机器学习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)
相关推荐
吾在学习路27 分钟前
【CVPR 2018最佳论文】Squeeze-and-Excitation Networks
人工智能·深度学习·神经网络·机器学习
Salt_072831 分钟前
DAY 47 Tensorboard的使用介绍
人工智能·python·深度学习·机器学习
Salt_072842 分钟前
DAY 40 早停策略和模型权重的保存
人工智能·python·算法·机器学习
JoannaJuanCV2 小时前
自动驾驶—CARLA仿真(29)传感器(Sensors and data)
人工智能·机器学习·自动驾驶
天呐草莓2 小时前
集成学习 (ensemble learning)
人工智能·python·深度学习·算法·机器学习·数据挖掘·集成学习
车队老哥记录生活3 小时前
强化学习 RL 基础 3:随机近似方法 | 梯度下降
人工智能·算法·机器学习·强化学习
Godspeed Zhao3 小时前
自动驾驶中的传感器技术83——Sensor Fusion(6)
人工智能·机器学习·自动驾驶
没有梦想的咸鱼185-1037-16634 小时前
面向自然科学的人工智能建模方法【涵盖机器学习与深度学习的核心方法(如随机森林、XGBoost、CNN、LSTM、Transformer等)】
人工智能·深度学习·随机森林·机器学习·数据分析·卷积神经网络·transformer
燕双嘤4 小时前
LLM:RAG,设计模式,Agent框架
人工智能·机器学习·设计模式
海边夕阳20065 小时前
【每天一个AI小知识】:什么是图神经网络?
人工智能·经验分享·深度学习·神经网络·机器学习