#Terence Coelho Hw 9 2-24-2019 #importing #C9.txt Feb. 21, 2019 Help:=proc() print(`PA1(L) , ArData(L1,H1,N,c), TestP(L,c) , NoisyData(L1,H1,N,c,p) `): end: #L:=[[TadeusBalaban, #Publ , #Citations, #h-index ,1], # [RobertBeals, 2], #.... # [DoronZeilberger, 210,2404, , 2], # [WujunZhang, ]] #1D perceptron algorithm, #PA1(L): inputs a list of pairs [heightIncm,{0,1}] where 0 if she or he did not make it 1 if he or she did #outputs a numnber c a "predictor" that if x>c then they get to be admitted PA1:=proc(L) local i,c,n: c:=0: n:=nops(L): for i from 1 to n do if L[i][2]=1 and L[i][1]c then #False negative then c:=c+L[i][1]: fi: od: c: end: #inputs L1 and H1 (low and high heights in basketball players and the number N of applicants #and a cut-off c that >=c gets 1 (admitted) and 0 (rejected) ArData:=proc(L1,H1,N,c) local L,ra,i,t: ra:=rand(L1..H1): L:=[]: for i from 1 to N do t:=ra(): if t>=c then L:=[op(L),[t,1]]: else L:=[op(L),[t,0]]: fi: od: L: end: #Inputs a data set of pairs [height,1 or 0] and a cut-off c, outputs the #triple # of false negatives and # of false positive, total number of data entries TestP:=proc(L,c) local fn,fp,i: fn:=0: fp:=0: for i from 1 to nops(L) do if L[i][1]>=c and L[i][2]=0 then fp:=fp+1: elif L[i][1]=c gets 1 (admitted) and 0 (rejected) and a prob. p of mis-classifying #outputs the noisy data set NoisyData:=proc(L1,H1,N,c,p) local L,ra,i,t,ra1: ra:=rand(L1..H1): ra1:=rand(0.0..1.0): L:=[]: for i from 1 to N do t:=ra(): if t>=c then if ra1()<=p then L:=[op(L),[t,0]]: else L:=[op(L),[t,1]]: fi: else if ra1()<=p then L:=[op(L),[t,1]]: else L:=[op(L),[t,0]]: fi: fi: od: L: end: #problems start here #dot product dot:=proc(L1,L2) local ret,i: ret:=0: for i from 1 to nops(L1) do ret:=ret+L1[i]*L2[i] od: ret: end: #2. #ArDataMV(A,v,n): given a list v, and an integer A, generates nops(v) random numbers from [-A,A] x, # then adds pair [x,1]/[x,0] if v.x>0, v.x<=0 respectively. Does this n times. ArDataMV:=proc(A,v,n) local s,i,ret,x,j,ra: ret:=[]: s:=nops(v): ra:=rand(-A*1.0...A*1.0): for j from 1 to n do x:=[]: for i from 1 to s do x:=[op(x),ra()]: od: if dot(x,v)<=0 then ret:=[op(ret),[x,0]]: fi: if dot(x,v)>0 then ret:=[op(ret),[x,1]]: fi: od: ret: end: #3 #PAmv(L,n): given list of data of form [x,{0,1}] where x has length n, find x that splits data as above. PAmv:=proc(L,n) local w,b,i,flag,L2,j: w:=[0$n]: b:=0: flag:=-1: L2:=L: #Going to use -1 instead of 0 to use sign function for shorter code. for i from 1 to nops(L2) do if L2[i][2]=0 then L2[i][2]:=-1: fi: od: for j from 1 to 1000*n while flag=-1 do flag:=1: for i from 1 to nops(L2) do if not sign(dot(L2[i][1],w)+b)=L2[i][2] then w:= w+L2[i][2]*L2[i][1]: b:=b+L2[i][2]: flag:=-1: fi: od: od: [w,b]: end: #4: #Inputs a data set of pairs [x,1 or 0] and a "cut-off" v and n outputs the #triple of false negatives and # of false positive, total number of data entries TestP:=proc(L,v) local fn,fp,i: fn:=0: fp:=0: for i from 1 to nops(L) do if dot(v,L[i][1])<0 and L[i][2]=1 then fp:=fp+1: elif dot(v,L[i][1])>0 and L[i][2]=0 then fn:=fn+1: fi: od: [fn,fp,nops(L)]: end: #5: #Given list of [height,{0,1}], map to list of [[height,1],{0,1}], then use procedure PAmv(L,n) to get #a,b such that L[i][2]=1 in case ax+b>=0 #Since the perceptron algorithm used above accidentally already outputs of form mx+b, assuming an implicit #1 in the entries, this will be just: PA1corrected:=proc(L): PAmv(L,1): end: #The original PA1 could potentially loop forever if one were to make it loop until it didn't misclassify #any data. I got such a list experimentally when testing the method in class but can't seem to get lucky now.