# Python numpy.choose() Examples

The following are 30 code examples for showing how to use numpy.choose(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module , or try the search function .

Example 1
```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)]) ```
Example 2
```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) ```
Example 3
```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, ```
Example 4
```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 ```
Example 5
```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:
"""
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 ```
Example 6
```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

# ============================================================================= ```
Example 7
```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 )
t_xsize, t_ysize )
data_dst = t_band.ReadAsArray( t_xoff, t_yoff, t_xsize, t_ysize )

to_write = Numeric.choose( mask_test, (data_src, data_dst) )

t_band.WriteArray( to_write, t_xoff, t_yoff )

return 0

# ============================================================================= ```
Example 8
```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']
#        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:
a['min'] = N.ma.minimum(a['min'], b['min'])
else:
a['min'] = N.minimum(a['min'], b['min'])
if 'max' in 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 ```
Example 9
```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) ```
Example 10
```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) ```
Example 11
```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 ```
Example 12
```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])) ```
Example 13
```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) ```
Example 14
```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 ```
Example 15
```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])) ```
Example 16
```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
self.replay_buffer.push(state, action, reward, done)
self.num_steps += 1
rewards.append(reward)
dones.append(done)

return states, actions, rewards, dones, qvals ```
Example 17
```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) ```
Example 18
```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 ```
Example 19
```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])) ```
Example 20
```def PriorDistancePotential(sequence=None, paramfile=None):
## 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')
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 ```
Example 21
```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)))) ```
Example 22
```def test_all(self):
a = np.random.normal(0, 1, (4, 5, 6, 7, 8))
for i in range(a.ndim):
amin = a.min(i)
aargmin = a.argmin(i)
axes = list(range(a.ndim))
axes.remove(i)
assert_(all(amin == aargmin.choose(*a.transpose(i,*axes)))) ```
Example 23
```def test_basic(self):
A = np.choose(self.ind, (self.x, self.y))
assert_equal(A, [2, 2, 3]) ```
Example 24
```def test_broadcast1(self):
A = np.choose(self.ind, (self.x2, self.y2))
assert_equal(A, [[2, 2, 3], [2, 2, 3]]) ```
Example 25
```def test_broadcast2(self):
A = np.choose(self.ind, (self.x, self.y2))
assert_equal(A, [[2, 2, 3], [2, 2, 3]]) ```
Example 26
```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) ```
Example 27
```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 ```
Example 28
```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])) ```
Example 29
```def ready_env():
# mock_datetime.now.return_value = datetime.fromtimestamp(1507990500.000000).astimezone(timezone.utc)
mock_datetime.now.return_value = datetime.fromtimestamp(np.choose(np.random.randint(low=10, high=len(indexes)),
indexes)).astimezone(timezone.utc)
mock_datetime.fromtimestamp = lambda *args, **kw: datetime.fromtimestamp(*args, **kw)
mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw)

env = PaperTradingEnvironment(period=5, obs_steps=10, tapi=tapi, fiat="USDT", name='env_test')
# env.fiat = "USDT"
env.balance = env.get_balance()
env.crypto = {"BTC": Decimal('0.00000000'), 'ETH': Decimal('0.00000000')}
yield env
shutil.rmtree(os.path.join(os.path.abspath(os.path.curdir), 'logs')) ```
Example 30
```def _dream_proposals(currentVectors, history, dimensions, nChains, DEpairs, gamma, jitter, eps):
"""
generates and returns proposal vectors given the current states
"""

sampleRange = history.ncombined_history
currentIndex = np.arange(sampleRange - nChains, sampleRange)[:, np.newaxis]
combined_history = history.combined_history

# choose some chains without replacement to combine
chains = _random_no_replace(DEpairs * 2, sampleRange - 1, nChains)

# makes sure we have already selected the current chain so it is not replaced
# this ensures that the the two chosen chains cannot be the same as the
# chain for which the jump is
chains += (chains >= currentIndex)

chainDifferences = (np.sum(combined_history[chains[:, 0:DEpairs], :], axis=1) -
np.sum(combined_history[chains[:, DEpairs:(DEpairs * 2)], :], axis=1))

e = np.random.normal(0, jitter, (nChains, dimensions))

# could replace eps with 1e-6 here
E = np.random.normal(0, eps, (nChains, dimensions))

proposalVectors = currentVectors + \
(1 + e) * gamma[:, np.newaxis] * chainDifferences + E
return proposalVectors ```