《机器学习基石》作业一第17题的程序
来源:互联网 发布:四川驾培网软件下载 编辑:程序博客网 时间:2024/05/07 00:54
import os
import sys
import random
# An example in that book, the training set and parameters' sizes are fixed
#training_set = [[(3, 3), 1], [(4, 3), 1], [(1, 1), -1]]
training_set=[]
for line in sys.stdin:
arr= line.split('\t')
tag=int(arr[1].strip(' \n'))
data_items=arr[0].split(' ')
iterms=()
item=[]
for term in data_items:
str1=term.strip(' ')
iterms+=(float(str1),)
item.append(iterms)
item.append(tag)
training_set.append(item)
circle=range(len(training_set))
random.shuffle(circle)
w = [0, 0,0,0]
b = 0
global steps
steps=0
# update parameters using stochastic gradient descent
def update(item):
global w, b
global steps
ratio=0.5
steps+=1
w[0] = w[0] + ratio * item[1] * item[0][0]
w[2] = w[2] + ratio * item[1] * item[0][2]
w[3] = w[3] + ratio * item[1] * item[0][3]
w[1] = w[1] + ratio * item[1] * item[0][1]
b = b + ratio * item[1]
# print w, b # you can uncomment this line to check the process of stochastic gradient descent
# calculate the functional distance between 'item' an the dicision surface
def cal(item):
global w, b
res = 0
for i in range(len(item[0])):
res += item[0][i] * w[i]
res += b
res *= item[1]
if item[1]==-1 and res==0 :
return 1
return res
# check if the hyperplane can classify the examples correctly
def check():
flag = False
global circle
for k in circle:
item=training_set[k]
if cal(item) <= 0:
flag = True
update(item)
if not flag:
# print "RESULT: w: " + str(w) + " b: "+ str(b)
print steps
os._exit(0)
flag = False
if __name__=="__main__":
for i in range(1000):
check()
print steps
print "The training_set is not linear separable. "
import sys
import random
# An example in that book, the training set and parameters' sizes are fixed
#training_set = [[(3, 3), 1], [(4, 3), 1], [(1, 1), -1]]
training_set=[]
for line in sys.stdin:
arr= line.split('\t')
tag=int(arr[1].strip(' \n'))
data_items=arr[0].split(' ')
iterms=()
item=[]
for term in data_items:
str1=term.strip(' ')
iterms+=(float(str1),)
item.append(iterms)
item.append(tag)
training_set.append(item)
circle=range(len(training_set))
random.shuffle(circle)
w = [0, 0,0,0]
b = 0
global steps
steps=0
# update parameters using stochastic gradient descent
def update(item):
global w, b
global steps
ratio=0.5
steps+=1
w[0] = w[0] + ratio * item[1] * item[0][0]
w[2] = w[2] + ratio * item[1] * item[0][2]
w[3] = w[3] + ratio * item[1] * item[0][3]
w[1] = w[1] + ratio * item[1] * item[0][1]
b = b + ratio * item[1]
# print w, b # you can uncomment this line to check the process of stochastic gradient descent
# calculate the functional distance between 'item' an the dicision surface
def cal(item):
global w, b
res = 0
for i in range(len(item[0])):
res += item[0][i] * w[i]
res += b
res *= item[1]
if item[1]==-1 and res==0 :
return 1
return res
# check if the hyperplane can classify the examples correctly
def check():
flag = False
global circle
for k in circle:
item=training_set[k]
if cal(item) <= 0:
flag = True
update(item)
if not flag:
# print "RESULT: w: " + str(w) + " b: "+ str(b)
print steps
os._exit(0)
flag = False
if __name__=="__main__":
for i in range(1000):
check()
print steps
print "The training_set is not linear separable. "
0 0
- 《机器学习基石》作业一第17题的程序
- 《机器学习基石》作业一
- 机器学习基石作业4第5题
- 机器学习基石 作业1 程序题(15-20)
- 机器学习基石 4.5 作业一
- 机器学习基石作业一PLA算法
- 机器学习基石作业1-17-PLA 的c++实现
- 机器学习基石第二次作业
- 机器学习基石作业1-Pocket_PLA 的c++实现
- 台大机器学习基石作业编程题
- 机器学习基石系列一
- 机器学习基石第四次作业代码
- 机器学习基石第三天学习笔记
- 机器学习基石 作业4 带Regularizer和Cross Validation的线性回归分类器
- 机器学习基石—作业1(15-20题PLA编程)
- 机器学习基石—作业2(16-20题Python实现)
- 机器学习基石 作业1 实现PLA和Pocket算法
- 林轩田-机器学习基石-作业1-python源码
- VC++中将对话框的背景设为自己想要的图片,增加打开文件按钮
- JAVA大作业 - 五道三星题
- Java运行环境的搭建
- CSU-1716 Morse(模拟字典暴力查找)From湖南省多校对抗赛(2016.03.27)
- Standard_C_字符和字符串操作
- 《机器学习基石》作业一第17题的程序
- Codeforces 660D Number of Parallelograms
- 简单二叉树的层次遍历
- 策略模式(Strategy Pattern)
- PCA主成分分析
- Oracle数据库--实用操作(5)过程,函数,程序包
- 1127 最短的包含字符串
- 山东省第一届ACM省赛 B SDUT 2152 Balloons(简单的BFS)
- Java集合框架复习之规则集Set-LinkedHashSet(四)