#OK to post homework #Jingze Li(RUID:195000313),02/24/19 #############READ ME########### #the homework 9 consists of 4 parts, corresponding to 2 to 5 problems of hw9. ################ Help:=proc() print(`PA1(L) , ArData(L1,H1,N,c), TestP(L,c) , NoisyData(L1,H1,N,c,p) `): end: ###################from c9.txt############# #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: ########hw 9############### ######### 2 ############ #ArDataMV(A,v,N) input a number A for range [-A,A], v for #the criterion vecter, and N for the number of data #generated. #Note: to generate decimal numbers, please input A.0 rather #than A, A will generate random number on Z. ArDataMV:=proc(A,v,N) local L,ra,i,t,n,j: ra:=rand(-A..A): n:=nops(v): L:=[]: for i from 1 to N do t:=[seq(ra(), j=1..n)]: t:=v*~t: if sum(t[j],j=1..n)>=0 then L:=[op(L),[t,1]]: else L:=[op(L),[t,0]]: fi: od: L: end: ############ 3############## #the following pakage is needed to do dot product with(LinearAlgebra): #nD perceptron algorithm, #PAmv(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 PA1mv:=proc(L,n) local i,c: c:=[seq(0,i=1..n)]: for i from 1 to nops(L) do if L[i][2]=1 and DotProduct(L[i][1],c)<0 then #False positive thens c:=c + L[i][1]: elif L[i][2]=0 and DotProduct(L[i][1],c)>=0 then #False negative then c:=c-L[i][1]: fi: od: c: end: ############## 4 ############## #TestPMV(L,v) inputs a list of data L, and the criterion vecter v, output the number of negative false fn, the number of positive false fp, and the total number of data. TestPMV:=proc(L,v) local fn,fp,i: fn:=0: fp:=0: for i from 1 to nops(L) do if DotProduct(L[i][1],v)>=0 and L[i][2]=0 then fp:=fp+1: elif DotProduct(L[i][1],v)<0 and L[i][2]=1 then fn:=fn+1: fi: od: [fn,fp,nops(L)]: end: ########### 5 ################## #PA1corrected(L), with respect to 1 dim perceptron algorithm, an correction of the PA1. The correction is to shift the data into 2 dim vecters by setting 1 as another entry. In this way we put our data in the line in the plane satisfying y=1. Then we can divide them with a line trough the origin. PA1corrected:=proc(L) local i,c,n,LL: c:=0: LL:=[]: n:=nops(L): for i from 1 to n do LL:=[op(LL),[ [L[i][1],1],L[i][2] ] ]: od: PA1mv(LL,2): end: # to compare, we need the following data transformation procedure: #DataTrans(L) adding 1 as second entry DataTrans:=proc(L) local i,LL: LL:=[]: for i from 1 to nops(L) do LL:=[op(LL),[ [L[i][1],1],L[i][2] ] ]: od: LL: end: # if we do #L:=NoisyData(-1,1.0,1000,0,0.1): #TestPMV(DataTrans(L),PA1corrected(L)) #TestP(L,PA1(LL)) # #After some tests, I find that the result is very unstable #if we turn to artificial data, they both have no false negatives or positives.