double112233

HTML HW1

Oct 15th, 2020
1,048
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. def dot(a,b):
  3.     c=0
  4.     for i in range(len(a)):
  5.         c=c+a[i]*b[i]
  6.     return c
  7. def update(a,b,y):
  8.     c=[]
  9.     for i in range(len(a)):
  10.         c.append(a[i]+b[i]*y)
  11.     # print(c)
  12.     return c
  13.  
  14.  
  15. # parameters
  16. x0=0
  17. multi=0.25
  18.  
  19.  
  20. # main function
  21. tot=[]
  22. w0=[]
  23. a=[];
  24. for i in range(100):
  25.     b=[x0];
  26.     for j in input().split():
  27.         b.append(float(j)*multi);
  28.     a.append(b)
  29. for x in range (1000):
  30.     random.seed(x)
  31.     cnt_miss=cnt_update=0
  32.     w=[x0]
  33.     for i in range(10):
  34.         w.append(0)
  35.     while cnt_miss<500:
  36.         t=random.randrange(100)
  37.         if dot(w,a[t][:11:])*a[t][11]>0:
  38.             cnt_miss=cnt_miss+1
  39.         else :
  40.             w=update(w,a[t][:11:],a[t][11])
  41.             cnt_miss=0
  42.             cnt_update=cnt_update+1
  43.     # print(cnt_update)
  44.     tot.append(cnt_update)
  45.     w0.append(w[0])
  46. tot.sort()
  47. w0.sort()
  48. print("the median number of updates "+str(tot[500]))
  49. print("the median of w0 "+str(w0[500]))
RAW Paste Data