Machine Learning in Action 5. Logistic Regression

本文详细介绍Logistic回归在二分类问题中的应用,包括分类原理、损失函数、优化方法及Python实现步骤。通过生成数据集、可视化、训练模型、预测及混淆矩阵评估,展示Logistic回归全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Logistic Regression

解决问题: 二分类问题(binary classification)
分类器: 为了实现logistic回归分类器,可以在每个特征上都乘以一个回归系数(权重),把所有结果值相加,总和(y= w1x1+w2x2+…+b)代入sigmoid函数中,从而得到一个范围在0-1之间的数值。任何大于0.5的数被分入1类,小于0.5被分入0类。 所以logistic regression可以堪称是一种概率估计。

损失函数/代价函数:对数似然损失(crossentropy 交叉熵) 公式如下图2
优化方法: 梯度下降 如图3
在这里插入图片描述

2. 如何用Python的内置函数直接完成logistic regression

# Step1: Import the required modules
from sklearn.datasets import make_classification
from matplotlib import pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import pandas as pd
# Step2: Generate the dataset
# Generate and dataset for Logistic Regression
x, y = make_classification(
    n_samples=100,
    n_features=1,
    n_classes=2,
    n_clusters_per_class=1,
    flip_y=0.03,
    n_informative=1,
    n_redundant=0,
    n_repeated=0
)
Step 3: Visualize the Data
# Create a scatter plot
plt.scatter(x, y, c=y, cmap='rainbow')
plt.title('Scatter Plot of Logistic Regression')
plt.show()
Step 4: Split the Dataset
# Split the dataset into training and test dataset
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1)
Step 5: Perform Logistic Regression
# Create a Logistic Regression Object, perform Logistic Regression
log_reg = LogisticRegression()
log_reg.fit(x_train, y_train)

LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
          verbose=0, warm_start=False)

# Show to Coeficient and Intercept
print(lr.coef_)
print(lr.intercept_)
Step 6: Make prediction using the model
# Perform prediction using the test dataset
y_pred = lr.predict(x_test)
Step 7: Display the Confusion Matrix
# Show the Confusion Matrix
confusion_matrix(y_test, y_pred)

array([[13,  1],
       [ 0, 11]], dtype=int64)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值