【课后题练习】 陈强-机器学习-Python-Ch9 惩罚回归(student-mat.csv)

系列文章目录

【学习笔记】 陈强-机器学习-Python-Ch4 线性回归
【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归
【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv)
【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归
【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析
【学习笔记】 陈强-机器学习-Python-Ch8 朴素贝叶斯
【学习笔记】 陈强-机器学习-Python-Ch9 惩罚回归



前言

本学习笔记 仅为以防自己忘记了,顺便分享给一起学习的网友们参考。如有不同意见/建议,可以友好讨论。

本学习笔记 所有的代码和数据都可以从 陈强老师的个人主页 上下载

参考书目:陈强.机器学习及Python应用. 北京:高等教育出版社, 2021.

数学原理等 详见陈强老师的 PPT


参考了:
网友带我去滑雪机器学习之惩罚回归—基于python实现(附完整代码)


一、数据集student-mat.csv

UCI Machine Learning Repository 的葡萄牙高中数学成绩数据student-mat.csv
变量s:
1 school - student’s school (binary: ‘GP’ - Gabriel Pereira or ‘MS’ - Mousinho da Silveira)
2 sex - student’s sex (binary: ‘F’ - female or ‘M’ - male)
3 age - student’s age (numeric: from 15 to 22)
4 address - student’s home address type (binary: ‘U’ - urban or ‘R’ - rural)
5 famsize - family size (binary: ‘LE3’ - less or equal to 3 or ‘GT3’ - greater than 3)
6 Pstatus - parent’s cohabitation status (binary: ‘T’ - living together or ‘A’ - apart)
7 Medu - mother’s education (numeric: 0 - none, 1 - primary education (4th grade), 2 – 5th to 9th grade, 3 – secondary education or 4 – higher education)
8 Fedu - father’s education (numeric: 0 - none, 1 - primary education (4th grade), 2 – 5th to 9th grade, 3 – secondary education or 4 – higher education)
9 Mjob - mother’s job (nominal: ‘teacher’, ‘health’ care related, civil ‘services’ (e.g. administrative or police), ‘at_home’ or ‘other’)
10 Fjob - father’s job (nominal: ‘teacher’, ‘health’ care related, civil ‘services’ (e.g. administrative or police), ‘at_home’ or ‘other’)
11 reason - reason to choose this school (nominal: close to ‘home’, school ‘reputation’, ‘course’ preference or ‘other’)
12 guardian - student’s guardian (nominal: ‘mother’, ‘father’ or ‘other’)
13 traveltime - home to school travel time (numeric: 1 - <15 min., 2 - 15 to 30 min., 3 - 30 min. to 1 hour, or 4 - >1 hour)
14 studytime - weekly study time (numeric: 1 - <2 hours, 2 - 2 to 5 hours, 3 - 5 to 10 hours, or 4 - >10 hours)
15 failures - number of past class failures (numeric: n if 1<=n<3, else 4)
16 schoolsup - extra educational support (binary: yes or no)
17 famsup - family educational support (binary: yes or no)
18 paid - extra paid classes within the course subject (Math or Portuguese) (binary: yes or no)
19 activities - extra-curricular activities (binary: yes or no)
20 nursery - attended nursery school (binary: yes or no)
21 higher - wants to take higher education (binary: yes or no)
22 internet - Internet access at home (binary: yes or no)
23 romantic - with a romantic relationship (binary: yes or no)
24 famrel - quality of family relationships (numeric: from 1 - very bad to 5 - excellent)
25 freetime - free time after school (numeric: from 1 - very low to 5 - very high)
26 goout - going out with friends (numeric: from 1 - very low to 5 - very high)
27 Dalc - workday alcohol consumption (numeric: from 1 - very low to 5 - very high)
28 Walc - weekend alcohol consumption (numeric: from 1 - very low to 5 - very high)
29 health - current health status (numeric: from 1 - very bad to 5 - very good)
30 absences - number of school absences (numeric: from 0 to 93)

响应变量:
31 G1 - first period grade (numeric: from 0 to 20)
31 G2 - second period grade (numeric: from 0 to 20)
32 G3 - final grade (numeric: from 0 to 20, output target)

二、课后习题

1.载入数据

(1)由于此csv文件以分号 “;”分割,故使用命令“pd.read_table(‘student-mat.csv’,sep=‘;’)”载入数据,并考察此数据框的形状与前5个观测值;

#导入模块
import numpy as np
import pandas as pd
#载入数据
student_mat =pd.read_table(r'D:\桌面文件\Python\【陈强-机器学习】MLPython-PPT-PDF\MLPython_Data\student-mat.csv',sep=';')
#数据集基本信息
print(student_mat.shape)
student_mat.head()

结果输出: (395, 33) 395个观测值与35个变量
在这里插入图片描述

笔记:pd.read_table()

pd.read_table() 是一个读取文本文件的函数,功能类似于 pd.read_csv()。
 pd.read_table() 默认以制表符(\t)作为分隔符,而 pd.read_csv() 默认以逗号(,)作为分隔符。
 pd.read_table() 是用来读取以制表符分隔的文本文件的,但也可以通过设置参数来读取其他分隔符的文件。

#基本语法和参数
import pandas as pd
# 读取文件
df = pd.read_table(
    'file_path.txt', 
    sep=',',  #分隔符或字段分隔符。默认为 \t(制表符)。如果文件使用其他分隔符,比如逗号(,)或分号(;)
    header=None,  # 默认为 infer,即自动推断。如果没有列名行,可以设置为 None。
    names=['col1', 'col2', 'col3'], #指定列名。如果文件没有列名行,可以通过这个参数提供列名。
    index_col=0,  #指定用作索引的列。可以是列的名称或列的索引号。
    usecols=['col1', 'col3'
关于数据集 关于数学学生数据集 该数据集最初来源于UCI机器学习库,提供了丰富的学生数学课程成绩数据。它提供了关于学生学业成就和社会人口背景的详细洞察,使其成为教育数据挖掘和预测分析的绝佳资源。 主要特点和属性 人口统计和背景: 学校:标识学生的学校(例如 Gabriel Pereira 或 Mousinho da Silveira)。 性别和年龄:基本人口统计信息,有助于探索不同群体之间的表现趋势。 地址和家庭规模:有关学生家庭环境的详细信息,包括他们居住在城市还是农村以及他们的家庭规模。 父母及家庭信息: 父母同居和教育:有关父母是否同居以及他们的教育水平的数据,这些数据与学生支持和学业成绩相关。 父母职业:有关母亲和父亲的工作的信息,提供有关社会经济因素的进一步背景信息。 教育和行为变量: 学习时间和失败:每周的学习时间和过去课堂失败的历史有助于衡量学术奉献精神和潜在挑战。 支持和课外活动:记录学生是否获得额外的教育支持或参加课外活动,这会影响整体表现。 与学校相关的因素:上学时间、出勤率(缺勤率)以及参加额外付费课程有助于全面了解教育环境。 生活方式和社会因素: 互联网接入、空闲时间和社交:互联网可用性、空闲时间以及学生与朋友外出频率等变量有助于捕捉生活方式和行为模式。 健康与福祉:工作日和周末的自我报告健康状况和饮酒模式可以了解个人健康状况,这可能会影响学习成绩。 学业成绩: 成绩:数据集包含三个关键评估:G1(第一阶段成绩)、G2(第二阶段成绩)和 G3(最终成绩)。G3(最终成绩)是预测模型的主要目标变量。 潜在应用 预测模型: 研究人员和数据科学家可以根据众多社会人口统计和教育特征建立回归模型来预测最终成绩(G3)。 探索性数据分析: 该数据集非常适合探索家庭背景、生活方式选择和学业成就之间的关系。例如,可以分析学习时间或父母教育水平与学业表现之
### 关于《机器学习Python陈强著 第十一章 内容 对于《机器学习Python》,由陈强编写的书籍,在第十一章主要探讨集成方法中的Bagging以及Pasting技术[^1]。这些技术属于组合多个模型来提升预测性能的一种方式。 #### Bagging 和 Pasting 技术概述 - **Bagging (Bootstrap Aggregating)** 是一种减少方差的技术,通过创建多个训练集样本(有放回抽样),并基于不同的子集独立地训练若干基估计器。最终预测采用平均值(回归)或多数投票分类的结果。 - **Pasting** 类似于Bagging但是不使用替换的方式抽取样本,因此某些样本可能不会被选中用于特定的学习器训练过程。 此章节会详细介绍这两种方法的工作原理及其应用场景,并提供具体的实现案例研究。读者可以了解到如何利用Scikit-Learn库轻松构建自己的bagging和pasting分类器/回归器实例。 此外,书中还会介绍一些重要的参数配置选项,比如`n_estimators`, `max_samples`, `bootstrap`等,帮助优化模型表现。 为了更好地理解本章内容,建议实际操作代码示例,尝试调整不同参数观察其对结果的影响。这有助于加深理论知识的理解并通过实践巩固所学技能。 ```python from sklearn.ensemble import BaggingClassifier from sklearn.tree import DecisionTreeClassifier # 创建决策树作为基础估计器 base_estimator = DecisionTreeClassifier() # 使用Bagging方法初始化分类器 clf = BaggingClassifier(base_estimator=base_estimator, n_estimators=100, # 基估计器数量 max_samples=0.5, # 训练每个基估计器使用的最大样本比例 bootstrap=True # 是否启用自助采样 ) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值