当前位置:   article > 正文

【机器学习sklearn实战】逻辑回归(Logistic regression)

【机器学习sklearn实战】逻辑回归(Logistic regression)

官网教程:logistic-regression — scikit-learn 1.5.1 documentation

一 导入包

# 导入包
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
  • 1
  • 2
  • 3
  • 4
  • 5

二 数据加载

# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target
  • 1
  • 2
  • 3
  • 4

三 数据划分

# 将数据划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
  • 1
  • 2

四 模型创建

# 创建逻辑回归模型实例
logistic_regression = LogisticRegression(max_iter=10, random_state=42)
  • 1
  • 2

五 模型训练

# 预测测试集上的标签
y_pred = logistic_regression.predict(X_test)
  • 1
  • 2

六 模型评估

# 输出预测准确率
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/979209
推荐阅读
  

闽ICP备14008679号