Python numpy.take() Examples
The following are 30
code examples of numpy.take().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: common.py From typhon with MIT License | 6 votes |
def interpolate_halflevels(x, axis=0): """Returns the linear inteprolated halflevels for given array. Parameters: x (ndarray): Data array. axis (int): Axis to interpolate along. Returns: ndarray: Values at halflevels. Examples: >>> interpolate_halflevels([0, 1, 2, 4]) array([ 0.5, 1.5, 3. ]) """ return (np.take(x, range(1, np.shape(x)[axis]), axis=axis) + np.take(x, range(0, np.shape(x)[axis] - 1), axis=axis)) / 2
Example #2
Source File: convolution.py From Traffic_sign_detection_YOLO with MIT License | 6 votes |
def recollect(self, w): if w is None: self.w = w return k = w['kernel'] b = w['biases'] k = np.take(k, self.inp, 2) k = np.take(k, self.out, 3) b = np.take(b, self.out) assert1 = k.shape == tuple(self.wshape['kernel']) assert2 = b.shape == tuple(self.wshape['biases']) assert assert1 and assert2, \ 'Dimension not matching in {} recollect'.format( self._signature) self.w['kernel'] = k self.w['biases'] = b
Example #3
Source File: normalize.py From radiometric_normalization with Apache License 2.0 | 6 votes |
def apply_using_lut(input_band, transformation): '''Applies a linear transformation to an array using a look up table. This creates a uint16 array as the output and clips the output band to the range of a uint16. :param array input_band: A 2D array representing the image data of the a single band :param LinearTransformation transformation: A LinearTransformation (gain and offset) :returns: A 2D array of of the input_band with the transformation applied ''' logging.info('Normalize: Applying linear transformation to band (uint16)') def _apply_lut(band, lut): '''Changes band intensity values based on intensity look up table (lut) ''' if lut.dtype != band.dtype: raise Exception( 'Band ({}) and lut ({}) must be the same data type.').format( band.dtype, lut.dtype) return numpy.take(lut, band, mode='clip') lut = _linear_transformation_to_lut(transformation) return _apply_lut(input_band, lut)
Example #4
Source File: negativesample.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def _push_queue(self, data_list, labels): """Takes a list of numpy arrays for data, and a numpy array for labels. Converts to minibatches and puts it on the queue. """ num_minibatches = 1+self.sample_ratio total_size = len(labels) slice_size = total_size / num_minibatches def slicer(x, s): idx = range(int(s*slice_size), int((s+1)*slice_size)) return np.take(x,idx,0) for i in range(1+self.sample_ratio): nddata = [mx.nd.array(slicer(x,i)) for x in data_list] ndlabels = mx.nd.array(slicer(labels,i)) batch = mx.io.DataBatch(nddata, [ndlabels], provide_data=self.provide_data, provide_label=self.provide_label) self._sampled_queue.append(batch)
Example #5
Source File: negativesample.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def _shuffle_batch(self, data): # Takes a list of NDArrays. Returns a shuffled version as numpy a = data[self.shuffle_data_idx].asnumpy() # Come up with a shuffled index batch_size = data[0].shape[0] si = np.arange(batch_size) np.random.shuffle(si) matches = (si == np.arange(batch_size)) # everywhere it didn't shuffle si -= matches # Shifts down by 1 when true, ensuring it differs # Note shifting down by 1 works in python because -1 is a valid index. #Q: Is this shifting introducing bias? # Shuffle the data with the shuffle index shuf_a = np.take(a,si,0) # like a[si,:] but general for ndarray's # Return similar datastructure to what we got. Convert all to numpy out = [d.asnumpy() for d in data] out[self.shuffle_data_idx] = shuf_a return out
Example #6
Source File: convolution.py From Automatic-Identification-and-Counting-of-Blood-Cells with GNU General Public License v3.0 | 6 votes |
def recollect(self, w): if w is None: self.w = w return k = w['kernel'] b = w['biases'] k = np.take(k, self.inp, 2) k = np.take(k, self.out, 3) b = np.take(b, self.out) assert1 = k.shape == tuple(self.wshape['kernel']) assert2 = b.shape == tuple(self.wshape['biases']) assert assert1 and assert2, \ 'Dimension not matching in {} recollect'.format( self._signature) self.w['kernel'] = k self.w['biases'] = b
Example #7
Source File: convolution.py From Automatic-Identification-and-Counting-of-Blood-Cells with GNU General Public License v3.0 | 6 votes |
def recollect(self, w): if w is None: self.w = w return idx = self.keep_idx k = w['kernel'] b = w['biases'] self.w['kernel'] = np.take(k, idx, 3) self.w['biases'] = np.take(b, idx) if self.batch_norm: m = w['moving_mean'] v = w['moving_variance'] g = w['gamma'] self.w['moving_mean'] = np.take(m, idx) self.w['moving_variance'] = np.take(v, idx) self.w['gamma'] = np.take(g, idx)
Example #8
Source File: eom_uccsd.py From pyscf with Apache License 2.0 | 6 votes |
def amplitudes_to_vector_eomsf(t1, t2, out=None): t1ab, t1ba = t1 t2baaa, t2aaba, t2abbb, t2bbab = t2 nocca, nvirb = t1ab.shape noccb, nvira = t1ba.shape otrila = np.tril_indices(nocca, k=-1) otrilb = np.tril_indices(noccb, k=-1) vtrila = np.tril_indices(nvira, k=-1) vtrilb = np.tril_indices(nvirb, k=-1) baaa = np.take(t2baaa.reshape(noccb*nocca,nvira*nvira), vtrila[0]*nvira+vtrila[1], axis=1) abbb = np.take(t2abbb.reshape(nocca*noccb,nvirb*nvirb), vtrilb[0]*nvirb+vtrilb[1], axis=1) vector = np.hstack((t1ab.ravel(), t1ba.ravel(), baaa.ravel(), t2aaba[otrila].ravel(), abbb.ravel(), t2bbab[otrilb].ravel())) return vector
Example #9
Source File: spamvec.py From pyGSTi with Apache License 2.0 | 6 votes |
def deriv_wrt_params(self, wrtFilter=None): """ Construct a matrix whose columns are the derivatives of the SPAM vector with respect to a single param. Thus, each column is of length get_dimension and there is one column per SPAM vector parameter. An empty 2D array in the StaticSPAMVec case (num_params == 0). Returns ------- numpy array Array of derivatives, shape == (dimension, num_params) """ dtype = complex if self._evotype == 'statevec' else 'd' derivMx = _np.zeros((self.dim, 0), dtype) if wrtFilter is None: return derivMx else: return _np.take(derivMx, wrtFilter, axis=1)
Example #10
Source File: spamvec.py From pyGSTi with Apache License 2.0 | 6 votes |
def deriv_wrt_params(self, wrtFilter=None): """ Construct a matrix whose columns are the derivatives of the SPAM vector with respect to a single param. Thus, each column is of length get_dimension and there is one column per SPAM vector parameter. Returns ------- numpy array Array of derivatives, shape == (dimension, num_params) """ if self._evotype == "statevec": derivMx = _np.concatenate((_np.identity(self.dim, complex), 1j * _np.identity(self.dim, complex)), axis=1) else: derivMx = _np.identity(self.dim, 'd') if wrtFilter is None: return derivMx else: return _np.take(derivMx, wrtFilter, axis=1)
Example #11
Source File: spamvec.py From pyGSTi with Apache License 2.0 | 6 votes |
def deriv_wrt_params(self, wrtFilter=None): """ Construct a matrix whose columns are the derivatives of the SPAM vector with respect to a single param. Thus, each column is of length get_dimension and there is one column per SPAM vector parameter. Returns ------- numpy array Array of derivatives, shape == (dimension, num_params) """ derivMx = _np.identity(self.dim, 'd') # TP vecs assumed real derivMx = derivMx[:, 1:] # remove first col ( <=> first-el parameters ) if wrtFilter is None: return derivMx else: return _np.take(derivMx, wrtFilter, axis=1)
Example #12
Source File: spamvec.py From pyGSTi with Apache License 2.0 | 6 votes |
def hessian_wrt_params(self, wrtFilter1=None, wrtFilter2=None): """ Construct the Hessian of this SPAM vector with respect to its parameters. This function returns a tensor whose first axis corresponds to the flattened operation matrix and whose 2nd and 3rd axes correspond to the parameters that are differentiated with respect to. Parameters ---------- wrtFilter1, wrtFilter2 : list Lists of indices of the paramters to take first and second derivatives with respect to. If None, then derivatives are taken with respect to all of the vectors's parameters. Returns ------- numpy array Hessian with shape (dimension, num_params1, num_params2) """ raise NotImplementedError("TODO: add hessian computation for CPTPSPAMVec")
Example #13
Source File: starfm4py.py From starfm4py with GNU General Public License v3.0 | 6 votes |
def block2row(array, row, folder, block_id=None): if array.shape[0] == windowSize: # Parameters name_string = str(block_id[0] + 1) m,n = array.shape u = m + 1 - windowSize v = n + 1 - windowSize # Get Starting block indices start_idx = np.arange(u)[:,None]*n + np.arange(v) # Get offsetted indices across the height and width of input array offset_idx = np.arange(windowSize)[:,None]*n + np.arange(windowSize) # Get all actual indices & index into input array for final output flat_array = np.take(array,start_idx.ravel()[:,None] + offset_idx.ravel()) # Save to (dask) array in .zarr format file_name = path + folder + name_string + 'r' + row + '.zarr' zarr.save(file_name, flat_array) return array # Divide an image in overlapping blocks
Example #14
Source File: convolution.py From Traffic-Signs-and-Object-Detection with GNU General Public License v3.0 | 6 votes |
def recollect(self, w): if w is None: self.w = w return k = w['kernel'] b = w['biases'] k = np.take(k, self.inp, 2) k = np.take(k, self.out, 3) b = np.take(b, self.out) assert1 = k.shape == tuple(self.wshape['kernel']) assert2 = b.shape == tuple(self.wshape['biases']) assert assert1 and assert2, \ 'Dimension not matching in {} recollect'.format( self._signature) self.w['kernel'] = k self.w['biases'] = b
Example #15
Source File: convolution.py From Traffic-Signs-and-Object-Detection with GNU General Public License v3.0 | 6 votes |
def recollect(self, w): if w is None: self.w = w return idx = self.keep_idx k = w['kernel'] b = w['biases'] self.w['kernel'] = np.take(k, idx, 3) self.w['biases'] = np.take(b, idx) if self.batch_norm: m = w['moving_mean'] v = w['moving_variance'] g = w['gamma'] self.w['moving_mean'] = np.take(m, idx) self.w['moving_variance'] = np.take(v, idx) self.w['gamma'] = np.take(g, idx)
Example #16
Source File: timer_comparison.py From recruit with Apache License 2.0 | 6 votes |
def test_4(self): """ Test of take, transpose, inner, outer products. """ x = self.arange(24) y = np.arange(24) x[5:6] = self.masked x = x.reshape(2, 3, 4) y = y.reshape(2, 3, 4) assert self.allequal(np.transpose(y, (2, 0, 1)), self.transpose(x, (2, 0, 1))) assert self.allequal(np.take(y, (2, 0, 1), 1), self.take(x, (2, 0, 1), 1)) assert self.allequal(np.inner(self.filled(x, 0), self.filled(y, 0)), self.inner(x, y)) assert self.allequal(np.outer(self.filled(x, 0), self.filled(y, 0)), self.outer(x, y)) y = self.array(['abc', 1, 'def', 2, 3], object) y[2] = self.masked t = self.take(y, [0, 3, 4]) assert t[0] == 'abc' assert t[1] == 2 assert t[2] == 3
Example #17
Source File: test_old_ma.py From recruit with Apache License 2.0 | 6 votes |
def test_testTakeTransposeInnerOuter(self): # Test of take, transpose, inner, outer products x = arange(24) y = np.arange(24) x[5:6] = masked x = x.reshape(2, 3, 4) y = y.reshape(2, 3, 4) assert_(eq(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1)))) assert_(eq(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1))) assert_(eq(np.inner(filled(x, 0), filled(y, 0)), inner(x, y))) assert_(eq(np.outer(filled(x, 0), filled(y, 0)), outer(x, y))) y = array(['abc', 1, 'def', 2, 3], object) y[2] = masked t = take(y, [0, 3, 4]) assert_(t[0] == 'abc') assert_(t[1] == 2) assert_(t[2] == 3)
Example #18
Source File: test_old_ma.py From recruit with Apache License 2.0 | 6 votes |
def test_testArrayMethods(self): a = array([1, 3, 2]) assert_(eq(a.any(), a._data.any())) assert_(eq(a.all(), a._data.all())) assert_(eq(a.argmax(), a._data.argmax())) assert_(eq(a.argmin(), a._data.argmin())) assert_(eq(a.choose(0, 1, 2, 3, 4), a._data.choose(0, 1, 2, 3, 4))) assert_(eq(a.compress([1, 0, 1]), a._data.compress([1, 0, 1]))) assert_(eq(a.conj(), a._data.conj())) assert_(eq(a.conjugate(), a._data.conjugate())) m = array([[1, 2], [3, 4]]) assert_(eq(m.diagonal(), m._data.diagonal())) assert_(eq(a.sum(), a._data.sum())) assert_(eq(a.take([1, 2]), a._data.take([1, 2]))) assert_(eq(m.transpose(), m._data.transpose()))
Example #19
Source File: test_core.py From recruit with Apache License 2.0 | 6 votes |
def test_generic_methods(self): # Tests some MaskedArray methods. a = array([1, 3, 2]) assert_equal(a.any(), a._data.any()) assert_equal(a.all(), a._data.all()) assert_equal(a.argmax(), a._data.argmax()) assert_equal(a.argmin(), a._data.argmin()) assert_equal(a.choose(0, 1, 2, 3, 4), a._data.choose(0, 1, 2, 3, 4)) assert_equal(a.compress([1, 0, 1]), a._data.compress([1, 0, 1])) assert_equal(a.conj(), a._data.conj()) assert_equal(a.conjugate(), a._data.conjugate()) m = array([[1, 2], [3, 4]]) assert_equal(m.diagonal(), m._data.diagonal()) assert_equal(a.sum(), a._data.sum()) assert_equal(a.take([1, 2]), a._data.take([1, 2])) assert_equal(m.transpose(), m._data.transpose())
Example #20
Source File: test_core.py From recruit with Apache License 2.0 | 6 votes |
def test_take(self): # Tests take x = masked_array([10, 20, 30, 40], [0, 1, 0, 1]) assert_equal(x.take([0, 0, 3]), masked_array([10, 10, 40], [0, 0, 1])) assert_equal(x.take([0, 0, 3]), x[[0, 0, 3]]) assert_equal(x.take([[0, 1], [0, 1]]), masked_array([[10, 20], [10, 20]], [[0, 1], [0, 1]])) # assert_equal crashes when passed np.ma.mask assert_(x[1] is np.ma.masked) assert_(x.take(1) is np.ma.masked) x = array([[10, 20, 30], [40, 50, 60]], mask=[[0, 0, 1], [1, 0, 0, ]]) assert_equal(x.take([0, 2], axis=1), array([[10, 30], [40, 60]], mask=[[0, 1], [1, 0]])) assert_equal(take(x, [0, 2], axis=1), array([[10, 30], [40, 60]], mask=[[0, 1], [1, 0]]))
Example #21
Source File: test_internals.py From recruit with Apache License 2.0 | 6 votes |
def test_take(self): def assert_take_ok(mgr, axis, indexer): mat = mgr.as_array() taken = mgr.take(indexer, axis) tm.assert_numpy_array_equal(np.take(mat, indexer, axis), taken.as_array(), check_dtype=False) tm.assert_index_equal(mgr.axes[axis].take(indexer), taken.axes[axis]) for mgr in self.MANAGERS: for ax in range(mgr.ndim): # take/fancy indexer assert_take_ok(mgr, ax, []) assert_take_ok(mgr, ax, [0, 0, 0]) assert_take_ok(mgr, ax, lrange(mgr.shape[ax])) if mgr.shape[ax] >= 3: assert_take_ok(mgr, ax, [0, 1, 2]) assert_take_ok(mgr, ax, [-1, -2, -3])
Example #22
Source File: test_nanops.py From recruit with Apache License 2.0 | 6 votes |
def check_bool(self, func, value, correct, *args, **kwargs): while getattr(value, 'ndim', True): try: res0 = func(value, *args, **kwargs) if correct: assert res0 else: assert not res0 except BaseException as exc: exc.args += ('dim: %s' % getattr(value, 'ndim', value), ) raise if not hasattr(value, 'ndim'): break try: value = np.take(value, 0, axis=-1) except ValueError: break
Example #23
Source File: categorical.py From recruit with Apache License 2.0 | 6 votes |
def get_values(self): """ Return the values. For internal compatibility with pandas formatting. Returns ------- values : numpy array A numpy array of the same dtype as categorical.categories.dtype or Index if datetime / periods """ # if we are a datetime and period index, return Index to keep metadata if is_datetimelike(self.categories): return self.categories.take(self._codes, fill_value=np.nan) elif is_integer_dtype(self.categories) and -1 in self._codes: return self.categories.astype("object").take(self._codes, fill_value=np.nan) return np.array(self)
Example #24
Source File: merge.py From recruit with Apache License 2.0 | 6 votes |
def _left_join_on_index(left_ax, right_ax, join_keys, sort=False): if len(join_keys) > 1: if not ((isinstance(right_ax, MultiIndex) and len(join_keys) == right_ax.nlevels)): raise AssertionError("If more than one join key is given then " "'right_ax' must be a MultiIndex and the " "number of join keys must be the number of " "levels in right_ax") left_indexer, right_indexer = \ _get_multiindex_indexer(join_keys, right_ax, sort=sort) else: jkey = join_keys[0] left_indexer, right_indexer = \ _get_single_indexer(jkey, right_ax, sort=sort) if sort or len(left_ax) != len(left_indexer): # if asked to sort or there are 1-to-many matches join_index = left_ax.take(left_indexer) return join_index, left_indexer, right_indexer # left frame preserves order & length of its index return left_ax, None, right_indexer
Example #25
Source File: fourier.py From ibllib with MIT License | 6 votes |
def fexpand(x, ns=1, axis=None): """ Reconstructs full spectrum from positive frequencies Works on the last dimension (contiguous in c-stored array) :param x: numpy.ndarray :param axis: axis along which to perform reduction (last axis by default) :return: numpy.ndarray """ if axis is None: axis = x.ndim - 1 # dec = int(ns % 2) * 2 - 1 # xcomp = np.conj(np.flip(x[..., 1:x.shape[-1] + dec], axis=axis)) ilast = int((ns + (ns % 2)) / 2) xcomp = np.conj(np.flip(np.take(x, np.arange(1, ilast), axis=axis), axis=axis)) return np.concatenate((x, xcomp), axis=axis)
Example #26
Source File: buffer.py From EasyRL with Apache License 2.0 | 6 votes |
def _encode_sample(self, idxes): idxes = np.asarray(idxes) obs = np.take(self._obs, indices=idxes, axis=0) actions = np.take(self._actions, indices=idxes, axis=0) rewards = np.take(self._rewards, indices=idxes, axis=0) next_obs = np.take(self._next_obs, indices=idxes, axis=0) dones = np.take(self._dones, indices=idxes, axis=0) batch_data = dict( obs=obs, actions=actions, rewards=rewards, dones=dones, next_obs=next_obs) return batch_data
Example #27
Source File: donkey.py From deepchem with MIT License | 6 votes |
def load_dataset(filename, whiten=False): f = open(filename, 'r') features = [] labels = [] tracer = 0 for line in f: if tracer == 0: tracer += 1 continue splits = line[:-1].split(',') features.append(splits[-1]) labels.append(float(splits[-2])) features = np.array(features) labels = np.array(labels, dtype='float32').reshape(-1, 1) train_ind, val_ind, test_ins = split(features) train_features = np.take(features, train_ind) train_labels = np.take(labels, train_ind) val_features = np.take(features, val_ind) val_labels = np.take(labels, val_ind) return train_features, train_labels, val_features, val_labels
Example #28
Source File: convolution.py From Traffic_sign_detection_YOLO with MIT License | 6 votes |
def recollect(self, w): if w is None: self.w = w return idx = self.keep_idx k = w['kernel'] b = w['biases'] self.w['kernel'] = np.take(k, idx, 3) self.w['biases'] = np.take(b, idx) if self.batch_norm: m = w['moving_mean'] v = w['moving_variance'] g = w['gamma'] self.w['moving_mean'] = np.take(m, idx) self.w['moving_variance'] = np.take(v, idx) self.w['gamma'] = np.take(g, idx)
Example #29
Source File: test_ndarray.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_take(): for data_ndim in range(2, 5): for idx_ndim in range(1, 4): data_shape = () for _ in range(data_ndim): data_shape += (np.random.randint(low=3, high=6), ) data_real = np.random.normal(size=data_shape).astype('float32') idx_shape = () for _ in range(idx_ndim): idx_shape += (np.random.randint(low=3, high=5), ) idx_real = np.random.randint(low=0, high=data_shape[0], size=idx_shape) data_real_mx = mx.nd.array(data_real) idx_real_mx = mx.nd.array(idx_real) result = mx.nd.take(data_real_mx, idx_real_mx) assert_almost_equal(result.asnumpy(), data_real[idx_real])
Example #30
Source File: test_nanops.py From recruit with Apache License 2.0 | 5 votes |
def check_nancomp(self, checkfun, targ0): arr_float = self.arr_float arr_float1 = self.arr_float1 arr_nan = self.arr_nan arr_nan_nan = self.arr_nan_nan arr_float_nan = self.arr_float_nan arr_float1_nan = self.arr_float1_nan arr_nan_float1 = self.arr_nan_float1 while targ0.ndim: try: res0 = checkfun(arr_float, arr_float1) tm.assert_almost_equal(targ0, res0) if targ0.ndim > 1: targ1 = np.vstack([targ0, arr_nan]) else: targ1 = np.hstack([targ0, arr_nan]) res1 = checkfun(arr_float_nan, arr_float1_nan) tm.assert_numpy_array_equal(targ1, res1, check_dtype=False) targ2 = arr_nan_nan res2 = checkfun(arr_float_nan, arr_nan_float1) tm.assert_numpy_array_equal(targ2, res2, check_dtype=False) except Exception as exc: exc.args += ('ndim: %s' % arr_float.ndim, ) raise try: arr_float = np.take(arr_float, 0, axis=-1) arr_float1 = np.take(arr_float1, 0, axis=-1) arr_nan = np.take(arr_nan, 0, axis=-1) arr_nan_nan = np.take(arr_nan_nan, 0, axis=-1) arr_float_nan = np.take(arr_float_nan, 0, axis=-1) arr_float1_nan = np.take(arr_float1_nan, 0, axis=-1) arr_nan_float1 = np.take(arr_nan_float1, 0, axis=-1) targ0 = np.take(targ0, 0, axis=-1) except ValueError: break