コード例 #1
0
    def plot_map(self, nmap=0, colbar='yes', min_m=-100, max_m=-100):
        """
        Plot a map created during the training process,

        :param int nmap: Number of created map, default is 0
        :param float min_m: Lower limit for coloring the cells, -100 uses min value
        :param float max_m: Upper limit for coloring the cells, -100 uses max value
        :param str colbar: Include a colorbar ('yes','no')
        """

        path_map = self.Pars.path_output_maps
        fileM = self.Pars.somfilename + '_%04d.npy' % nmap
        utils_mlz.printpz('Plotting ', fileM)
        M = np.load(path_map + fileM)
        M = M.item()
        M.plot_map(colbar=colbar, min_m=min_m, max_m=max_m)
コード例 #2
0
    def plot_tree(self, ntree=0, save_files='no', fileroot='TPZ', path=''):
        """
        Plot a tree created during the training process,
        uses the Graphviz package (dot and neato)

        :param int ntree: Number of created tree, default is 0
        :param str save_files: Saves the created files from Graphviz (the .png and the .dot files) 'yes'/'no'
        :param str fileroot: root name for saved files
        :param str path: path name for output files
        """
        path_tree = self.Pars.path_output_trees
        fileT = self.Pars.treefilename + '_%04d.npy' % ntree
        utils_mlz.printpz('Plotting ', fileT)
        T = np.load(path_tree + fileT)
        T = T.item()
        if T.dict_dim == 'all':
            dd = {}
            for i in range(len(self.Pars.att)):
                dd[self.Pars.att[i]] = {'ind': i}
        T.dict_dim = dd
        T.plot_tree(save_png=save_files, fileout=fileroot, path=path)
コード例 #3
0
    def plot_importance(self, result_id=0, Nzb=10, list_att=''):
        """
        Plot ranking of importance of attributes used during the training/testing process

        .. note ::

            The key `OobError` and `VarImportance` in :ref:`input-file` must be set to 'yes' to compute these quantities

        :param int results_id: Result id number as the output on the results folder, default 0
        :param int Nzb: Number of redshift bins
        """
        filenum = str(result_id)
        froot = self.Pars.path_results + self.Pars.finalfilename + '_oob'
        zs, za, zb, oa, ob, ea, eb = np.loadtxt(froot + '.' + filenum + '.mlz',
                                                unpack=True)
        fdz = lambda x, y: (abs(x - y)) / (1. + y)
        O_all = utils_mlz.bias(zs,
                               zb,
                               'OOB_all',
                               self.Pars.minz,
                               self.Pars.maxz,
                               1,
                               mode=0,
                               d_z=fdz,
                               verb=False)
        ki = np.arange(len(self.Pars.att))
        Im0 = np.zeros(len(ki))
        Im1 = np.zeros(len(ki))
        keys = []
        utils_mlz.printpz("Importance")
        utils_mlz.printpz("----------")
        for k in range(len(self.Pars.att)):
            kk = self.Pars.att[k]
            keys.append(kk)
            filev = froot + '_' + kk + '.' + filenum + '.mlz'
            zs, za, zb, oa, ob, ea, eb = np.loadtxt(filev, unpack=True)
            O_temp = utils_mlz.bias(zs,
                                    zb,
                                    'OOB_temp',
                                    self.Pars.minz,
                                    self.Pars.maxz,
                                    1,
                                    mode=0,
                                    d_z=fdz,
                                    verb=False)
            Im0[k] = O_temp.mean / O_all.mean
            Im1[k] = O_temp.sigma / O_all.sigma
            del O_temp
            imp = '%.5f' % Im1[k]
            utils_mlz.printpz(kk, ' ', imp)

        fimportance = froot + '.' + filenum + '.importance'
        FF = open(fimportance, 'w')
        linew = ','.join(keys)
        linew = '#' + linew + '\n'
        FF.write(linew)
        for i in range(len(ki)):
            linew = '%.8f\n' % Im0[i]
            FF.write(linew)
        FF.close()

        utils_mlz.printpz()
        utils_mlz.printpz("Importance file saved in: ", fimportance)

        Im = (Im0 + Im1) / 2.
        sk = np.argsort(Im0)
        sk = sk[::-1]
        fig, ax = plt.subplots()
        ax.plot(ki, Im0[sk], 'bo-', label='bias')
        ax.plot(ki, Im1[sk], 'go-', label='sigma')
        ax.plot(ki, Im[sk], 'ro-', label='avg')
        ax.set_xlabel('Importance', fontsize=16)
        ax.set_ylabel('Attributes', fontsize=16)
        plt.legend(loc=0)
        ax.set_xticks(ki)
        ax.set_xticklabels(np.array(keys)[sk],
                           rotation=40,
                           ha='right',
                           fontsize=12)
        ax.set_xlim(-1, len(ki))

        ##
        zs, za, zb, oa, ob, ea, eb = np.loadtxt(froot + '.' + filenum + '.mlz',
                                                unpack=True)
        O_all = utils_mlz.bias(zs,
                               zb,
                               'OOB_all',
                               self.Pars.minz,
                               self.Pars.maxz,
                               Nzb,
                               mode=1,
                               d_z=fdz,
                               verb=False)
        ki = np.arange(len(self.Pars.att))
        Im0 = np.zeros((Nzb, len(ki)))
        Im1 = np.zeros((Nzb, len(ki)))
        keys = []
        for k in range(len(self.Pars.att)):
            kk = self.Pars.att[k]
            keys.append(kk)
            filev = froot + '_' + kk + '.' + filenum + '.mlz'
            zs, za, zb, oa, ob, ea, eb = np.loadtxt(filev, unpack=True)
            O_temp = utils_mlz.bias(zs,
                                    zb,
                                    'OOB_temp',
                                    self.Pars.minz,
                                    self.Pars.maxz,
                                    Nzb,
                                    mode=1,
                                    d_z=fdz,
                                    verb=False)
            Im0[:, k] = O_temp.mean / O_all.mean
            Im1[:, k] = O_temp.sigma / O_all.sigma
            del O_temp

        fig2, ax2 = plt.subplots()
        if list_att == '':
            ax2.plot(O_all.bins, (Im0[:, sk[0]] + Im1[:, sk[0]]) / 2,
                     'bo-',
                     label=np.array(keys)[sk[0]])
            ax2.plot(O_all.bins, (Im0[:, sk[1]] + Im1[:, sk[1]]) / 2,
                     'go-',
                     label=np.array(keys)[sk[1]])
            ax2.plot(O_all.bins, (Im0[:, sk[-1]] + Im1[:, sk[-1]]) / 2,
                     'ro-',
                     label=np.array(keys)[sk[-1]])
        else:
            for katt in list_att:
                wk = np.where(np.array(keys) == katt)[0]
                ax2.plot(O_all.bins, (Im0[:, wk[0]] + Im1[:, wk[0]]) / 2,
                         'o-',
                         label=np.array(keys)[wk[0]])
        ax2.set_xlabel('redshift', fontsize=16)
        ax2.set_ylabel('Importance', fontsize=16)
        plt.legend(loc=0)
        plt.show()