Skip to content
Snippets Groups Projects
Select Git revision
  • nightly_master
  • master default
  • cmake_ana_dev
  • fairroot_target_dev
  • cmake_remaining_files
  • cmake_test_dev
  • cmake_mq_dev
  • cmake_reco_dev
  • cmake_test2
  • cmake_test1
  • cmake_test
  • cmake-dev
  • temp
  • APR21
  • dev_2021_20
  • dev_2021_19
  • RC3_APR21
  • dev_2021_18
  • RC2_APR21
  • dev_2021_17
  • RC1_APR21
  • dev_2021_16
  • dev_2021_15
  • dev_2021_14
  • dev_2021_13
  • dev_2021_12
  • dev_2021_11
  • dev_2021_10
  • dev_2021_09
  • dev_2021_08
  • dev_2021_07
  • dev_2021_06
32 results

Dart.sh

Blame
  • Forked from Computing / cbmroot
    Source project has a limited visibility.
    XGBoostExplain.py 6.42 KiB
    import pickle
    import numpy as np
    import matplotlib
    from matplotlib import pyplot as plt
    import scipy
    from sklearn import svm
    import random
    
    def f_pred(Theta,h,X):
        x = np.arange(-7,7,0.2)
        y = x*np.tan(Theta) + h
        if -np.pi/2 < Theta  and Theta  < np.pi/2:
            return 2*np.array([np.interp(item[0],x,y) < item[1]  for item in X])-1
        else:
            return 2*np.array([np.interp(item[0],x,y) > item[1]  for item in X])-1
        
    def f_pred1(Theta,h,item):
        x = np.arange(-7,7,0.2)
        y = x*np.tan(Theta) + h
        return 2*int(np.interp(item[0],x,y) < item[1] ) -1
    
    
    
    def PlotF(F,ax,n,bar,text):
        c = ax.pcolormesh(X1, Y1, F,alpha=0.5, vmin=-1, vmax=1)
        ax.scatter(x1_samples[:,0],x1_samples[:,1],  marker='o',label= 'Electron (-1)')
        ax.scatter(x2_samples[:,0],x2_samples[:,1],  marker='o',label= 'Pion (+1)')
        ax.set_xlim([-3,3])
        ax.set_ylim([-4,4])
        ax.set_ylabel("Detector 2 "+ r'$(x_2)$')
        ax.set_xlabel("Detector 1 "+ r'$(x_1)$')
        if text is True:
            textstr = "Iteration "+ r'$ i=$' +str(n)
            props = dict(boxstyle='round', facecolor='wheat', alpha=0.9)
            ax.text(0.3,0.9, textstr,  horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, bbox=props)
            
        if bar is True:
            cbar = fig.colorbar(c, ax=ax)
            cbar.set_label('Classification', rotation=270, labelpad=15)
            return cbar
        ax.legend(loc = 'lower right')
        
    
    def CreatePoints():
        mu_vec1 = np.array([0,0])
        cov_mat1 = np.array([[2,0],[0,2]])
        x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, 100)
        mu_vec1 = mu_vec1.reshape(1,2).T # to 1-col vector
        mu_vec2 = np.array([1,2])
        cov_mat2 = np.array([[1,0],[0,1]])
        x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)
        mu_vec2 = mu_vec2.reshape(1,2).T
        with open("XGExplain.pkl", 'wb') as f:
            pickle.dump([mu_vec1 , cov_mat1, x1_samples, mu_vec1 ,mu_vec2 ,cov_mat2,x2_samples , mu_vec2 ], f)
    
    
    #CreatePoints()
    with open("XGExplain.pkl", 'rb') as file:
        mu_vec1 , cov_mat1, x1_samples, mu_vec1 ,mu_vec2 ,cov_mat2,x2_samples , mu_vec2  =  pickle.load(file)
         
         
    X = np.concatenate((x1_samples,x2_samples), axis = 0)
    Y = np.array([-1]*100 + [1]*100)
    Theta = np.arange(-np.pi,np.pi,0.1)
    Hight = np.arange(-4,4,0.1)
    x1 = np.linspace(-5, 5, 100)
    y1 = np.linspace(-5, 5, 100)
    X1, Y1 = np.meshgrid(x1, y1)
    
    
    def Plot1():
        global fig
        fig, axs = plt.subplot_mosaic([['a)','b)']], constrained_layout=True, figsize=(10,4))
        mini = np.array([[np.sum((Y-f_pred(T,H,X))**2) for T in Theta] for H in Hight])
        Theta_min = Theta[np.unravel_index(mini.argmin(), mini.shape)[1]]
        H_min = Hight[np.unravel_index(mini.argmin(), mini.shape)[0]]
        xx = np.arange(-7,7,0.2)
        yy = xx*np.tan(Theta_min) + H_min
        axs['a)'].plot(xx, yy, 'k-')
        F =  np.array([[f_pred1(Theta_min,H_min,[x,y]) for x in x1] for y in y1 ])
        cbar = PlotF(F,axs['a)'],0,True,False)
        axs['a)'].legend(loc = 'lower right')
        cbar.set_label(r'$F(x_1,x_2)$', rotation=270, labelpad=15)
        
        textstr =  r'$\vert y_i -f(\mathbf{x}_i) \vert = 2 $' 
        props = dict(boxstyle='round', facecolor='w', alpha=1)
        axs['a)'].text(0.3,0.9, textstr,  horizontalalignment='center', verticalalignment='center', transform=axs['a)'].transAxes, bbox=props)
        axs['a)'].arrow(-1,3, 0.45, -0.8, head_width=0.1, head_length=0.1, color='black')
        for i in range(7):
            Theta_min = random.choice(Theta)
            H_min = random.choice(Hight)
            xx = np.arange(-7,7,0.2)
            yy = xx*np.tan(Theta_min) + H_min
            axs['b)'].plot(xx, yy, 'k-')
            axs['b)'].set_xlim([-3,3])
            axs['b)'].set_ylim([-4,4])
            axs['b)'].set_ylabel("Detector 2 "+ r'$(x_2)$')
            axs['b)'].set_xlabel("Detector 1 "+ r'$(x_1)$')
            axs['b)'].set_title("Classifier "+ r'$\phi \in \mathbf{\Phi}$' + "(all Possible Lines) ")
            axs['b)'].legend(["Example of Possible Classifaication lines"],loc = 'lower right')
        plt.savefig("XG_lines.pdf")
        plt.show()
    
    
    
    def RunXG(Y):   
        alpha = 0.1
        f= np.array([0]*200 )
        F = np.ones_like(X1)*0
        Y = Y*2-1
        Theta_List, H_list, F_list,G = [],[],[],[]
        
        for i in range(41):
            F_list.append(F)
            global fig
            fig, axs = plt.subplot_mosaic([['a)']], constrained_layout=True, figsize=(5,4))
            PlotF(F_list[i], axs['a)'],i+1,True,True)
            plt.show()
            print(i)
            g = np.array(2*(f-Y))
            h = np.array([2]*200 )
            mini = np.array([[np.sum(1/2*h*(-g/h-f_pred(T,H,X))**2) for T in Theta] for H in Hight])
            Theta_min = Theta[np.unravel_index(mini.argmin(), mini.shape)[1]]
            H_min = Hight[np.unravel_index(mini.argmin(), mini.shape)[0]]
            Theta_List.append(Theta_min)
            H_list.append(H_min)
            f = f+alpha*f_pred(Theta_min,H_min,X)
            F = F + alpha* np.array([[f_pred1(Theta_min,H_min,[x,y]) for x in x1] for y in y1 ])
        with open("XGExplain2.pkl", 'wb') as f:
             pickle.dump(F_list, f)
    
    
    
    
         
    def Plot2():
        fig, axs = plt.subplot_mosaic([['a)', 'b)', 'c)']], constrained_layout=True, figsize=(10,4))
        PlotF(F_list[1], axs['a)'],1,False, True)
        PlotF(F_list[5], axs['b)'],5,False, True)
        PlotF(F_list[40], axs['c)'],40,True, True)
        plt.savefig("XG_Iter.pdf")
        plt.show()
    
    def Plot3():
        fig, axs = plt.subplot_mosaic([['b)','a)']], constrained_layout=True, figsize=(9,3))
        xx = np.arange(-3,3,0.2)
        yy = xx**2
        yb = (-0.5+xx)*2
        axs['a)'].plot(xx, yy, 'k-')
        axs['a)'].scatter([1], [1], color = 'r', marker='o')
        axs['a)'].plot(xx, yb, 'r-')
        axs['a)'].set_xlim([-2,2])
        axs['a)'].set_ylim([0,4])
        axs['a)'].set_ylabel(r'$L(\Delta y_i)$')
        axs['a)'].set_xlabel(r'$\Delta y_i$')
        textstr = '\n'.join((
            r'$ h_m(\mathbf{x}_i)= \frac{\partial^2L(\Delta y_i)}{\partial \Delta y_i ^2} = 2$',
            r'$ g_0(\mathbf{x}_i)= 2$'))
        
        props = dict(boxstyle='round', facecolor='wheat', alpha=0.9)
        axs['a)'].text(0.5,0.7, textstr,  horizontalalignment='center', verticalalignment='center', transform=axs['a)'].transAxes, bbox=props)
        axs['a)'].arrow(0.5,2.25, +0.5-0.1, -1.25+0.3, head_width=0.1, head_length=0.1, color='black')
      
        F = np.ones_like(X1)*0
        cbar = PlotF(F,axs['b)'],0,True,True)
        axs['b)'].legend(loc = 'lower right')
        cbar.set_label(r'$\hat f_{(0)}(\mathbf{x})$', rotation=270, labelpad=15)
        plt.savefig("XG_f0.pdf")
        plt.show()
    
    
    #RunXG(Y)
    with open("XGExplain2.pkl", 'rb') as file:
        F_list =  pickle.load(file)
    Plot1()
    Plot2()
    Plot3()