机器学习基石笔记:Homework #4 Regularization&Validation相关习题
原文地址:https://www.jianshu.com/p/3f7d4aa6a7cf
问题描述



程序实现
# coding: utf-8
import numpy as np
import math
import matplotlib.pyplot as plt
def sign(x):
if(x>=0):
return 1
else:
return -1
def read_data(dataFile):
with open(dataFile,'r') as f:
lines=f.readlines()
data_list=[]
for line in lines:
line=line.strip().split()
data_list.append([1.0] + [float(l) for l in line])
dataArray=np.array(data_list)
num_data=dataArray.shape[0]
num_dim=dataArray.shape[1]-1
dataX=dataArray[:,:-1].reshape((num_data,num_dim))
dataY=dataArray[:,-1].reshape((num_data,1))
return dataX,dataY
def w_reg(dataX,dataY,namuta):
num_dim=dataX.shape[1]
dataX_T=np.transpose(dataX)
tmp=np.dot(np.linalg.inv(np.dot(dataX_T,dataX)+namuta*np.eye(num_dim)),dataX_T)
return np.dot(tmp,dataY)
def pred(wREG,dataX):
pred=np.dot(dataX,wREG)
num_data=dataX.shape[0]
for i in range(num_data):
pred[i][0]=sign(pred[i][0])
return pred
def zero_one_cost(pred,dataY):
return np.sum(pred!=dataY)/dataY.shape[0]
if __name__=="__main__":
# train
dataX,dataY=read_data("hw4_train.dat")
print("\n13")
wREG=w_reg(dataX,dataY,namuta=10)
Ein=zero_one_cost(pred(wREG,dataX),dataY)
print("the Ein on the train set: ",Ein)
# test
testX,testY=read_data("hw4_test.dat")
Eout=zero_one_cost(pred(wREG,testX),testY)
print("the Eout on the test set: ",Eout)
l=[2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]
print("\n14")
Ein_list=[]
Eout_list=[]
for i in l:
namuta=math.pow(10,i)
wREG=w_reg(dataX,dataY,namuta)
Ein_list.append(zero_one_cost(pred(wREG,dataX),dataY))
Eout_list.append(zero_one_cost(pred(wREG,testX),testY))
id_in=Ein_list.index(min(Ein_list))
plt.figure()
plt.plot(np.power(np.full(shape=(len(l),),fill_value=10,dtype=np.int32),l),Ein_list)
plt.xlabel("namuta")
plt.xlim((math.pow(10,l[0]),math.pow(10,l[-1])))
plt.ylabel("Ein")
plt.savefig("14.png")
print("the namuta with the minimun Ein: ",math.pow(10,l[id_in]))
print("the Eout on such namuta: ", Eout_list[id_in])
print("\n15")
id_out = Eout_list.index(min(Eout_list))
plt.figure()
plt.plot(np.power(np.full(shape=(len(l),),fill_value=10,dtype=np.int32),l),Eout_list)
plt.xlabel("namuta")
plt.xlim((math.pow(10,l[0]),math.pow(10,l[-1])))
plt.ylabel("Eout")
plt.savefig("15.png")
print("the namuta with the minimun Eout: ", math.pow(10, l[id_out]))
trainX=dataX[:120]
trainY=dataY[:120]
validX=dataX[120:]
validY=dataY[120:]
# validation
print("\n16")
Ein_list.clear()
Eout_list.clear()
Eval_list=[]
for i in l:
namuta=math.pow(10,i)
wREG=w_reg(trainX,trainY,namuta)
Ein_list.append(zero_one_cost(pred(wREG,trainX),trainY))
Eout_list.append(zero_one_cost(pred(wREG,testX),testY))
Eval_list.append(zero_one_cost(pred(wREG,validX),validY))
id_in=Ein_list.index(min(Ein_list))
plt.figure()
plt.plot(np.power(np.full(shape=(len(l),),fill_value=10,dtype=np.int32),l),Ein_list)
plt.xlabel("namuta")
plt.xlim((math.pow(10,l[0]),math.pow(10,l[-1])))
plt.ylabel("Ein")
plt.savefig("16.png")
print("the namuta with the minimun Ein: ",math.pow(10,l[id_in]))
print("the Eout on such namuta: ", Eout_list[id_in])
print("\n17")
id_val=Eval_list.index(min(Eval_list))
plt.figure()
plt.plot(np.power(np.full(shape=(len(l),),fill_value=10,dtype=np.int32),l),Eval_list)
plt.xlabel("namuta")
plt.xlim((math.pow(10,l[0]),math.pow(10,l[-1])))
plt.ylabel("Eval")
plt.savefig("17.png")
print("the namuta with the minimun Eval: ",math.pow(10,l[id_val]))
print("the Eout on such namuta: ", Eout_list[id_val])
print("\n18")
wREG=w_reg(dataX,dataY,namuta=math.pow(10,l[id_val]))
Ein=zero_one_cost(pred(wREG,dataX),dataY)
Eout = zero_one_cost(pred(wREG, testX), testY)
print("Ein: ",Ein)
print("Eout: ",Eout)
# 5-fold cross validation
print("\n19")
Eval_list.clear()
splX=np.split(dataX,5,axis=0)
splY=np.split(dataY,5,axis=0)
for j in l:
Eval = 0
namuta=math.pow(10,j)
for i in range(5):
li=[a for a in range(5)]
li.pop(i)
trainX=np.concatenate([splX[k] for k in li],axis=0)
trainY=np.concatenate([splY[k] for k in li],axis=0)
wREG=w_reg(trainX,trainY,namuta)
Eval+=zero_one_cost(pred(wREG,splX[i]),splY[i])/5
Eval_list.append(Eval)
id_val=Eval_list.index(min(Eval_list))
plt.figure()
plt.plot(np.power(np.full(shape=(len(l),),fill_value=10,dtype=np.int32),l),Eval_list)
plt.xlabel("namuta")
plt.xlim((math.pow(10,l[0]),math.pow(10,l[-1])))
plt.ylabel("Ecv")
plt.savefig("19.png")
print("the namuta with the minimun Ecv: ",math.pow(10,l[id_val]))
print("\n20")
wREG=w_reg(dataX,dataY,namuta=math.pow(10,l[id_val]))
Ein=zero_one_cost(pred(wREG,dataX),dataY)
Eout = zero_one_cost(pred(wREG, testX), testY)
print("Ein: ",Ein)
print("Eout: ",Eout)
运行结果
13

14


15


16


17


18

19


20

机器学习基石笔记:Homework #4 Regularization&Validation相关习题的更多相关文章
- 机器学习基石笔记:14 Regularization
一.正则化的假设集合 通过从高次多项式的H退回到低次多项式的H来降低模型复杂度, 以降低过拟合的可能性, 如何退回? 通过加约束条件: 如果加了严格的约束条件, 没有必要从H10退回到H2, 直接使用 ...
- 机器学习基石笔记:Homework #1 PLA&PA相关习题
原文地址:http://www.jianshu.com/p/5b4a64874650 问题描述 程序实现 # coding: utf-8 import numpy as np import matpl ...
- 机器学习基石笔记:Homework #2 decision stump相关习题
原文地址:http://www.jianshu.com/p/4bc01760ac20 问题描述 程序实现 17-18 # coding: utf-8 import numpy as np import ...
- 机器学习基石笔记:Homework #3 LinReg&LogReg相关习题
原文地址:http://www.jianshu.com/p/311141f2047d 问题描述 程序实现 13-15 # coding: utf-8 import numpy as np import ...
- 机器学习基石笔记:15 Validation
一.模型选择问题 如何选择? 视觉上 NO 不是所有资料都能可视化;人脑模型复杂度也得算上. 通过Ein NO 容易过拟合;泛化能力差. 通过Etest NO 能保证好的泛化,不过往往没法提前获得测试 ...
- 机器学习基石:Homework #0 SVD相关&常用矩阵求导公式
- 机器学习基石笔记:13 Hazard of Overfitting
泛化能力差和过拟合: 引起过拟合的原因: 1)过度VC维(模型复杂度高)------确定性噪声: 2)随机噪声: 3)有限的样本数量N. 具体实验来看模型复杂度Qf/确定性噪声.随机噪声sigma2. ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 7 Regularization 正则化
Lecture7 Regularization 正则化 7.1 过拟合问题 The Problem of Overfitting7.2 代价函数 Cost Function7.3 正则化线性回归 R ...
- 林轩田机器学习基石笔记1—The Learning Problem
机器学习分为四步: When Can Machine Learn? Why Can Machine Learn? How Can Machine Learn? How Can Machine Lear ...
随机推荐
- PHP 工程师技能图谱
# PHP 工程师技能图谱## 基础知识 - HTTP - HEADER - REQUEST - RESPONSE - GET/POST/PUT/DELETE/PATCH/CONNECT/OPTION ...
- 测试过程中bug分类
测试的核心任务是发现bug.在这之前是分析需求,之后是跟踪bug.跳出具体的项目来看,所有的bug无非是以下五大类. 软件没有实现应该实现的功能:如指定的登录功能. 软件出现了本应该避免的错误:如用户 ...
- JSON与Java对象的互相转换
JSON与Java对象的互相转换 例一(单个对象进行赋值): @RequestMapping("test1.do") @ResponseBody public JSONObject ...
- Dubbo入门到精通学习笔记(十八):使用Redis3.0集群实现Tomcat集群的Session共享
文章目录 1.单节点访问http://192.168.1.61:8082/pay-web-boss/: 2.增加多一个消费者节点:192.168.1.62,以同样的方式部署pay-web-boss工程 ...
- vue input框type=number 保留两位小数自定义组件
第一步:自定义组件MyNumberInput.vue<template> <input class="numberInput" type="number ...
- 实用maven笔记三-仓库
maven管理依赖的一个很重要的基础在于,其维护了收集大量依赖jar包的仓库. maven的仓库分类为本地仓库和远程仓库. 构件在仓库的路径大致为:groupId/artifactId/version ...
- SpringBoot集成Thymeleaf模板
1.添加起步依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...
- CTU OPEN 2017 Pond Cascade /// 思维
题目大意: 给定N F 给定N个水池的大小 每个水池都以流量F开始注水 当位置较前的水池注满后 水会溢出到下一个水池 求 最后一个水池开始溢出的时间 和 所有水池全部注满的时间 1.最后一个n水池开始 ...
- HTML + CSS (下)【更新中】
弹性盒子: 定义:弹性盒子模型是css3中新提出的一种布局方案.是一种为了应对针对不同屏幕宽度不同设备的一整套新的布局方案. 主要是对一个容器中的子元素进行排列.对齐和分配空白空间的方案的调整. 新旧 ...
- LeetCode Array Easy 66. Plus One
Description Given a non-empty array of digits representing a non-negative integer, plus one to the i ...