我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.equal()。
def go_nn_kdtree(eps=0, parallel=True): """ Using a specialized data structure, the KDTree This is not as performant because we're in a high dimensional space 0.777 accuracy? Should be 0.794 """ n_jobs = 1 if parallel: n_jobs = -1 neighbors = tree.query(Xtest, eps=eps, n_jobs=n_jobs) predictions = ytrain[neighbors[1]] acc = np.equal(predictions, ytest).mean() return acc
def updateSpots(self, dataSet=None): if dataSet is None: dataSet = self.data invalidate = False if self.opts['pxMode']: mask = np.equal(dataSet['sourceRect'], None) if np.any(mask): invalidate = True opts = self.getSpotOpts(dataSet[mask]) sourceRect = self.fragmentAtlas.getSymbolCoords(opts) dataSet['sourceRect'][mask] = sourceRect self.fragmentAtlas.getAtlas() # generate atlas so source widths are available. dataSet['width'] = np.array(list(imap(QtCore.QRectF.width, dataSet['sourceRect'])))/2 dataSet['targetRect'] = None self._maxSpotPxWidth = self.fragmentAtlas.max_width else: self._maxSpotWidth = 0 self._maxSpotPxWidth = 0 self.measureSpotSizes(dataSet) if invalidate: self.invalidate()
def getSpotOpts(self, recs, scale=1.0): if recs.ndim == 0: rec = recs symbol = rec['symbol'] if symbol is None: symbol = self.opts['symbol'] size = rec['size'] if size < 0: size = self.opts['size'] pen = rec['pen'] if pen is None: pen = self.opts['pen'] brush = rec['brush'] if brush is None: brush = self.opts['brush'] return (symbol, size*scale, fn.mkPen(pen), fn.mkBrush(brush)) else: recs = recs.copy() recs['symbol'][np.equal(recs['symbol'], None)] = self.opts['symbol'] recs['size'][np.equal(recs['size'], -1)] = self.opts['size'] recs['size'] *= scale recs['pen'][np.equal(recs['pen'], None)] = fn.mkPen(self.opts['pen']) recs['brush'][np.equal(recs['brush'], None)] = fn.mkBrush(self.opts['brush']) return recs
def calc_metrics(self, data_gen, history, dataset, logs): y_true = [] predictions = [] for i in range(data_gen.steps): if self.verbose == 1: print "\r\tdone {}/{}".format(i, data_gen.steps), (x,y) = next(data_gen) pred = self.model.predict(x, batch_size=self.batch_size) if isinstance(x, list) and len(x) == 2: # deep supervision for m, t, p in zip(x[1].flatten(), y.flatten(), pred.flatten()): if np.equal(m, 1): y_true.append(t) predictions.append(p) else: y_true += list(y.flatten()) predictions += list(pred.flatten()) print "\n" predictions = np.array(predictions) predictions = np.stack([1-predictions, predictions], axis=1) ret = metrics.print_metrics_binary(y_true, predictions) for k, v in ret.iteritems(): logs[dataset + '_' + k] = v history.append(ret)
def add(self, output, target): if torch.is_tensor(output): output = output.cpu().squeeze().numpy() if torch.is_tensor(target): target = target.cpu().squeeze().numpy() elif isinstance(target, numbers.Number): target = np.asarray([target]) assert np.ndim(output) == 1, \ 'wrong output size (1D expected)' assert np.ndim(target) == 1, \ 'wrong target size (1D expected)' assert output.shape[0] == target.shape[0], \ 'number of outputs and targets does not match' assert np.all(np.add(np.equal(target, 1), np.equal(target, 0))), \ 'targets should be binary (0, 1)' self.scores = np.append(self.scores, output) self.targets = np.append(self.targets, target)
def validate_transitions_cpu_old(transitions, **kwargs): pre = np.array(transitions[0]) suc = np.array(transitions[1]) base = setting['base'] width = pre.shape[1] // base height = pre.shape[1] // base load(width,height) pre_validation = validate_states(pre, **kwargs) suc_validation = validate_states(suc, **kwargs) results = [] for pre, suc, pre_validation, suc_validation in zip(pre, suc, pre_validation, suc_validation): if pre_validation and suc_validation: c = to_configs(np.array([pre, suc]), verbose=False) succs = successors(c[0], width, height) results.append(np.any(np.all(np.equal(succs, c[1]), axis=1))) else: results.append(False) return results
def setup(): setting['base'] = 14 def loader(width,height): from ..util.mnist import mnist base = setting['base'] x_train, y_train, _, _ = mnist() filters = [ np.equal(i,y_train) for i in range(9) ] imgs = [ x_train[f] for f in filters ] panels = [ imgs[0].reshape((28,28)) for imgs in imgs ] panels[8] = imgs[8][3].reshape((28,28)) panels[1] = imgs[8][3].reshape((28,28)) panels = np.array(panels) stepy = panels.shape[1]//base stepx = panels.shape[2]//base # unfortunately the method below generates "bolder" fonts # panels = panels[:,:stepy*base,:stepx*base,] # panels = panels.reshape((panels.shape[0],base,stepy,base,stepx)) # panels = panels.mean(axis=(2,4)) # panels = panels.round() panels = panels[:,::stepy,::stepx][:,:base,:base].round() panels = preprocess(panels) return panels setting['loader'] = loader
def validate_transitions(transitions, check_states=True, **kwargs): pre = np.array(transitions[0]) suc = np.array(transitions[1]) if check_states: pre_validation = validate_states(pre, verbose=False, **kwargs) suc_validation = validate_states(suc, verbose=False, **kwargs) pre_configs = to_configs(pre, verbose=False, **kwargs) suc_configs = to_configs(suc, verbose=False, **kwargs) results = [] if check_states: for pre_c, suc_c, pre_validation, suc_validation in zip(pre_configs, suc_configs, pre_validation, suc_validation): if pre_validation and suc_validation: succs = successors(pre_c) results.append(np.any(np.all(np.equal(succs, suc_c), axis=1))) else: results.append(False) else: for pre_c, suc_c in zip(pre_configs, suc_configs): succs = successors(pre_c) results.append(np.any(np.all(np.equal(succs, suc_c), axis=1))) return results
def equal(x1, x2): """ Return (x1 == x2) element-wise. Unlike `numpy.equal`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray or bool Output array of bools, or a single bool if x1 and x2 are scalars. See Also -------- not_equal, greater_equal, less_equal, greater, less """ return compare_chararrays(x1, x2, '==', True)
def not_equal(x1, x2): """ Return (x1 != x2) element-wise. Unlike `numpy.not_equal`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray or bool Output array of bools, or a single bool if x1 and x2 are scalars. See Also -------- equal, greater_equal, less_equal, greater, less """ return compare_chararrays(x1, x2, '!=', True)
def greater_equal(x1, x2): """ Return (x1 >= x2) element-wise. Unlike `numpy.greater_equal`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray or bool Output array of bools, or a single bool if x1 and x2 are scalars. See Also -------- equal, not_equal, less_equal, greater, less """ return compare_chararrays(x1, x2, '>=', True)
def less_equal(x1, x2): """ Return (x1 <= x2) element-wise. Unlike `numpy.less_equal`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray or bool Output array of bools, or a single bool if x1 and x2 are scalars. See Also -------- equal, not_equal, greater_equal, greater, less """ return compare_chararrays(x1, x2, '<=', True)
def greater(x1, x2): """ Return (x1 > x2) element-wise. Unlike `numpy.greater`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray or bool Output array of bools, or a single bool if x1 and x2 are scalars. See Also -------- equal, not_equal, greater_equal, less_equal, less """ return compare_chararrays(x1, x2, '>', True)
def test_scalar_none_comparison(self): # Scalars should still just return False and not give a warnings. # The comparisons are flagged by pep8, ignore that. with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', FutureWarning) assert_(not np.float32(1) == None) assert_(not np.str_('test') == None) # This is dubious (see below): assert_(not np.datetime64('NaT') == None) assert_(np.float32(1) != None) assert_(np.str_('test') != None) # This is dubious (see below): assert_(np.datetime64('NaT') != None) assert_(len(w) == 0) # For documentation purposes, this is why the datetime is dubious. # At the time of deprecation this was no behaviour change, but # it has to be considered when the deprecations are done. assert_(np.equal(np.datetime64('NaT'), None))
def almost(a, b, decimal=6, fill_value=True): """ Returns True if a and b are equal up to decimal places. If fill_value is True, masked values considered equal. Otherwise, masked values are considered unequal. """ m = mask_or(getmask(a), getmask(b)) d1 = filled(a) d2 = filled(b) if d1.dtype.char == "O" or d2.dtype.char == "O": return np.equal(d1, d2).ravel() x = filled(masked_array(d1, copy=False, mask=m), fill_value).astype(float_) y = filled(masked_array(d2, copy=False, mask=m), 1).astype(float_) d = np.around(np.abs(x - y), decimal) <= 10.0 ** (-decimal) return d.ravel()
def fail_if_equal(actual, desired, err_msg='',): """ Raises an assertion error if two items are equal. """ if isinstance(desired, dict): if not isinstance(actual, dict): raise AssertionError(repr(type(actual))) fail_if_equal(len(actual), len(desired), err_msg) for k, i in desired.items(): if k not in actual: raise AssertionError(repr(k)) fail_if_equal(actual[k], desired[k], 'key=%r\n%s' % (k, err_msg)) return if isinstance(desired, (list, tuple)) and isinstance(actual, (list, tuple)): fail_if_equal(len(actual), len(desired), err_msg) for k in range(len(desired)): fail_if_equal(actual[k], desired[k], 'item=%r\n%s' % (k, err_msg)) return if isinstance(actual, np.ndarray) or isinstance(desired, np.ndarray): return fail_if_array_equal(actual, desired, err_msg) msg = build_err_msg([actual, desired], err_msg) if not desired != actual: raise AssertionError(msg)
def categorical_accuracy(y_true, y_pred, mask=True): ''' categorical_accuracy adjusted for padding mask ''' # if mask is not None: print y_true print y_pred eval_shape = (reduce(mul, y_true.shape[:-1]), y_true.shape[-1]) print eval_shape y_true_ = np.reshape(y_true, eval_shape) y_pred_ = np.reshape(y_pred, eval_shape) flat_mask = np.flatten(mask) comped = np.equal(np.argmax(y_true_, axis=-1), np.argmax(y_pred_, axis=-1)) ## not sure how to do this in tensor flow good_entries = flat_mask.nonzero()[0] return np.mean(np.gather(comped, good_entries)) # else: # return K.mean(K.equal(K.argmax(y_true, axis=-1), # K.argmax(y_pred, axis=-1)))
def __estimate_entropy__(self): counts = self.feature_vector_counts #Counter(self.timeline_feature_vectors) #print counts #N = float(sum(counts.values())) N = float(len(self.timeline) + 1) max_H = np.log(float(len(list(filter(lambda x: x, counts))))) if np.equal(max_H, 0.0): return 0.0 entropy = 0.0 for key in counts.keys(): if counts[key] > 0: key_probability = counts[key] / N entropy += -(key_probability * np.log(key_probability)) entropy /= max_H #print u'N={0}, |counts|={3}, max_H={1}, entropy={2}, counter={4}'.format(N, max_H, entropy, len(counts), counts) return entropy
def _f_dice(a, b): """DICE between two segmentations. Args: a: [..., H, W], binary mask b: [..., H, W], binary mask Returns: dice: [...] """ card_a = a.sum(axis=-1).sum(axis=-1) card_b = b.sum(axis=-1).sum(axis=-1) card_ab = (a * b).sum(axis=-1).sum(axis=-1) card_sum = card_a + card_b dice = 2 * card_ab / (card_sum + np.equal(card_sum, 0).astype('float32')) return dice
def test_accuracy(): def cat_acc(y_pred, y_true): return np.expand_dims(np.equal(np.argmax(y_pred, axis=-1), np.argmax(y_true, axis=-1)), -1), objectives_test(objectives.accuracy, cat_acc, np_pred=[[0,0,.9], [0,.9,0], [.9,0,0]], np_true=[[0,0,1], [0,0,1], [0,0,1]]) def bi_acc(y_pred, y_true): return np.equal(np.round(y_pred), y_true) objectives_test(objectives.accuracy, bi_acc, np_pred=[[0], [0.6], [0.7]], np_true=[[0], [1], [1]])
def test_convolutional_embedding_encoder(config, out_data_shape, out_data_length, out_seq_len): conv_embed = sockeye.encoder.ConvolutionalEmbeddingEncoder(config) data_nd = mx.nd.random_normal(shape=(_BATCH_SIZE, _SEQ_LEN, _NUM_EMBED)) data = mx.sym.Variable("data", shape=data_nd.shape) data_length = mx.sym.Variable("data_length", shape=_DATA_LENGTH_ND.shape) (encoded_data, encoded_data_length, encoded_seq_len) = conv_embed.encode(data=data, data_length=data_length, seq_len=_SEQ_LEN) exe = encoded_data.simple_bind(mx.cpu(), data=data_nd.shape) exe.forward(data=data_nd) assert exe.outputs[0].shape == out_data_shape exe = encoded_data_length.simple_bind(mx.cpu(), data_length=_DATA_LENGTH_ND.shape) exe.forward(data_length=_DATA_LENGTH_ND) assert np.equal(exe.outputs[0].asnumpy(), np.asarray(out_data_length)).all() assert encoded_seq_len == out_seq_len
def grayscaleimage(self, value): try: if value.ndim == 2: self._grayscaleimage = value if (_np.equal(self._x,None).any() or _np.equal(self._y,None).any() or self._x.size != value.shape[1] or self._y.size != value.shape[0]): self._x = _np.linspace(1, value.shape[1], value.shape[1]) self._y = _np.linspace(1, value.shape[0], value.shape[0]) self.xunits = self.XUNITS self.yunits = self.YUNITS else: pass except: pass
def paren_data(T, n_data): MAX_COUNT = 10 n_paren = 10 n_noise = 10 inputs = (np.random.rand(T, n_data)* (n_paren * 2 + n_noise)).astype(np.int32) counts = np.zeros((n_data, n_paren), dtype=np.int32) targets = np.zeros((T, n_data, n_paren), dtype = np.int32) opening_parens = (np.arange(0, n_paren)*2)[None, :] closing_parens = opening_parens + 1 for i in range(T): opened = np.equal(inputs[i, :, None], opening_parens) counts = np.minimum(MAX_COUNT, counts + opened) closed = np.equal(inputs[i, :, None], closing_parens) counts = np.maximum(0, counts - closed) targets[i, :, :] = counts x = np.transpose(inputs, [1,0]) y = np.transpose(targets, [1,0,2]) return x, y
def is_connect_exist_nn(node_in, node_out, nn): """ check if the connection between node_in and node_out exists :param node_in: :param node_out: :param nn: Neural network instance :return: True if exists, False if DNE """ assert type(nn) == NeuralNetwork, "nn must be an instance of Neural Network" if nn.connect_genes is None: return False connect = [node_in, node_out] history = nn.connect_genes[:, :2] return any(np.equal(connect, history).all(1))
def test_scalar_none_comparison(self): # Scalars should still just return false and not give a warnings. # The comparisons are flagged by pep8, ignore that. with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', FutureWarning) assert_(not np.float32(1) == None) assert_(not np.str_('test') == None) # This is dubious (see below): assert_(not np.datetime64('NaT') == None) assert_(np.float32(1) != None) assert_(np.str_('test') != None) # This is dubious (see below): assert_(np.datetime64('NaT') != None) assert_(len(w) == 0) # For documentaiton purpose, this is why the datetime is dubious. # At the time of deprecation this was no behaviour change, but # it has to be considered when the deprecations is done. assert_(np.equal(np.datetime64('NaT'), None))
def approx(a, b, fill_value=True, rtol=1e-5, atol=1e-8): """ Returns true if all components of a and b are equal to given tolerances. If fill_value is True, masked values considered equal. Otherwise, masked values are considered unequal. The relative error rtol should be positive and << 1.0 The absolute error atol comes into play for those elements of b that are very small or zero; it says how small a must be also. """ m = mask_or(getmask(a), getmask(b)) d1 = filled(a) d2 = filled(b) if d1.dtype.char == "O" or d2.dtype.char == "O": return np.equal(d1, d2).ravel() x = filled(masked_array(d1, copy=False, mask=m), fill_value).astype(float_) y = filled(masked_array(d2, copy=False, mask=m), 1).astype(float_) d = np.less_equal(umath.absolute(x - y), atol + rtol * umath.absolute(y)) return d.ravel()
def get_same_status(pairs, items, target): text_compare = pairs item1 = items[['itemID', target]] item1 = item1.rename( columns={ 'itemID': 'itemID_1', target: target + '_1', } ) text_compare = pd.merge(text_compare, item1, how='left', on='itemID_1', left_index=True) item2 = items[['itemID', target]] item2 = item2.rename( columns={ 'itemID': 'itemID_2', target: target + '_2', } ) text_compare = pd.merge(text_compare, item2, how='left', on='itemID_2', left_index=True) text_compare[target + '_same'] = np.equal(text_compare[target + '_1'], text_compare[target + '_2']).astype(np.int32) # print(text_compare[target + '_same'].describe()) return text_compare[['id', target + '_same']]
def gen_hull(p, p_mask, f_encode, f_probi, options): # p: n_sizes * n_samples * data_dim n_sizes = p.shape[0] n_samples = p.shape[1] if p.ndim == 3 else 1 hprev = f_encode(p_mask, p) # n_sizes * n_samples * data_dim points = numpy.zeros((n_samples, n_sizes), dtype='int64') h = hprev[-1] c = numpy.zeros((n_samples, options['dim_proj']), dtype=config.floatX) xi = numpy.zeros((n_samples,), dtype='int64') xi_mask = numpy.ones((n_samples,), dtype=config.floatX) for i in range(n_sizes): h, c, probi = f_probi(p_mask[i], xi, h, c, hprev, p_mask, p) xi = probi.argmax(axis=0) xi *= xi_mask.astype(numpy.int64) # Avoid compatibility problem in numpy 1.10 xi_mask = (numpy.not_equal(xi, 0)).astype(config.floatX) if numpy.equal(xi_mask, 0).all(): break points[:, i] = xi return points
def VOCap(rec,prec): mpre = np.zeros([1,2+len(prec)]) mpre[0,1:len(prec)+1] = prec mrec = np.zeros([1,2+len(rec)]) mrec[0,1:len(rec)+1] = rec mrec[0,len(rec)+1] = 1.0 for i in range(mpre.size-2,-1,-1): mpre[0,i] = max(mpre[0,i],mpre[0,i+1]) i = np.argwhere( ~np.equal( mrec[0,1:], mrec[0,:mrec.shape[1]-1]) )+1 i = i.flatten() # compute area under the curve ap = np.sum( np.multiply( np.subtract( mrec[0,i], mrec[0,i-1]), mpre[0,i] ) ) return ap