本文整理汇总了Python中numpy.take方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.take方法的具体用法?Python numpy.take怎么用?Python numpy.take使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.take方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: recollect
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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
示例2: _push_queue
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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)
示例3: _shuffle_batch
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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
示例4: amplitudes_to_vector_eomsf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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
示例5: apply_using_lut
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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)
示例6: deriv_wrt_params
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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)
示例7: hessian_wrt_params
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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")
示例8: block2row
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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
示例9: test_4
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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
示例10: test_testTakeTransposeInnerOuter
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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)
示例11: test_testArrayMethods
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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()))
示例12: test_generic_methods
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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())
示例13: test_take
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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]]))
示例14: test_take
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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])
示例15: check_bool
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import take [as 别名]
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