numpy实现回归树

import numpy as np     import time      def pla(x,y):     k=y/x     deg=np.rad2deg(np.arctan(k))     if k>0:         if x<0:             deg=deg+180     if k<0:         if x<0:             deg=deg+180     if deg<0:         deg=360+deg     return deg def yangangle(dogangle,direction,x,y):     angle=dogangle+direction*(135)     angle1=pla(x,y)     if angle>angle1:         if angle-angle1>90:             angle=angle1+90     if angle<angle1:         if angle1-angle>90:             angle=angle1-90     return angle class node:     def __init__(self,index,value,value1):         self.index=index         self.value=value         self.value1=value1         self.left=None         self.right=None def com(da):     sum=0     mid=0     for i in range(len(da)):         mid=mid+da[i,2]     mid=mid/len(da)     for i in range(len(da)):         sum=sum+(da[i,2]-mid)*(da[i,2]-mid)        return sum def los(j,i,data):     ya1=[]     ya2=[]     for i1 in range(len(data)):         if data[i1,i]<=data[j,i]:             ya1.append(data[i1])         else:             ya2.append(data[i1])     ya1=np.array(ya1)     ya2=np.array(ya2)     loss=0     if len(ya1)>1:         loss=loss+com(ya1)     if len(ya2)>1:         loss=loss+com(ya2)     return loss,i,data[j,i] start=time.time() x=np.zeros((340,1)) y=np.zeros((len(x),1)) deg=np.zeros((len(x),1)) depdeg=np.zeros((len(x),1)) dogx=np.zeros((len(x),1)) dogy=np.zeros((len(x),1)) lable=np.zeros((len(x),1))
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)
上一篇:大连理工大学矩阵与数值分析上机实验


下一篇:用numpy创建零数组