深度学习基础 Week7 逻辑回归使用实例

发布时间:2026/8/3 3:42:45

深度学习基础 Week7 逻辑回归使用实例 目录摘要Abstract1实验目的2实验环境3实验原理简述4完整实验代码5程序运行结果展示5.1控制台文字输出结果6拓展小实验多分类 Softmax 逻辑回归7实验结论8实验收获摘要本实验通过Python和scikit-learn实现了逻辑回归模型的完整流程对比了原生线性逻辑回归与多项式特征变换后的非线性分类效果。实验使用环形数据集验证了特征变换的重要性结果显示二阶多项式变换将准确率从线性模型的50%提升至90%以上。实验还展示了决策边界可视化并拓展到Softmax多分类任务。结果表明特征工程对提升逻辑回归的非线性分类能力至关重要但需注意避免维度爆炸。该实验验证了逻辑回归的适用场景和局限性加深了对判别式模型和特征变换的理解。AbstractThis experiment implemented the full process of a logistic regression model using Python and scikit-learn, and compared the performance of original linear logistic regression with non-linear classification after polynomial feature transformation. A ring-shaped dataset was used to verify the importance of feature transformation, and the results showed that a second-order polynomial transformation increased accuracy from 50% with the linear model to over 90%. The experiment also demonstrated decision boundary visualization and extended to Softmax multi-class tasks. The results indicate that feature engineering is crucial for improving the non-linear classification ability of logistic regression, but its important to avoid dimensionality explosion. This experiment validated the applicable scenarios and limitations of logistic regression, and deepened the understanding of discriminative models and feature transformation.1实验目的理解逻辑回归建模完整流程数据加载、数据预处理、特征变换、模型训练、预测、效果评估对比原生逻辑回归线性分类局限 多项式特征变换后的非线性分类效果直观观测模型准确率、决策边界差异验证课堂理论知识掌握 Python scikit-learn 实现逻辑回归代码编写与结果解读。2实验环境编程语言Python 3.8依赖库numpy、matplotlib、scikit-learn实验数据集sklearn 内置环形二维数据集make_circles天然线性不可分适合验证特征变换作用3实验原理简述逻辑回归属于线性判别式分类器原生决策边界为直线无法划分环形数据通过多项式特征变换将二维特征映射至高维空间逻辑回归可学习非线性边界模型评价指标分类准确率 Accuracy4完整实验代码# 1. 导入所需工具包 import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_circles from sklearn.linear_model import LogisticRegression from sklearn.preprocessing import PolynomialFeatures, StandardScaler from sklearn.metrics import accuracy_score # 设置图片中文正常显示 plt.rcParams[font.family] [SimHei, WenQuanYi Micro Hei] plt.rcParams[axes.unicode_minus] False # 2. 生成环形非线性数据集 # noise添加少量噪声因子随机种子固定保证每次运行结果一致 X, y make_circles(n_samples500, noise0.1, random_state20) # 3. 实验1原生逻辑回归无特征变换纯线性分类 # 3.1 标准化特征梯度下降收敛必备 scaler StandardScaler() X_scaled scaler.fit_transform(X) # 3.2 初始化并训练逻辑回归模型 lr_linear LogisticRegression(random_state20) lr_linear.fit(X_scaled, y) # 3.3 预测样本并计算准确率 y_pred_linear lr_linear.predict(X_scaled) acc_linear accuracy_score(y, y_pred_linear) print( 未做特征变换线性逻辑回归 ) print(f模型分类准确率{acc_linear:.4f}) # 4. 实验2添加二阶多项式特征变换构造非线性特征 poly PolynomialFeatures(degree2, include_biasFalse) X_poly poly.fit_transform(X) X_poly_scaled scaler.fit_transform(X_poly) # 训练多项式特征下的逻辑回归 lr_poly LogisticRegression(random_state20) lr_poly.fit(X_poly_scaled, y) y_pred_poly lr_poly.predict(X_poly_scaled) acc_poly accuracy_score(y, y_pred_poly) print(\n 二阶多项式特征变换后逻辑回归 ) print(f模型分类准确率{acc_poly:.4f}) # 5. 绘制两组模型决策边界对比图 def plot_decision_boundary(model, X, y, title): x_min, x_max X[:, 0].min() - 0.5, X[:, 0].max() 0.5 y_min, y_max X[:, 1].min() - 0.5, X[:, 1].max() 0.5 xx, yy np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02)) # 区分是否为多项式特征输入 if poly in title: grid poly.transform(np.c_[xx.ravel(), yy.ravel()]) grid scaler.transform(grid) else: grid scaler.transform(np.c_[xx.ravel(), yy.ravel()]) Z model.predict(grid) Z Z.reshape(xx.shape) plt.contourf(xx, yy, Z, alpha0.3) plt.scatter(X[:, 0], X[:, 1], cy, s30) plt.title(title) plt.xlabel(特征x1) plt.ylabel(特征x2) plt.figure(figsize(12, 5)) plt.subplot(1, 2, 1) plot_decision_boundary(lr_linear, X, y, f线性逻辑回归 准确率:{acc_linear:.2f}) plt.subplot(1, 2, 2) plot_decision_boundary(lr_poly, X, y, f多项式变换后 准确率:{acc_poly:.2f}) plt.tight_layout() plt.show()5程序运行结果展示5.1控制台文字输出结果6拓展小实验多分类 Softmax 逻辑回归from sklearn.datasets import load_iris # 鸢尾花3分类数据集 iris load_iris() X_iris, y_iris iris.data, iris.target # Softmax多分类逻辑回归 softmax_lr LogisticRegression(multi_classmultinomial, solverlbfgs, max_iter200) softmax_lr.fit(X_iris, y_iris) y_pre softmax_lr.predict(X_iris) acc accuracy_score(y_iris, y_pre) print(f\n鸢尾花3分类Softmax回归准确率{acc:.4f})运行输出验证逻辑回归拓展 Softmax 可轻松完成多分类任务。7实验结论原生逻辑回归是严格线性模型面对非线性分布数据分类效果很差必须依靠特征变换提升拟合能力多项式特征变换是弥补线性模型缺陷最常用手段但阶数过高易产生维度爆炸、引发过拟合逻辑回归结构简单、训练快速可解释性强适合风控、打分卡等需要输出概率的业务场景若数据非线性关系极度复杂人工特征工程成本过高可改用级联逻辑回归或者神经网络自动学习特征。8实验收获通过代码实操将之前学习的判别式模型、特征变换、决策边界、Softmax 多分类等理论知识落地直观看到每一步预处理和特征工程对模型精度的影响清晰掌握了逻辑回归的适用场景与局限性。

相关新闻