【机器学习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)
相关推荐
OpenBayes贝式计算2 天前
解决视频模型痛点,TurboDiffusion 高效视频扩散生成系统;Google Streetview 涵盖多个国家的街景图像数据集
人工智能·深度学习·机器学习
OpenBayes贝式计算2 天前
OCR教程汇总丨DeepSeek/百度飞桨/华中科大等开源创新技术,实现OCR高精度、本地化部署
人工智能·深度学习·机器学习
够快云库3 天前
能源行业非结构化数据治理实战:从数据沼泽到智能资产
大数据·人工智能·机器学习·企业文件安全
B站_计算机毕业设计之家3 天前
电影知识图谱推荐问答系统 | Python Django系统 Neo4j MySQL Echarts 协同过滤 大数据 人工智能 毕业设计源码(建议收藏)✅
人工智能·python·机器学习·django·毕业设计·echarts·知识图谱
Flying pigs~~3 天前
机器学习之逻辑回归
人工智能·机器学习·数据挖掘·数据分析·逻辑回归
Evand J3 天前
通过matlab实现机器学习的小项目示例(鸢尾花分类)
机器学习·支持向量机·matlab
_Li.3 天前
Simulink - 6DOF (Euler Angles)
人工智能·算法·机器学习·游戏引擎·cocos2d
Project_Observer3 天前
工时日志在项目进度管理中扮演着怎样的角色?
数据库·深度学习·机器学习
scott1985123 天前
Improving Classifier-Free Guidance of Flow Matching via Manifold Projection
人工智能·python·机器学习