本文整理匯總了Python中numpy.choose方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.choose方法的具體用法?Python numpy.choose怎麽用?Python numpy.choose使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.choose方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: zero_if_close
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def zero_if_close(a, tol=1.e-15):
"""set real and/or imaginary part to 0 if their absolute value is smaller than `tol`.
Parameters
----------
a : ndarray
numpy array to be rounded
tol : float
the threashold which values to consider as '0'.
"""
if a.dtype == np.complex128 or a.dtype == np.complex64:
ar = np.choose(np.abs(a.real) < tol, [a.real, np.zeros(a.shape)])
ai = np.choose(np.abs(a.imag) < tol, [a.imag, np.zeros(a.shape)])
return ar + 1j * ai
else:
return np.choose(np.abs(a) < tol, [a, np.zeros_like(a)])
示例2: gather_numpy
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def gather_numpy(inputs, dim, index):
"""
Gathers values along an axis specified by dim.
For a 3-D tensor the output is specified by:
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2
:param dim: The axis along which to index
:param index: A tensor of indices of elements to gather
:return: tensor of gathered values
"""
idx_xsection_shape = index.shape[:dim] + index.shape[dim + 1:]
self_xsection_shape = inputs.shape[:dim] + inputs.shape[dim + 1:]
if idx_xsection_shape != self_xsection_shape:
raise ValueError("Except for dimension " + str(dim) +
", all dimensions of index and self should be the same size")
if index.dtype != np.dtype('int_'):
raise TypeError("The values of index must be integers")
data_swaped = np.swapaxes(inputs, 0, dim)
index_swaped = np.swapaxes(index, 0, dim)
gathered = np.choose(index_swaped, data_swaped)
return np.swapaxes(gathered, 0, dim)
示例3: forward
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def forward(self, inputs):
self.retain_inputs((1,))
x, t = inputs
self._in_shape = x.shape
self._in_dtype = x.dtype
if chainer.is_debug():
if not ((0 <= t).all() and
(t < x.shape[1]).all()):
msg = 'Each label `t` need to satisfty `0 <= t < x.shape[1]`'
raise ValueError(msg)
xp = backend.get_array_module(x)
if xp is numpy:
# This code is equivalent to `t.choose(x.T)`, but `numpy.choose`
# does not work when `x.shape[1] > 32`.
return x[six.moves.range(t.size), t],
else:
y = cuda.elementwise(
'S t, raw T x',
'T y',
'int ind[] = {i, t}; y = x[ind];',
'getitem_fwd'
)(t, x)
return y,
示例4: compress_image
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def compress_image(img, num_clusters):
# Convert input image into (num_samples, num_features)
# array to run kmeans clustering algorithm
X = img.reshape((-1, 1))
# Run kmeans on input data
kmeans = cluster.KMeans(n_clusters=num_clusters, n_init=4, random_state=5)
kmeans.fit(X)
centroids = kmeans.cluster_centers_.squeeze()
labels = kmeans.labels_
# Assign each value to the nearest centroid and
# reshape it to the original image shape
input_image_compressed = np.choose(labels, centroids).reshape(img.shape)
return input_image_compressed
開發者ID:PacktPublishing,項目名稱:Python-Machine-Learning-Cookbook-Second-Edition,代碼行數:18,代碼來源:vector_quantization.py
示例5: adjust_angle_array
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def adjust_angle_array(angles):
"""
Resolve ambiguities within a array of angles. It assumes neighboring angles should be close.
Args:
angles: an array of angles.
Return:
Adjusted angle array.
"""
new_angle = np.copy(angles)
angle_diff = angles[1:] - angles[:-1]
diff_cand = angle_diff[:, None] - np.array([-math.pi * 4, -math.pi * 2, 0, math.pi * 2, math.pi * 4])
min_id = np.argmin(np.abs(diff_cand), axis=1)
diffs = np.choose(min_id, diff_cand.T)
new_angle[1:] = np.cumsum(diffs) + new_angle[0]
return new_angle
示例6: raster_copy_with_nodata
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def raster_copy_with_nodata( s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n,
t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n,
nodata ):
try:
import numpy as Numeric
except ImportError:
import Numeric
s_band = s_fh.GetRasterBand( s_band_n )
t_band = t_fh.GetRasterBand( t_band_n )
data_src = s_band.ReadAsArray( s_xoff, s_yoff, s_xsize, s_ysize,
t_xsize, t_ysize )
data_dst = t_band.ReadAsArray( t_xoff, t_yoff, t_xsize, t_ysize )
nodata_test = Numeric.equal(data_src,nodata)
to_write = Numeric.choose( nodata_test, (data_src, data_dst) )
t_band.WriteArray( to_write, t_xoff, t_yoff )
return 0
# =============================================================================
示例7: raster_copy_with_mask
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def raster_copy_with_mask( s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n,
t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n,
m_band ):
try:
import numpy as Numeric
except ImportError:
import Numeric
s_band = s_fh.GetRasterBand( s_band_n )
t_band = t_fh.GetRasterBand( t_band_n )
data_src = s_band.ReadAsArray( s_xoff, s_yoff, s_xsize, s_ysize,
t_xsize, t_ysize )
data_mask = m_band.ReadAsArray( s_xoff, s_yoff, s_xsize, s_ysize,
t_xsize, t_ysize )
data_dst = t_band.ReadAsArray( t_xoff, t_yoff, t_xsize, t_ysize )
mask_test = Numeric.equal(data_mask, 0)
to_write = Numeric.choose( mask_test, (data_src, data_dst) )
t_band.WriteArray( to_write, t_xoff, t_yoff )
return 0
# =============================================================================
示例8: combine
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def combine(a, b):
'''Combine accumulators by summing.'''
print >> sys.stderr, 'Combining accumulators . . .'
if 'urls' in a['_meta'] and 'urls' in b['_meta']:
a['_meta']['urls'].extend(b['_meta']['urls'])
if 'mean' in a:
ntotal = a['count'] + b['count']
# mask = N.not_equal(ntotal, 0)
# a['mean'] = N.choose(mask, (0, (a['mean'] * a['count'] + b['mean'] * b['count']) / ntotal))
a['mean'] = (a['mean'] * a['count'] + b['mean'] * b['count']) / ntotal
if 'min' in a:
if N.ma.isMaskedArray(a):
a['min'] = N.ma.minimum(a['min'], b['min'])
else:
a['min'] = N.minimum(a['min'], b['min'])
if 'max' in a:
if N.ma.isMaskedArray(a):
a['max'] = N.ma.maximum(a['max'], b['max'])
else:
a['max'] = N.maximum(a['max'], b['max'])
for k in a.keys():
if k[0] == '_': continue
if k != 'mean' and k != 'min' and k != 'max':
a[k] += b[k] # just sum count and other moments
return a
示例9: int_abs
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def int_abs(arr):
""" Absolute values of array taking care of max negative int values
Parameters
----------
arr : array-like
Returns
-------
abs_arr : array
array the same shape as `arr` in which all negative numbers have been
changed to positive numbers with the magnitude.
Examples
--------
This kind of thing is confusing in base numpy:
>>> import numpy as np
>>> np.abs(np.int8(-128))
-128
``int_abs`` fixes that:
>>> int_abs(np.int8(-128))
128
>>> int_abs(np.array([-128, 127], dtype=np.int8))
array([128, 127], dtype=uint8)
>>> int_abs(np.array([-128, 127], dtype=np.float32))
array([ 128., 127.], dtype=float32)
"""
arr = np.array(arr, copy=False)
dt = arr.dtype
if dt.kind == 'u':
return arr
if dt.kind != 'i':
return np.absolute(arr)
out = arr.astype(np.dtype(dt.str.replace('i', 'u')))
return np.choose(arr < 0, (arr, arr * -1), out=out)
示例10: test_choose
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def test_choose(self):
choices = [[0, 1, 2],
[3, 4, 5],
[5, 6, 7]]
tgt = [5, 1, 5]
a = [2, 0, 1]
out = np.choose(a, choices)
assert_equal(out, tgt)
示例11: clip
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def clip(self, a, m, M, out=None):
# use slow-clip
selector = np.less(a, m) + 2*np.greater(a, M)
return selector.choose((a, m, M), out=out)
# Handy functions
示例12: test_mixed
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def test_mixed(self):
c = np.array([True, True])
a = np.array([True, True])
assert_equal(np.choose(c, (a, 1)), np.array([1, 1]))
示例13: take_one_step
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def take_one_step(self, envs, add_to_replay=False):
states = [
e.last_state if hasattr(e, 'last_state') else e.reset()
for e in envs
]
tensor_states = torch.tensor(states, device=self.compute_device, dtype=torch.float32)
qvals = self.training_model(tensor_states).detach().cpu().numpy()
num_states, num_actions = qvals.shape
actions = np.argmax(qvals, axis=-1)
random_actions = get_rng().integers(num_actions, size=num_states)
use_random = get_rng().random(num_states) < self.epsilon
actions = np.choose(use_random, [actions, random_actions])
rewards = []
dones = []
for env, state, action in zip(envs, states, actions):
next_state, reward, done, info = env.step(action)
if done:
next_state = env.reset()
env.last_state = next_state
if add_to_replay:
self.replay_buffer.push(state, action, reward, done)
self.num_steps += 1
rewards.append(reward)
dones.append(done)
return states, actions, rewards, dones, qvals
示例14: PriorDistancePotential
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def PriorDistancePotential(sequence=None, paramfile=None):
##add pairwise distance potential here
## an example paramfile is data4contact/pdb25-pair-dist.pkl
if not os.path.isfile(paramfile):
print 'cannot find the parameter file: ', paramfile
exit(-1)
fh = open(paramfile,'rb')
potential = cPickle.load(fh)[0].astype(np.float32)
fh.close()
assert (len(potential.shape) == 4)
potentialFeature = np.zeros((len(sequence), len(sequence), potential.shape[-1]), dtype=theano.config.floatX)
##convert AAs to integers
ids = [ ord(AA) - ord('A') for AA in sequence ]
##the below procedure is not very effective. What we can do is to generate a full matrix of only long-range potential using OuterConcatenate and np.choose
##and then using the np.diagonal() function to replace near-, short- and medium-range potential in the full matrix
for i, id0 in zip(xrange(len(ids)), ids):
for j, id1 in zip(xrange(i+1, len(ids)), ids[i+1:]):
if j-i<6:
sepIndex = 0
elif j-i < 12:
sepIndex = 1
elif j-i < 24:
sepIndex = 2
else:
sepIndex = 3
if id0 <=id1:
potentialFeature[i][j]=potential[sepIndex][id0][id1]
else:
potentialFeature[i][j]=potential[sepIndex][id1][id0]
potentialFeature[j][i]=potentialFeature[i][j]
return potentialFeature
##d is a dictionary for a protein
示例15: test_all
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import choose [as 別名]
def test_all(self):
a = np.random.normal(0, 1, (4, 5, 6, 7, 8))
for i in range(a.ndim):
amax = a.max(i)
aargmax = a.argmax(i)
axes = list(range(a.ndim))
axes.remove(i)
assert_(all(amax == aargmax.choose(*a.transpose(i,*axes))))