《机器学习基石》作业一第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. "
0 0
原创粉丝点击