def plot2d(self,
               plotfreq=1,
               show_plot=False,
               fname='plots/2dtestplot.pdf'):

        xs = np.arange(self.domain[0][0], self.domain[0][1], self.res)
        ys = np.arange(self.domain[1][0], self.domain[1][1], self.res)
        xs, ys = np.meshgrid(xs, ys)

        def pred(i, j):
            #if (i*len(xs[0])+j)%100==0:
            #print('making ' +str(i*len(xs[0])+j) + 'th prediction')
            return self.pred_y([xs[i][j], ys[i][j]])

        #print('about to make Z')

        Z = [[pred(i, j) for j in range(len(xs[i]))] for i in range(len(xs))]

        #Z = [ [ self.objective_func([xs[i][j],ys[i][j]]) for j in range(len(xs[i]) )] for i in range(len(xs))]
        """
        xs = self.domain_buffer.transpose()[0]
        ys = self.domain_buffer.transpose()[1]
        def prediction(i):
            if i%100==0:
                print('making ' +str(i) + 'th prediction')
                print('with x = ('+str(xs[i])+', '+str(ys[i])+')')
            return self.pred_y([xs[i],ys[i]])
        
        Z = [ prediction(i) for i in range(len(xs)) ]
        """
        print('got the Z')
        plt.figure()
        CS = plt.contour(xs, ys, Z, 20)

        points = plt.plot([x[0] for x in self.X], [x[1] for x in self.X],
                          'ko',
                          label="sample points")

        print('contoured the thing')
        plt.title('branin model test n = ' + str(self.n))
        if fname: plt.savefig(fname, bbox_inches='tight')

        if show_plot: plt.show()
Example #2
0
    def plot2d(self, plotfreq=1, show_plot=False, fname='plots/2dtestplot.pdf'):
                
        xs = np.arange(self.domain[0][0], self.domain[0][1], self.res)
        ys = np.arange(self.domain[1][0], self.domain[1][1], self.res)
        xs, ys = np.meshgrid(xs, ys)
        def pred(i,j):
            #if (i*len(xs[0])+j)%100==0:
                #print('making ' +str(i*len(xs[0])+j) + 'th prediction')
            return self.pred_y([xs[i][j],ys[i][j]])
            
            
        #print('about to make Z')
            
        Z = [[pred(i,j) for j in range(len(xs[i]))] for i in range(len(xs))]
                
        #Z = [ [ self.objective_func([xs[i][j],ys[i][j]]) for j in range(len(xs[i]) )] for i in range(len(xs))]
        """
        xs = self.domain_buffer.transpose()[0]
        ys = self.domain_buffer.transpose()[1]
        def prediction(i):
            if i%100==0:
                print('making ' +str(i) + 'th prediction')
                print('with x = ('+str(xs[i])+', '+str(ys[i])+')')
            return self.pred_y([xs[i],ys[i]])
        
        Z = [ prediction(i) for i in range(len(xs)) ]
        """
        print('got the Z')
        plt.figure()
        CS = plt.contour(xs, ys, Z,20)
        
        points = plt.plot([x[0] for x in self.X],[x[1] for x in self.X], 'ko',label="sample points")
        
        
        print('contoured the thing')
        plt.title('branin model test n = ' + str(self.n))
        if fname: plt.savefig(fname, bbox_inches='tight')

        if show_plot: plt.show()
    def plot1d(self,
               dim=0,
               plot_objective=False,
               plot_err=True,
               plot_improvement=True,
               plot_next_sp=True,
               show_plot=False,
               fname=None,
               legend=False):
        """
        Args:
            plot_objective (bool): whether the objective function should be plotted. This is only feasible if that function can be called enough times to generate a smooth plot
            plot_improvement (bool): option to overlay expected emprovement at each x
            plot_next_sp (bool): whether the selection of the next sample point is plotted
            show_plot (bool): whether pyplot.show is called at the end of the function
            dim (int): the index of the dimension of interest
            x_delta (float): the resolution of the plot
            fname: if set, the plot is saved to this
        Uses pyplot to make a nice 1d plot of the predictor function and its error. Optionally, overlay a plot of the
        """
        x_min = self.domain[dim][0]
        x_max = self.domain[dim][1]

        #plt.rc('text', usetex=True)
        #plt.rc('font', family='serif')

        font = {'fontname': 'Palatino'}

        fig, ax = plt.subplots()
        plt.subplots_adjust(left=0.25, bottom=0.25)
        plt.xlabel('x')
        ax.set_ylabel('predicted y(x)')
        plt.title('Predictor Surface with ' + str(self.n) + ' samples')
        pred_range = np.arange(x_min, x_max, self.res)

        if plot_objective:
            obj = [self.objective_func([x]) for x in pred_range]
            exp_imp_line, = ax.plot(pred_range,
                                    obj,
                                    'k--',
                                    linewidth=2,
                                    label='objective func')

        preds = self.prediction_buffer
        # errors are illustrated with 2 times sigma (two standard devs)

        # plot the predictor and +/- errors
        pred_line, = ax.plot(pred_range, preds, label='predictor surface')

        if plot_err:
            # to illustrate a 95% confidence interval
            errors = self.error_buffer
            # elem-wise sum/difference of above two arrays
            pl_errors = map(add, preds, errors)
            mi_errors = map(sub, preds, errors)
            ax.fill_between(pred_range,
                            pl_errors,
                            mi_errors,
                            facecolor="lightgreen")
            p_err_line, = ax.plot(pred_range,
                                  pl_errors,
                                  color="green",
                                  label='plus/minus error')
            m_err_line, = ax.plot(pred_range, mi_errors, color="green")
            y_min = np.amin(preds) - np.amax(errors)
            y_max = np.amax(preds) + np.amax(errors)
            plt.axis([x_min, x_max, -4, 19])

        # just for making some plots right now
        #plt.axis([self.domain[0][0], self.domain[0][1], -3, 12])

        # legend handler, labels
        leg_hand, leg_lab = ax.get_legend_handles_labels()

        # make another axis (exp improv. is at a smaller scale than predictor)
        # plot the expected improvement
        if plot_improvement:
            ax2 = ax.twinx()
            imps = [self.exp_improvement([x]) for x in pred_range]
            # if you're plotting, might as well use that info for maximization
            exp_imp_line, = ax2.plot(pred_range,
                                     imps,
                                     color='r',
                                     label='expected improvement')
            ax2.set_ylabel('expected improvement', color='r')
            for tl in ax2.get_yticklabels():
                tl.set_color('r')
            ax2.axis([x_min, x_max, 0, 1.])

            # combining labels from both axes
            lh2, ll2 = ax2.get_legend_handles_labels()
            leg_hand += lh2
            leg_lab += ll2

        # plot a vertical dotted line at the next sample point
        if plot_next_sp:
            ax3 = ax.twinx()
            point = ax.axvline(self.next_sample,
                               color='k',
                               linestyle='dotted',
                               label='next sample')
            lh3, ll3 = ax3.get_legend_handles_labels()
            leg_hand += lh3
            leg_lab += ll3

        # plot the actual sample points
        points = ax.plot([x[dim] for x in self.X],
                         self.Y,
                         'ko',
                         label='sample points')

        if legend:
            plt.legend(leg_hand, leg_lab, loc=9, numpoints=1, fontsize=10)

        if fname:
            # bbox is tight bc I'll add margins in latex if I want to, thankyouverymuch
            plt.savefig(fname, bbox_inches='tight')

        plt.tight_layout()
        if show_plot: plt.show()
        plt.close()
Example #4
0
 def plot1d(self, dim=0, plot_objective=False, plot_err=True, plot_improvement=True, plot_next_sp=True, show_plot=False, fname=None, legend=False):
     """
     Args:
         plot_objective (bool): whether the objective function should be plotted. This is only feasible if that function can be called enough times to generate a smooth plot
         plot_improvement (bool): option to overlay expected emprovement at each x
         plot_next_sp (bool): whether the selection of the next sample point is plotted
         show_plot (bool): whether pyplot.show is called at the end of the function
         dim (int): the index of the dimension of interest
         x_delta (float): the resolution of the plot
         fname: if set, the plot is saved to this
     Uses pyplot to make a nice 1d plot of the predictor function and its error. Optionally, overlay a plot of the
     """
     x_min=self.domain[dim][0]
     x_max=self.domain[dim][1]
     
     #plt.rc('text', usetex=True)
     #plt.rc('font', family='serif')
     
     
     
     font = {'fontname':'Palatino'}
     
     fig, ax = plt.subplots()
     #plt.subplots_adjust(left=0.25, bottom=0.25)
     plt.xlabel('x')
     ax.set_ylabel('predicted y(x)')
     plt.title('Predictor Surface with '+str(self.n)+' samples')
     pred_range = np.arange(x_min, x_max, self.res)
     
     if plot_objective:
         obj = [ self.objective_func([x]) for x in pred_range ]
         exp_imp_line, = ax.plot( pred_range, obj, 'k--', linewidth=2, label= 'objective func' )
     
     # plot the actual sample points
     points = ax.plot([x[dim] for x in self.X],self.Y, 'ko',label='sample points')
     
     
     preds  = self.prediction_buffer
     # errors are illustrated with 2 times sigma (two standard devs)
     
     
     # plot the predictor and +/- errors
     pred_line,  = ax.plot(pred_range, preds, label='predictor surface')
     
     
     
     
     if plot_err:
         # to illustrate a 95% confidence interval
         errors = self.error_buffer
         # elem-wise sum/difference of above two arrays
         pl_errors = map(add, preds, errors)
         mi_errors = map(sub, preds, errors)
         ax.fill_between(pred_range, pl_errors, mi_errors, facecolor="lightgreen")
         p_err_line, = ax.plot(pred_range, pl_errors, color="green", label= 'plus/minus error')
         m_err_line, = ax.plot(pred_range, mi_errors, color="green")
         y_min=np.amin(preds)-np.amax(errors)
         y_max=np.amax(preds)+np.amax(errors)
         plt.axis([x_min, x_max, -4, 19])
         
     # just for making some plots right now
     #plt.axis([self.domain[0][0], self.domain[0][1], -3, 12])
     
     
             
     
     
     
     
     
     # legend handler, labels
     leg_hand, leg_lab = ax.get_legend_handles_labels()
     
     # make another axis (exp improv. is at a smaller scale than predictor)
     # plot the expected improvement
     if plot_improvement:
         ax2 = ax.twinx()
         ax2.axis([x_min, x_max, 0, 1])
         imps = [ self.exp_improvement([x]) for x in pred_range ]
         # if you're plotting, might as well use that info for maximization
         exp_imp_line, = ax2.plot(pred_range, imps, color='r',label= 'expected improvement')
         ax2.set_ylabel( 'expected improvement', color='r')
         for tl in ax2.get_yticklabels():
             tl.set_color('r')
         
         if plot_next_sp:
             # plot a vertical dotted line at the next sample point
             point = ax2.axvline(self.next_sample, color='k', linestyle='dotted', label= 'next sample')
         
         
         # combining labels from both axes
         lh2,ll2 = ax2.get_legend_handles_labels()
         leg_hand += lh2
         leg_lab  += ll2
         
               
         ax2.tick_params(axis="y", labelcolor="r")   
         
         
     
             
     
     
     
 
     if legend:   
         plt.legend(leg_hand,leg_lab,numpoints=1,fontsize=10)
     
      
     if fname:
         # bbox is tight bc I'll add margins in latex if I want to, thankyouverymuch
         plt.savefig(fname, bbox_inches='tight')
         
     plt.tight_layout()
     if show_plot: plt.show()
     plt.close()