在本次作业中,我们要完成的是预测水库水位的变化预测大坝流出的水量。已知特征为水库的水位,要预测的y是大坝流出的水量。

编程作业 5 - 偏差和方差

这次练习我们将会看到如何使用课上的方法改进机器学习算法,包括过拟合、欠拟合的的状态判断以及学习曲线的绘制。

import numpy as np
import scipy.io as sio
import scipy.optimize as opt
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as snsdef load_data():"""for ex5d['X'] shape = (12, 1)pandas has trouble taking this 2d ndarray to construct a dataframe, so I ravelthe results"""d = sio.loadmat('ex5data1.mat')return map(np.ravel, [d['X'], d['y'], d['Xval'], d['yval'], d['Xtest'], d['ytest']])X, y, Xval, yval, Xtest, ytest = load_data()df = pd.DataFrame({'water_level':X, 'flow':y})sns.lmplot('water_level', 'flow', data=df, fit_reg=False, size=7)
plt.show()

输出结果为:

X, Xval, Xtest = [np.insert(x.reshape(x.shape[0], 1), 0, np.ones(x.shape[0]), axis=1) for x in (X, Xval, Xtest)]

代价函数

def cost(theta, X, y):
# INPUT:参数值theta,数据X,标签y
# OUTPUT:当前参数值下代价函数
# TODO:根据参数和输入的数据计算代价函数# STEP1:获取样本个数# your code here  (appro ~ 1 lines)m = X.shape[0]# STEP2:计算代价函数# your code here  (appro ~ 3 lines)inner = X @ theta - ysquare_sum = inner.T @ innercost = square_sum / (2 * m)return costtheta = np.ones(X.shape[1])
cost(theta, X, y

输出结果为:

303.95152555359761

梯度

def gradient(theta, X, y):
# INPUT:参数值theta,数据X,标签y
# OUTPUT:当前参数值下梯度
# TODO:根据参数和输入的数据计算梯度  # STEP1:获取样本个数# your code here  (appro ~ 1 lines)m = X.shape[0]# STEP2:计算代价函数# your code here  (appro ~ 1 lines)grad=  (X.T @ (X @ theta - y))/mreturn gradgradient(theta, X, y)

输出结果为:

array([ -15.30301567,  598.16741084])

正则化梯度与代价函数

def regularized_gradient(theta, X, y, l=1):
# INPUT:参数值theta,数据X,标签y
# OUTPUT:当前参数值下梯度
# TODO:根据参数和输入的数据计算梯度 # STEP1:获取样本个数# your code here  (appro ~ 1 lines)m = X.shape[0]# STEP2:计算正则化梯度regularized_term = theta.copy()  # same shape as thetaregularized_term[0] = 0  # don't regularize intercept theta# your code here  (appro ~ 1 lines)regularized_term = (l / m) * regularized_termreturn gradient(theta, X, y) + regularized_termdef regularized_cost(theta, X, y, l=1):m = X.shape[0]regularized_term = (l / (2 * m)) * np.power(theta[1:], 2).sum()return cost(theta, X, y) + regularized_term

拟合数据

正则化项 ?=0

def linear_regression_np(X, y, l=1):
# INPUT:数据X,标签y,正则化参数l
# OUTPUT:当前参数值下梯度
# TODO:根据参数和输入的数据计算梯度 # STEP1:初始化参数theta = np.ones(X.shape[1])# STEP2:调用优化算法拟合参数# your code here  (appro ~ 1 lines)res = opt.minimize(fun=regularized_cost,x0=theta,args=(X, y, l),method='TNC',jac=regularized_gradient,options={'disp': True})return restheta = np.ones(X.shape[0])final_theta = linear_regression_np(X, y, l=0).get('x')b = final_theta[0] # intercept
m = final_theta[1] # slopeplt.scatter(X[:,1], y, label="Training data")
plt.plot(X[:, 1], X[:, 1]*m + b, label="Prediction")
plt.legend(loc=2)
plt.show()

输出结果为:

training_cost, cv_cost = [], []

1.使用训练集的子集来拟合应模型

2.在计算训练代价和交叉验证代价时,没有用正则化

3.记住使用相同的训练集子集来计算训练代价

TIP:向数组里添加新元素可使用append函数

# TODO:计算训练代价和交叉验证集代价
# STEP1:获取样本个数,遍历每个样本
m = X.shape[0]
for i in range(1, m+1):# STEP2:计算当前样本的代价res = linear_regression_np(X[:i, :], y[:i], l=0)# your code here  (appro ~ 2 lines)tc = regularized_cost(res.x, X[:i, :], y[:i], l=0)cv = regularized_cost(res.x, Xval, yval, l=0)# STEP3:把计算结果存储至预先定义的数组training_cost, cv_cost中# your code here  (appro ~ 2 lines)training_cost.append(tc)cv_cost.append(cv)plt.plot(np.arange(1, m+1), training_cost, label='training cost')
plt.plot(np.arange(1, m+1), cv_cost, label='cv cost')
plt.legend(loc=1)
plt.show()

这个模型拟合不太好, 欠拟合了

创建多项式特征

def prepare_poly_data(*args, power):"""args: keep feeding in X, Xval, or Xtestwill return in the same order"""def prepare(x):# 特征映射df = poly_features(x, power=power)# 归一化处理ndarr = normalize_feature(df).as_matrix()# 添加偏置项return np.insert(ndarr, 0, np.ones(ndarr.shape[0]), axis=1)return [prepare(x) for x in args]def poly_features(x, power, as_ndarray=False):  #特征映射data = {'f{}'.format(i): np.power(x, i) for i in range(1, power + 1)}df = pd.DataFrame(data)return df.as_matrix() if as_ndarray else dfX, y, Xval, yval, Xtest, ytest = load_data()
poly_features(X, power=3)

准备多项式回归数据

  1. 扩展特征到 8阶,或者你需要的阶数
  2. 使用 归一化 来合并 ??xn
  3. 不要忘记添加偏置项
def normalize_feature(df):"""Applies function along input axis(default 0) of DataFrame."""return df.apply(lambda column: (column - column.mean()) / column.std())X_poly, Xval_poly, Xtest_poly= prepare_poly_data(X, Xval, Xtest, power=8)
X_poly[:3, :]
array([[  1.00000000e+00,  -3.62140776e-01,  -7.55086688e-01,1.82225876e-01,  -7.06189908e-01,   3.06617917e-01,-5.90877673e-01,   3.44515797e-01,  -5.08481165e-01],[  1.00000000e+00,  -8.03204845e-01,   1.25825266e-03,-2.47936991e-01,  -3.27023420e-01,   9.33963187e-02,-4.35817606e-01,   2.55416116e-01,  -4.48912493e-01],[  1.00000000e+00,   1.37746700e+00,   5.84826715e-01,1.24976856e+00,   2.45311974e-01,   9.78359696e-01,-1.21556976e-02,   7.56568484e-01,  -1.70352114e-01]])

画出学习曲线¶

首先,我们没有使用正则化,所以 ?=0

def plot_learning_curve(X, y, Xval, yval, l=0):
# INPUT:训练数据集X,y,交叉验证集Xval,yval,正则化参数l
# OUTPUT:当前参数值下梯度
# TODO:根据参数和输入的数据计算梯度 # STEP1:初始化参数,获取样本个数,开始遍历training_cost, cv_cost = [], []m = X.shape[0]for i in range(1, m + 1):# STEP2:调用之前写好的拟合数据函数进行数据拟合# your code here  (appro ~ 1 lines)res = linear_regression_np(X[:i, :], y[:i], l=l)# STEP3:计算样本代价# your code here  (appro ~ 1 lines)tc = cost(res.x, X[:i, :], y[:i])cv = cost(res.x, Xval, yval)# STEP3:把计算结果存储至预先定义的数组training_cost, cv_cost中# your code here  (appro ~ 2 lines)training_cost.append(tc)cv_cost.append(cv)plt.plot(np.arange(1, m + 1), training_cost, label='training cost')plt.plot(np.arange(1, m + 1), cv_cost, label='cv cost')plt.legend(loc=1)plot_learning_curve(X_poly, y, Xval_poly, yval, l=0)
plt.show()

你可以看到训练的代价太低了,不真实. 这是 过拟合

try ?=1

plot_learning_curve(X_poly, y, Xval_poly, yval, l=1)
plt.show()

训练代价增加了些,不再是0了。 也就是说我们减轻过拟合

try ?=100

plot_learning_curve(X_poly, y, Xval_poly, yval, l=100)
plt.show()

太多正则化了.
变成 欠拟合状态

找到最佳的 ?

l_candidate = [0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10]
training_cost, cv_cost = [], []for l in l_candidate:res = linear_regression_np(X_poly, y, l)tc = cost(res.x, X_poly, y)cv = cost(res.x, Xval_poly, yval)training_cost.append(tc)cv_cost.append(cv)plt.plot(l_candidate, training_cost, label='training')
plt.plot(l_candidate, cv_cost, label='cross validation')
plt.legend(loc=2)plt.xlabel('lambda')plt.ylabel('cost')
plt.show()

# best cv I got from all those candidates
l_candidate[np.argmin(cv_cost)]

输出:1

# use test data to compute the cost
for l in l_candidate:theta = linear_regression_np(X_poly, y, l).xprint('test cost(l={}) = {}'.format(l, cost(theta, Xtest_poly, ytest)))
test cost(l=0) = 10.122298845834932
test cost(l=0.001) = 10.989357236615056
test cost(l=0.003) = 11.26731092609127
test cost(l=0.01) = 10.881623900868235
test cost(l=0.03) = 10.02232745596236
test cost(l=0.1) = 8.632062332318977
test cost(l=0.3) = 7.336513212074589
test cost(l=1) = 7.466265914249742
test cost(l=3) = 11.643931713037912
test cost(l=10) = 27.7150802906621

调参后, ?=0.3λ=0.3 是最优选择,这个时候测试代价最小

入门机器学习(十二)--课后作业解析-偏差与方差(Python 实现)相关推荐

  1. 国科大学习资料--人工智能原理与算法-第十二次作业解析(学长整理)

    国科大学习资料–人工智能原理与算法-第十二次作业解析(张文生老师主讲)(14.14)

  2. 入门机器学习(十四)--编程作业-支持向量机(Python实现)

    在本练习中,我们将使用支持向量机(SVM)来构建垃圾邮件分类器. 我们将从一些简单的2D数据集开始使用SVM来查看它们的工作原理. 然后,我们将对一组原始电子邮件进行一些预处理工作,并使用SVM在处理 ...

  3. 新手入门机器学习十大算法

    新手入门机器学习十大算法 2018年9月17日 磐石 TensorFlowNews, 机器学习 0 在机器学习的世界中,有一种被称为"无免费午餐"的定理. 它意在说明没有哪种算法能 ...

  4. 无人驾驶汽车系统入门(十二)——卷积神经网络入门,基于深度学习的车辆实时检测

    无人驾驶汽车系统入门(十二)--卷积神经网络入门,基于深度学习的车辆实时检测 上篇文章我们讲到能否尽可能利用上图像的二维特征来设计神经网络,以此来进一步提高识别的精度.在这篇博客中,我们学习一类专门用 ...

  5. 2021年春季学期-信号与系统-第十二次作业参考答案-第四小题

    ▓ 本文是 2021年春季学期-信号与系统-第十二次作业参考答案中的小题答案 §04 第四小题 4. 画出X(z)X\left( z \right)X(z) 的零极点图,在下列三种收敛域下,求各对应的 ...

  6. 2021年春季学期-信号与系统-第十二次作业参考答案-第三小题

    ▓ 本文是 2021年春季学期-信号与系统-第十二次作业参考答案中的小题答案 §03 第三小题 3. 设激励x(t)=e−tx\left( t \right) = e^{ - t}x(t)=e−t 时 ...

  7. 2021年春季学期-信号与系统-第十二次作业参考答案-第二小题

    ▓ 本文是 2021年春季学期-信号与系统-第十二次作业参考答案中的小题答案 §02 第二小题 2. 用单边 zzz 变换求解下列差分方程,并求出零输入响应和零状态状态响应. (1) y[n]+3y[ ...

  8. 2021年春季学期-信号与系统-第十二次作业参考答案-第一小题

    ▓ 本文是2021年春季学期-信号与系统-第十二次作业参考答案中的小题答案 §01 第一小题 1. 用拉普拉斯变换解下列微分方程: (1) d2dt2y(t)+2ddty(t)+y(t)=δ(t)+2 ...

  9. 2021年春季学期-信号与系统-第十二次作业参考答案-第五小题

    ▓ 本文是 2021年春季学期-信号与系统-第十二次作业参考答案中的小题答案 §05 第五小题 5. 给定实数序列 x[n]x\left[ n \right]x[n]及其Z变换表达式 X(z)X\le ...

最新文章

  1. cni k8s 插件安装_第一次,如此清晰脱俗的直解K8S网络
  2. 日记 [2007年01月26日] 用 phpMyAdmin 让 MySQL 数据库管理温和化
  3. 计算机为什么会出现网络用户,告诉你电脑显示无internet访问权限怎么办
  4. 支持html5浏览器速查
  5. 【408预推免复习】计算机网络(谢希仁第七版)第四章——网络层
  6. Android 获取屏幕宽度和高度直接转换为DP
  7. 每个Java开发人员都必须避免的9个安全错误
  8. css折线效果,CSS3 box-shadow实现纸张的曲线投影效果 张鑫旭-鑫空间-鑫生活
  9. Dbutils常见方法
  10. RNA_seq(1)植物转录组实战(下)之DESeq2进行差异基因分析
  11. 机载激光雷达原理与应用科普(四)
  12. python粒子特效_初试PyOpenGL四 (Python+OpenGL)GPU粒子系统与基本碰撞
  13. Python爬取中国票房网所有电影片名和演员名字,爬取齐鲁网大陆所有电视剧名称...
  14. 信息安全三级易错题总结
  15. 电脑远程开机以及控制
  16. Resultful接口实现后端文件下载
  17. pandas学习之pandas基础
  18. oracle实验报告4:Oracle数据库模式对象管理(含实验小结)
  19. (转)Google Voice呼转到中国电话的五种方法
  20. 机器学习部分书籍推荐

热门文章

  1. Springboot2 搭建 高性能Websocket服务器
  2. Log4j、Log4j 2、JUL、JCL 、SFL4J 、Logback 与 Lombok 的使用
  3. Inception v2/v3原理与实现
  4. CentOS 7 安装MongoDB 4.0
  5. ARouter使用随记
  6. 系统软键盘Android在外接物理键盘时,如何强制调用系统软键盘?
  7. 杭电 1248 寒冰王座(全然背包)
  8. 微信公众平台开发--判断终端使用的浏览器是否是微信浏览器
  9. Flutter UiKitView 嵌入iOS原生View
  10. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误