【机器学习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)
相关推荐
茗鹤APS和MES7 小时前
工业排产:AI可赋能,APS不可替代
人工智能·深度学习·机器学习
zx_741484817 小时前
【机器学习入门】PyTorch 神经网络、卷积神经网络 CNN 实现矿物分类
pytorch·神经网络·机器学习
leihefeng8 小时前
手写数字识别:KNN vs 逻辑回归实战
python·算法·机器学习·逻辑回归·scikit-learn
VL——MOESR8 小时前
【具身智能】TurboVLA阅读随笔
论文阅读·机器学习·turbo·具身智能·vla
张欣-男8 小时前
5分钟理解线性代数v2
人工智能·线性代数·机器学习
是翎9 小时前
Vibe Coding零基础入门:六步工作流拆解
大数据·人工智能·学习·机器学习·数据挖掘
恒知学术9 小时前
锂离子电池 SOC 估计英文论文怎么找?卡尔曼滤波与数据驱动方法检索
机器学习·参考文献·谷歌学术·sci论文·免费论文检索工具·英文文献
桃西西呀9 小时前
9月1日起AI图要被标记了,机器怎么一眼认出哪张是 AI 画的?
人工智能·机器学习·llm
Zzj_tju9 小时前
Prompt Injection 防御:隔离不可信上下文的最小复现
人工智能·深度学习·机器学习·自然语言处理·prompt
陈年老古董9 小时前
矿物分类实战:从传统机器学习到深度学习(含PyTorch实现)
笔记·深度学习·学习·机器学习·分类