机器学习基石笔记: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 ...
随机推荐
- MySQL高级学习笔记(三):Mysql逻辑架构介绍、mysql存储引擎
文章目录 Mysql逻辑架构介绍 总体概览 总体概览 mysql存储引擎 查看命令 看你的 mysql 现在已提供什么存储引擎 : 看你的 mysql 当前默认的存储引擎 : 各个引擎简介 MyISA ...
- LightOJ 1030 Discovering Gold (概率/期望DP)
题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...
- [ERR] 153 - Got a packet bigger than 'max_allowed_packet' bytes
异常原因: 用客户端导入数据的时候,信息包过大 ,终止了数据导入,需要修改max_allowed_packet 参数 解决方案: 1. sql语句修改( ⚠️重启服务设置会失效) 登陆mysql查看当 ...
- MYSQL索引的深入学习
通常大型网站单日就可能会产生几十万甚至几百万的数据,对于没有索引的表,单表查询可能几十万数据就是瓶颈. 一个简单的对比测试 以我去年测试的数据作为一个简单示例,20多条数据源随机生成200万条数据,平 ...
- 高级UI晋升之自定义view实战(七)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章自定义ViewGroup实现瀑布流效果来进行详解dispatchTouch ...
- Scrapy框架: pipelines.py设置
保存数据到json文件 # -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your p ...
- CentOS MySQL 5.7编译安装
CentOS MySQL 5.7编译安装 MySQL 5.7 GA版本的发布,也就是说从现在开始5.7已经可以在生产环境中使用,有任何问题官方都将立刻修复. MySQL 5.7主要特性: 更好的性能: ...
- springboot多数据库及分布式事务配置
1.导入相应的jar包依赖 <!-- 集成mybatis --> <dependency> <groupId>org.mybatis.spring.boot< ...
- CG-CTF web部分wp
bin不动了,学学webWEB1,签到1f12,得到flag2,签到2给了输入窗口和密码,但输入后却显示错误,查看源码,发现对输入长度进行了限制,改下长度,得到flag3,md5 collision给 ...
- 搜索solr
这是我第一次写博客,没有系统性.专业性,东西很杂,也不知道自己在写些什么. SOA分布式架构,所以,使用solr,搜索层的服务层需要搭建起来.搜索系统的表现层搭建 ,打包方式是war包 域名改变代表系 ...