机器学习课程作业一
KNN、线性回归与逻辑回归综合实践
一、 作业目标 本次作业旨在系统理解并实践三种经典机器学习方法:K-近邻算法(KNN)、线性回归(Linear Regression)与逻辑回归(Logistic Regression)。需要同学在真实医疗相关数据集上完成回归任务与分类任务,比较不同模型及不同参数设置下的表现,并对实验现象进行解释。 • 理解回归任务与分类任务的差异,以及相应评价指标的含义。 • 掌握 KNN、线性回归、逻辑回归的基本建模流程。 • 在不同参数配置下完成实验,对比模型表现并进行分析。 • 形成规范的实验报告,能够清晰展示代码、结果与结论。 二、 数据集介绍与获取方式 本次作业使用两个规模适中、适合教学实验的数据集,均可通过 scikit-learn 直接加载。其中 Diabetes 数据集用于回归任务;Breast Cancer Wisconsin 数据集用于分类任务。 数据集 任务类型 样本数 / 特征数 适用模型 获取方式 Diabetes 回归 442 / 10 线性回归、KNN 回归 sklearn.datasets.load_diabetes Breast Cancer Wisconsin 二分类 569 / 30 逻辑回归、KNN 分类 sklearn.datasets.load_breast_cancer
2.1 Diabetes 数据集(回归任务) 该数据集是一个监督学习、回归任务数据集,共 442 个样本、10 个特征,目标值为疾病进展的连续量化指标,适合用于线性回归和 KNN 回归实验。 •推荐任务:线性回归、KNN 回归 •官方获取链接:https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_diabetes.html Data Set Characteristics: Number of Instances: 442 Number of Attributes: First 10 columns are numeric predictive values Target: Column 11 is a quantitative measure of disease progression one year after baseline Attribute Information: • age age in years • sex • bmi body mass index • bp average blood pressure • s1 tc, total serum cholesterol • s2 ldl, low-density lipoproteins • s3 hdl, high-density lipoproteins • s4 tch, total cholesterol / HDL • s5 ltg, possibly log of serum triglycerides level • s6 glu, blood sugar level Note: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times the square root of n_samples (i.e. the sum of squares of each column totals 1). Source URL: https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html
2.2 Breast Cancer Wisconsin 数据集(二分类) 该数据集是一个经典的二分类数据集,共 569 个样本、30 个特征,其中 212 个恶性样本(malignant)和 357 个良性样本(benign)。特征来源于乳腺肿块细针穿刺图像中的细胞核特征,适合用于逻辑回归与 KNN 分类实验。 • 推荐任务:逻辑回归、KNN 分类 • scikit-learn 获取链接:https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html Data Set Characteristics: Number of Instances: 569 Number of Attributes: 30 numeric, predictive attributes and the class Attribute Information: • radius (mean of distances from center to points on the perimeter) • texture (standard deviation of gray-scale values) • perimeter • area • smoothness (local variation in radius lengths) • compactness (perimeter^2 / area - 1.0) • concavity (severity of concave portions of the contour) • concave points (number of concave portions of the contour) • symmetry • fractal dimension (“coastline approximation” - 1) The mean, standard error, and “worst” or largest (mean of the three worst/largest values) of these features were computed for each image, resulting in 30 features. For instance, field 0 is Mean Radius, field 10 is Radius SE, field 20 is Worst Radius. • class: o WDBC-Malignant o WDBC-Benign
三、 具体任务要求 3.1 线性回归实验(Diabetes 数据集) 请使用 Diabetes 数据集完成回归任务,建立线性回归模型,并分析模型在训练集与测试集上的表现。 • 完成训练集 / 测试集划分,进行五折交叉验证。 • 训练线性回归模型,并输出 MSE、RMSE、MAE、R²。 • 输出模型系数,分析哪些特征对预测影响较大。 • 展示真实值与预测值的对比结果(表格或图像均可)。 • 至少尝试不同划分比例、不同随机种子(初值),比较结果变化。 • 可进一步比较是否进行特征标准化对结果的影响。 3.2 逻辑回归实验(Breast Cancer Wisconsin 数据集) 请使用 Breast Cancer Wisconsin 数据集完成二分类任务,建立逻辑回归模型,并在不同参数设置下比较分类性能。 • 完成训练集 / 测试集划分,进行五折交叉验证。 • 建议对输入特征进行标准化处理。 • 输出 Accuracy、Precision、Recall、F1-score 与 Confusion Matrix。 • 比较不同参数设置下的模型效果,并分析变化原因。 • 输出模型系数,简要讨论特征重要性。 3.3 KNN 实验(同时包含回归与分类) KNN 需要在两个数据集上分别完成:在 Diabetes 数据集上做 KNN 回归,在 Breast Cancer Wisconsin 数据集上做 KNN 分类。目的是理解同一种方法在回归任务和分类任务中的不同用法。 • KNN 回归:输出 MSE、RMSE、MAE、R²,并与线性回归结果进行对比。 • KNN 分类:输出 Accuracy、Precision、Recall、F1-score、Confusion Matrix,并与逻辑回归结果进行对比。 • 重点比较不同 k 值、不同权重方式以及是否标准化对结果的影响。 四、 结果呈现与报告要求 每位同学需提交书面实验报告。报告中至少应包括:数据集简介、实验环境、模型与参数设置、评价指标、结果对比表、结果分析与结论,要求展示5折交叉验证的结果,并且将代码文件和报告文件压缩打包。 建议将结果以表格方式统一整理。 4.1 任务结果表(建议模板) 表格 1 KNN结果 参数设置 5折交叉 MSE RMSE MAE R²
示例: k=5, weights=distance 1 2 3 4 5 示例: k=8, weights=distance 1 2 3 4 5