我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用numpy.array_equiv()。
def test_array_equiv(self): res = np.array_equiv(np.array([1, 2]), np.array([1, 2])) assert_(res) assert_(type(res) is bool) res = np.array_equiv(np.array([1, 2]), np.array([1, 2, 3])) assert_(not res) assert_(type(res) is bool) res = np.array_equiv(np.array([1, 2]), np.array([3, 4])) assert_(not res) assert_(type(res) is bool) res = np.array_equiv(np.array([1, 2]), np.array([1, 3])) assert_(not res) assert_(type(res) is bool) res = np.array_equiv(np.array([1, 1]), np.array([1])) assert_(res) assert_(type(res) is bool) res = np.array_equiv(np.array([1, 1]), np.array([[1], [1]])) assert_(res) assert_(type(res) is bool) res = np.array_equiv(np.array([1, 2]), np.array([2])) assert_(not res) assert_(type(res) is bool) res = np.array_equiv(np.array([1, 2]), np.array([[1], [2]])) assert_(not res) assert_(type(res) is bool) res = np.array_equiv(np.array([1, 2]), np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) assert_(not res) assert_(type(res) is bool)
def test_tri_sym_convert(): from brainiak.utils.utils import from_tri_2_sym, from_sym_2_tri import numpy as np sym = np.random.rand(3, 3) tri = from_sym_2_tri(sym) assert tri.shape[0] == 6,\ "from_sym_2_tri returned wrong result!" sym1 = from_tri_2_sym(tri, 3) assert sym1.shape[0] == sym1.shape[1],\ "from_tri_2_sym returned wrong shape!" tri1 = from_sym_2_tri(sym1) assert np.array_equiv(tri, tri1),\ "from_sym_2_tri returned wrong result!"
def test_times_1(): cntk_op = C.times([1, 2, 3], [[4], [5], [6]]) cntk_ret = cntk_op.eval() ng_op, _ = CNTKImporter().import_model(cntk_op) ng_ret = ng.transformers.make_transformer().computation(ng_op)() assert np.array_equiv(cntk_ret, ng_ret)
def test_times_2(): cntk_op = C.times([[1, 2], [3, 4]], [[5, 6], [7, 8]]) cntk_ret = cntk_op.eval() ng_op, _ = CNTKImporter().import_model(cntk_op) ng_ret = ng.transformers.make_transformer().computation(ng_op)() assert np.array_equiv(cntk_ret, ng_ret)
def test_times_3(): cntk_op = C.times([1, 2, 3], [[4, 5], [6, 7], [8, 9]]) cntk_ret = cntk_op.eval() ng_op, _ = CNTKImporter().import_model(cntk_op) ng_ret = ng.transformers.make_transformer().computation(ng_op)() assert np.array_equiv(cntk_ret, ng_ret)
def test_times_4(): cntk_op = C.times([[1, 2, 3], [4, 5, 6]], [[7], [8], [9]]) cntk_ret = cntk_op.eval() ng_op, _ = CNTKImporter().import_model(cntk_op) ng_ret = ng.transformers.make_transformer().computation(ng_op)() assert np.array_equiv(cntk_ret, ng_ret)
def test_times_5(): cntk_op = C.times([[1, 2, 3], [4, 5, 6]], [[7, 8], [9, 10], [11, 12]]) cntk_ret = cntk_op.eval() ng_op, _ = CNTKImporter().import_model(cntk_op) ng_ret = ng.transformers.make_transformer().computation(ng_op)() assert np.array_equiv(cntk_ret, ng_ret)
def _process_sample_weight(self, interactions, sample_weight): if sample_weight is not None: if self.loss == 'warp-kos': raise NotImplementedError('k-OS loss with sample weights ' 'not implemented.') if not isinstance(sample_weight, sp.coo_matrix): raise ValueError('Sample_weight must be a COO matrix.') if sample_weight.shape != interactions.shape: raise ValueError('Sample weight and interactions ' 'matrices must be the same shape') if not (np.array_equal(interactions.row, sample_weight.row) and np.array_equal(interactions.col, sample_weight.col)): raise ValueError('Sample weight and interaction matrix ' 'entries must be in the same order') if sample_weight.data.dtype != CYTHON_DTYPE: sample_weight_data = sample_weight.data.astype(CYTHON_DTYPE) else: sample_weight_data = sample_weight.data else: if np.array_equiv(interactions.data, 1.0): # Re-use interactions data if they are all # ones sample_weight_data = interactions.data else: # Otherwise allocate a new array of ones sample_weight_data = np.ones_like(interactions.data, dtype=CYTHON_DTYPE) return sample_weight_data
def test_init(self): kf = KeyFrame(np.zeros((100, 100)), np.ones((2, 100))) self.assertTrue(np.array_equiv(kf.image, np.zeros((100, 100)))) self.assertTrue(np.array_equiv(kf.features, np.ones((2, 100))))
def test_serializzation(small_model): with io.BytesIO() as fileobj: pickle.dump(small_model, fileobj) fileobj.seek(0) loaded = pickle.load(fileobj) assert all(str(loaded.wv.vocab[w]) == str(small_model.wv.vocab[w]) for w in small_model.wv.vocab) assert all(str(loaded.lvocab[w]) == str(small_model.lvocab[w]) for w in small_model.lvocab) assert numpy.array_equiv(loaded.syn1, small_model.syn1) assert numpy.array_equiv(loaded.wv.syn0, small_model.wv.syn0)
def equality(self): equal = numpy.array_equiv(self.image_i, self.image_j) self.assertTrue(equal)
def array_equal(a1, a2): """ True if two arrays have the same shape and elements, False otherwise. Parameters ---------- a1, a2 : array_like Input arrays. Returns ------- b : bool Returns True if the arrays are equal. See Also -------- allclose: Returns True if two arrays are element-wise equal within a tolerance. array_equiv: Returns True if input arrays are shape consistent and all elements equal. Examples -------- >>> np.array_equal([1, 2], [1, 2]) True >>> np.array_equal(np.array([1, 2]), np.array([1, 2])) True >>> np.array_equal([1, 2], [1, 2, 3]) False >>> np.array_equal([1, 2], [1, 4]) False """ try: a1, a2 = asarray(a1), asarray(a2) except: return False if a1.shape != a2.shape: return False return bool(asarray(a1 == a2).all())
def arr_equiv(): ar1 = np.array([[1, 2], [3, 4]]) ar2 = np.array([[1, 2]]) ar3 = np.array([[1, 2], [1, 2]]) ar4 = np.array([1, 2]) print np.array_equiv(ar1, ar2) # False print np.array_equiv(ar1, ar4) # False print np.array_equiv(ar2, ar3) # True
def preprocess(self, **kwargs): """Construct kernel distance arrays.""" new_times = np.array(kwargs.get('all_times', []), dtype=float) self._codeltatime = kwargs.get(self.key('codeltatime'), -1) self._codeltalambda = kwargs.get(self.key('codeltalambda'), -1) if np.array_equiv(new_times, self._times) and self._preprocessed: return self._times = new_times self._all_band_indices = kwargs.get('all_band_indices', []) self._are_bands = np.array(self._all_band_indices) >= 0 self._freqs = kwargs.get('all_frequencies', []) self._u_freqs = kwargs.get('all_u_frequencies', []) self._waves = np.array([ self._average_wavelengths[bi] if bi >= 0 else C_CGS / self._freqs[i] / ANG_CGS for i, bi in enumerate(self._all_band_indices)]) self._observed = np.array(kwargs.get('observed', []), dtype=bool) self._n_obs = len(self._observed) self._o_times = self._times[self._observed] self._o_waves = self._waves[self._observed] if self._type == 'full': self._times_1 = self._times self._times_2 = self._times self._waves_1 = self._waves self._waves_2 = self._waves elif self._type == 'oa': self._times_1 = self._o_times self._times_2 = self._times self._waves_1 = self._o_waves self._waves_2 = self._waves elif self._type == 'ao': self._times_1 = self._times self._times_2 = self._o_times self._waves_1 = self._waves self._waves_2 = self._o_waves else: self._times_1 = self._o_times self._times_2 = self._o_times self._waves_1 = self._o_waves self._waves_2 = self._o_waves # Time deltas (radial distance) for covariance matrix. if self._codeltatime >= 0: self._dt2mat = self._times_1[:, None] - self._times_2[None, :] self._dt2mat **= 2 self._dt2mat *= -0.5 # Wavelength deltas (radial distance) for covariance matrix. if self._codeltalambda >= 0: self._dl2mat = self._waves_1[:, None] - self._waves_2[None, :] self._dl2mat **= 2 self._dl2mat *= -0.5 self._preprocessed = True
def array_equiv(a1, a2): """ Returns True if input arrays are shape consistent and all elements equal. Shape consistent means they are either the same shape, or one input array can be broadcasted to create the same shape as the other one. Parameters ---------- a1, a2 : array_like Input arrays. Returns ------- out : bool True if equivalent, False otherwise. Examples -------- >>> np.array_equiv([1, 2], [1, 2]) True >>> np.array_equiv([1, 2], [1, 3]) False Showing the shape equivalence: >>> np.array_equiv([1, 2], [[1, 2], [1, 2]]) True >>> np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]]) False >>> np.array_equiv([1, 2], [[1, 2], [1, 3]]) False """ try: a1, a2 = asarray(a1), asarray(a2) except: return False try: multiarray.broadcast(a1, a2) except: return False return bool(asarray(a1 == a2).all())
def parse_array(self, ar): """ Consolidate an array to something smaller and remains broadcastable to the original dimensions. ndim remains the same. todo: - if squeezable in multiple dimensions, squeeze in all dimensions. it currently does this, but the entire most_squeezable_dim can be left out. :param ar: array to be parsed :return: consolidated array """ assert isinstance(ar, np.ndarray) output = np.unique(ar) if output.size == 1: return 0, output.item() elif output.size == 0: return -1, output else: items_per_squeezed_dim = ar.ndim * [0] for dim in range(ar.ndim): output, index = uniquend(ar, axis=dim, return_index=True) if len(index) == 1: items_per_squeezed_dim[dim] = output.size else: items_per_squeezed_dim[dim] = ar.size most_squeezable_dim = items_per_squeezed_dim.index( min(items_per_squeezed_dim)) if ar.size == items_per_squeezed_dim[most_squeezable_dim]: return -1, ar else: # can be squeezable in multiple dimensions # therefore call self cur = uniquend(ar, axis=most_squeezable_dim) # test if broadcastable shape, same elements values assert np.array_equiv(ar, cur) return 1, self.parse_array(cur)[1]