# Ok to post homework. # Name: Tong Cheng Data: Feb 23th, 2019 Assignment: 9 #------------------------------------------------------------------ # Part 2: # ArData(A,v,N) # inputs a pos. number A, and a vector of numbers v (given as a list), (let n:=nops(v)) # outputs a list of length N where each entry is a pair obtained by picking a random # point (a list of length n) in [-A,A]^n and returns the pair [x,0] if v.x (the dot product # of v and x) is negative and [x,1] if it is zero or positive ArData:=proc(A,v,N) local L, ra, ra_point, vec_pro, n, i, j: #A:=abs(A): ra:=rand(-convert(A,float)..convert(A,float)): n:=nops(v): L:=[]: for j from 1 to N do ra_point:=[seq(ra(), i=1..n)]: vec_pro:=sum(ra_point[i]*v[i], i=1..n): if vec_pro < 0 then L:=[op(L), [ra_point,0]]: else L:=[op(L), [ra_point,1]]: fi: od: L: end: #----------------------------------------------------------------------- # Part 3: # PAmv(L,n) # inputs a list containing data of the form [VectorOfLenghn,ZeroOrOne] where, # VectorOfLengthn is a list of length n, and n is the dimension. # To Implement the Perceptron algorithm in n dimensions (n arbitrary) PAmv:=proc(L,n) local ra, v, i, Data, Num_Data, j, wrong, ratio, vec_pro: ra:=rand(0.0..1.0): v:=[seq(ra(), i=1..n)]: Num_Data:=nops(L): for j from 1 to Num_Data do vec_pro:=sum(L[j][1][i]*v[i], i=1..n): if vec_pro<0 and L[j][2]=1 then v:=[seq(v[i]+L[j][1][i], i=1..n)]: elif vec_pro >=0 and L[j][2]=0 then v:=[seq(v[i]-L[j][1][i], i=1..n)]: fi: od: v: end: #----------------------------------------------------------------- # Part 4: # Code from C9.txt #|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| #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]=0 and L[j][2]=0 then fp:=fp+1: fi: od: [fn, fp, Num_Data]: end: #-------------------------------------------------------------------------- # Part 5: # PA1corrected(L) # That first transfoms the list L to the format [[data,1],ZeroOrOne], and uses your # procedure PAmv(L,n) with n=2 to outputs two numbers a and b such that L[i][2]=1 # iff ax+b>=0. Compare its performance to PA1(L). PA1corrected:=proc(L) local Data, i, v: Data:=[seq([[L[i][1],1],L[i][2]], i=1..nops(L))]: v:=PAmv(Data, 2): v: end: # Code 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_C9:=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: #|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||s (* The experimental result is as follows: Djkk:=ArData_C9(180,220, 1000, 190): c1:=PA1(Djkk) c1 := 189 TestP(Djkk,c1) [0, 26, 1000] c2:=PA1corrected(Djkk) c2 := [167.1022349, -17.40172346] Djkkk:=[seq([[Djkk[i][1],1],L[i][2]], i=1..nops(Djkk))]: TestPMV(Djkkk,c2) [0, 0, 1000] *)