importnumpyasnpimportmatplotlib.pyplotaspltimporttimeclassData:''' Object of labeled data '''def__init__(self,x,y):self.x=xself.y=ydefread_data(file_name):''' function that read the data from file and parse them into the form of class Data '''X=[]Y=[]withopen(file_name,'rt')asf:forlninf.readlines():ln=ln.split()X.append(np.array([1.0]+[float(x)forxinln[:-1]]))Y.append(int(ln[-1]))X=np.array(X)Y=np.array(Y)returnnp.array([Data(x,y)forx,yinzip(X,Y)])classPLA:def__init__(self,file_name,shuffle=False,nn=1):self.D=read_data(file_name)self.w_f=Noneself.shuffle=shuffleself.nn=nn@staticmethoddefsign(x):return1ifx>0else-1@staticmethoddeftest(d,w):returnPLA.sign(d.x@w)==d.ydefrun(self):D=np.copy(self.D)ifself.shuffle:np.random.shuffle(D)w=np.zeros(5)upd=0done=Falsewhilenotdone:done=TruefordinD:ifnotPLA.test(d,w):done=Falseupd+=1w+=self.nn*d.y*d.xself.w_f=wself.upd=updif__name__=='__main__':pla=PLA('hw1_6_train.dat',shuffle=True,nn=1)T=1126tot_upd=0max_upd=0upd_time_list=[]for_inrange(T):np.random.seed(int(time.clock()*1000000000)%(1<<31-1))pla.run()tot_upd+=pla.updmax_upd=max(max_upd,pla.upd)upd_time_list.append(pla.upd)print(f"average # of update: {tot_upd/T}")# plot histogramfig,ax=plt.subplots()num_bins=max_updn,bins,patches=ax.hist(upd_time_list,num_bins,facecolor='purple',alpha=0.5)# prettifyax.set_title('Figure 1: PLA statistics')ax.set_xlabel('number of updates')ax.set_ylabel('number frequency')fig.tight_layout()plt.grid(True)plt.show()
importnumpyasnpimportmatplotlib.pyplotaspltimporttime,randomclassData:def__init__(self,x,y):self.x=xself.y=ydefread_data(file_name):''' function that read the data from file and parse them into the form of class Data '''X=[]Y=[]withopen(file_name,'rt')asf:forlninf.readlines():ln=ln.split()X.append(np.array([1.0]+[float(x)forxinln[:-1]]))Y.append(int(ln[-1]))X=np.array(X)Y=np.array(Y)returnnp.array([Data(x,y)forx,yinzip(X,Y)])classVerifier:def__init__(self,file_name):self.test_set=read_data(file_name)defverify(self,w):''' return the error rate of w on self.test_set '''err_cnt=0fordinself.test_set:ifnotPLA.test(d,w):err_cnt+=1returnerr_cnt/len(self.test_set)classPLA:def__init__(self,file_name,upd_time=50):self.D=read_data(file_name)self.w_pocket=Noneself.w_n=Noneself.upd_time=upd_time@staticmethoddefsign(x):return1ifx>0else-1@staticmethoddeftest(d,w):returnPLA.sign(d.x@w)==d.ydefget_err_cnt(self,w):err_w=0fordinself.D:ifnotPLA.test(d,w):err_w+=1returnerr_wdefrun(self):w=np.zeros(5)w_pocket=np.zeros(5)err_wp=self.get_err_cnt(w_pocket)upd=0whileupd<self.upd_time:d=random.choice(self.D)ifnotPLA.test(d,w):upd+=1w+=d.y*d.xerr_w=self.get_err_cnt(w)iferr_w<err_wp:w_pocket=np.copy(w)err_wp=err_wself.w_pocket=w_pocketself.w_n=wif__name__=='__main__':pla=PLA('hw1_7_train.dat',upd_time=100)vrfy=Verifier('hw1_7_test.dat')T=1126tot_err_rate=0.0err_rate_list=[]for_inrange(T):print(_)random.seed(int(time.process_time()*1000000000)%(1<<32-1))pla.run()err_rate=vrfy.verify(pla.w_pocket)tot_err_rate+=err_rateerr_rate_list.append(err_rate)print(tot_err_rate/T)# plot histogramfig,ax=plt.subplots()num_bins=100n,bins,patches=ax.hist(err_rate_list,num_bins,facecolor='green',alpha=0.5)# prettifyax.set_title('Figure 2: Pocket Algorithm statistics of $w_{pocket}$')ax.set_xlabel('error rate')ax.set_ylabel('frequency')fig.tight_layout()plt.grid(True)plt.show()
importnumpyasnpimportmatplotlib.pyplotaspltimporttime,randomclassData:def__init__(self,x,y):self.x=xself.y=ydefread_data(file_name):''' function that read the data from file and parse them into the form of class Data '''X=[]Y=[]withopen(file_name,'rt')asf:forlninf.readlines():ln=ln.split()X.append(np.array([1.0]+[float(x)forxinln[:-1]]))Y.append(int(ln[-1]))X=np.array(X)Y=np.array(Y)returnnp.array([Data(x,y)forx,yinzip(X,Y)])classVerifier:def__init__(self,file_name):self.test_set=read_data(file_name)defverify(self,w):''' return the error rate of w on self.test_set '''err_cnt=0fordinself.test_set:ifnotPLA.test(d,w):err_cnt+=1returnerr_cnt/len(self.test_set)classPLA:def__init__(self,file_name,upd_time=50):self.D=read_data(file_name)self.w_pocket=Noneself.w_n=Noneself.upd_time=upd_time@staticmethoddefsign(x):return1ifx>0else-1@staticmethoddeftest(d,w):returnPLA.sign(d.x@w)==d.ydefget_err_cnt(self,w):err_w=0fordinself.D:ifnotPLA.test(d,w):err_w+=1returnerr_wdefrun(self):w=np.zeros(5)w_pocket=np.zeros(5)err_wp=self.get_err_cnt(w_pocket)upd=0whileupd<self.upd_time:d=random.choice(self.D)ifnotPLA.test(d,w):upd+=1w+=d.y*d.xerr_w=self.get_err_cnt(w)iferr_w<err_wp:w_pocket=np.copy(w)err_wp=err_wself.w_pocket=w_pocketself.w_n=wif__name__=='__main__':pla=PLA('hw1_7_train.dat',upd_time=100)vrfy=Verifier('hw1_7_test.dat')T=1126tot_err_rate=0.0err_rate_list=[]for_inrange(T):print(_)random.seed(int(time.process_time()*1000000000)%(1<<32-1))pla.run()err_rate=vrfy.verify(pla.w_n)tot_err_rate+=err_rateerr_rate_list.append(err_rate)print(tot_err_rate/T)# plot histogramfig,ax=plt.subplots()num_bins=100n,bins,patches=ax.hist(err_rate_list,num_bins,facecolor='crimson',alpha=0.5)# prettifyax.set_title('Figure 3: Pocket Algorithm statistics of $w_{100}$')ax.set_xlabel('error rate')ax.set_ylabel('frequency')fig.tight_layout()plt.grid(True)plt.show()