test_x=np.zeros((20,1)) test_y=np.zeros((len(test_x),1)) test_deg=np.zeros((len(test_x),1)) test_depdeg=np.zeros((len(test_x),1)) test_dogx=np.zeros((len(test_x),1)) test_dogy=np.zeros((len(test_x),1)) test_lable=np.zeros((len(test_x),1))
def predict(data,node): if node.left==None: return node.value1 if data[node.index]<=node.value: return predict(data,node.left) else: return predict(data,node.right)
for i in range(len(x)): x[i]=np.random.randint(0,10) deg[i]=np.random.randint(0,360) y[i]=np.random.randint(0,10) depdeg[i]=pla(x[i],y[i]) dogx[i]=20*np.cos(np.deg2rad(deg[i])) dogy[i]=20*np.sin(np.deg2rad(deg[i])) lable[i]=yangangle(deg[i],1,x[i],y[i])
for i in range(len(test_x)): test_x[i]=np.random.randint(0,10) test_deg[i]=np.random.randint(0,360) test_y[i]=np.random.randint(0,10) test_depdeg[i]=pla(test_x[i],test_y[i]) test_dogx[i]=20*np.cos(np.deg2rad(test_deg[i])) test_dogy[i]=20*np.sin(np.deg2rad(test_deg[i])) test_lable[i]=yangangle(test_deg[i],1,test_x[i],test_y[i])
test=np.concatenate((test_depdeg,test_deg,test_lable),axis=1)
train=np.concatenate((depdeg,deg,lable),axis=1) def create(data): min=float('inf') index=0 value=0 value1=0 if len(data)==1: value1=data[0,2] no=node(index,value,value1) return no else: for i in range(2): for j in range(len(data)): k,ind,val=los(j,i,data) if k<min: min=k index=ind value=val value1=data[0,2] no=node(index,value,value1) ya1=[] ya2=[] for i2 in range(len(data)): if data[i2,index]<=value: ya1.append(data[i2]) else: ya2.append(data[i2]) ya1=np.array(ya1) ya2=np.array(ya2) if len(ya1)==0 or len(ya2)==0: return no flag=0 for i4 in range(len(data)-1): if data[i4,2]!=data[i4+1,2]: flag=1 if flag==0: ya1=[] ya2=[] i7=0 for i7 in range(len(data)): if i7%2==0: ya1.append(data[i7]) else: ya2.append(data[i7]) ya1=np.array(ya1) ya2=np.array(ya2) no.left=create(ya1) no.right=create(ya2) return no no.left=create(ya1) no.right=create(ya2) return no no=create(train) sum=0 end=time.time() print(end-start) start1=time.time() for i in range(len(test)): lable=predict(test[i],no) sum=sum+(lable-test[i,2])*(lable-test[i,2]) print(sum) end1=time.time() print(end1-start1)