Python numpy.isin() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def _load_data(self, data_id):
        imgpath = osp.join(
            self.data_dir, 'JPEGImages/{}.jpg'.format(data_id))
        seg_imgpath = osp.join(
            self.data_dir, 'SegmentationClass/{}.png'.format(data_id))
        ins_imgpath = osp.join(
            self.data_dir, 'SegmentationObject/{}.png'.format(data_id))
        img = cv2.imread(imgpath)
        img = img.transpose((2, 0, 1))
        seg_img = PIL.Image.open(seg_imgpath)
        seg_img = np.array(seg_img, dtype=np.int32)
        seg_img[seg_img == 255] = -1
        ins_img = PIL.Image.open(ins_imgpath)
        ins_img = np.array(ins_img, dtype=np.int32)
        ins_img[ins_img == 255] = -1
        ins_img[np.isin(seg_img, [-1, 0])] = -1
        return img, seg_img, ins_img 

Example 2

def _load_data(self, data_id):
        imgpath = osp.join(
            self.data_dir, 'img/{}.jpg'.format(data_id))
        seg_imgpath = osp.join(
            self.data_dir, 'cls/{}.mat'.format(data_id))
        ins_imgpath = osp.join(
            self.data_dir, 'inst/{}.mat'.format(data_id))
        img = cv2.imread(imgpath, cv2.IMREAD_COLOR)
        img = img.transpose((2, 0, 1))
        mat = scipy.io.loadmat(seg_imgpath)
        seg_img = mat['GTcls'][0]['Segmentation'][0].astype(np.int32)
        seg_img = np.array(seg_img, dtype=np.int32)
        seg_img[seg_img == 255] = -1
        mat = scipy.io.loadmat(ins_imgpath)
        ins_img = mat['GTinst'][0]['Segmentation'][0].astype(np.int32)
        ins_img[ins_img == 255] = -1
        ins_img[np.isin(seg_img, [-1, 0])] = -1
        return img, seg_img, ins_img 

Example 3

def crossover(self, parent, pop):
        if np.random.rand() < self.cross_rate:
            i_ = np.random.randint(0, self.pop_size, size=1)                        # select another individual from pop
            cross_points = np.random.randint(0, 2, self.DNA_size).astype(np.bool)   # choose crossover points
            keep_city = parent[~cross_points]                                       # find the city number
            swap_city = pop[i_, np.isin(pop[i_].ravel(), keep_city, invert=True)]
            parent[:] = np.concatenate((keep_city, swap_city))
        return parent 

Example 4

def create_ignore_mask(self, postags, ignore_punct=True):
        if ignore_punct:
            mask = np.isin(postags, self._PUNCTS).astype(np.int32)
        else:
            mask = np.zeros(len(postags), np.int32)
        mask[0] = 1
        return mask 

Example 5

def binary_volume_opening(vol, minvol):
    lb_vol, num_objs = label(vol)
    lbs = np.arange(1, num_objs + 1)
    v = labeled_comprehension(lb_vol > 0, lb_vol, lbs, np.sum, np.int, 0)
    ix = np.isin(lb_vol, lbs[v >= minvol])
    newvol = np.zeros(vol.shape)
    newvol[ix] = vol[ix]
    return newvol 

Example 6

def _print_df_scores(df_scores, score_types, indent=''):
    """Pretty print the scores dataframe.

    Parameters
    ----------
    df_scores : pd.DataFrame
        the score dataframe
    score_types : list of score types
        a list of score types to use
    indent : str, default=''
        indentation if needed
    """
    try:
        # try to re-order columns/rows in the printed array
        # we may not have all train, valid, test, so need to select
        index_order = np.array(['train', 'valid', 'test'])
        ordered_index = index_order[np.isin(index_order, df_scores.index)]
        df_scores = df_scores.loc[
            ordered_index, [score_type.name for score_type in score_types]]
    except Exception:
        _print_warning("Couldn't re-order the score matrix..")
    with pd.option_context("display.width", 160):
        df_repr = repr(df_scores)
    df_repr_out = []
    for line, color_key in zip(df_repr.splitlines(),
                               [None, None] +
                               list(df_scores.index.values)):
        if line.strip() == 'step':
            continue
        if color_key is None:
            # table header
            line = stylize(line, fg(fg_colors['title']) + attr('bold'))
        if color_key is not None:
            tokens = line.split()
            tokens_bak = tokens[:]
            if 'official_' + color_key in fg_colors:
                # line label and official score bold & bright
                label_color = fg(fg_colors['official_' + color_key])
                tokens[0] = stylize(tokens[0], label_color + attr('bold'))
                tokens[1] = stylize(tokens[1], label_color + attr('bold'))
            if color_key in fg_colors:
                # other scores pale
                tokens[2:] = [stylize(token, fg(fg_colors[color_key]))
                              for token in tokens[2:]]
            for token_from, token_to in zip(tokens_bak, tokens):
                line = line.replace(token_from, token_to)
        line = indent + line
        df_repr_out.append(line)
    print('\n'.join(df_repr_out)) 
点赞