# Python numpy.all() Examples

The following are 30 code examples of numpy.all(). 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 also want to check out all available functions/classes of the module , or try the search function .
Example #1
```def is_pos_def(self, A):
"""
Check for positive definiteness.

Parameters
---------
A : array
square symmetric matrix.

Returns
-------
bool
whether matrix is positive-definite.
Warning! Returns false for arrays containing inf or NaN.

"""
# Check for valid numbers
if np.any(np.isnan(A)) or np.any(np.isinf(A)):
return False

else:
return np.all(np.real(np.linalg.eigvals(A)) > 0) ```
Example #2
```def test_one_hot():
"""Check if one_hot returns correct label matrices."""
# Generate label vector
y = np.hstack((np.ones((10,))*0,
np.ones((10,))*1,
np.ones((10,))*2))

# Map to matrix
Y, labels = one_hot(y)

# Check for only 0's and 1's
assert len(np.setdiff1d(np.unique(Y), [0, 1])) == 0

# Check for correct labels
assert np.all(labels == np.unique(y))

# Check correct shape of matrix
assert Y.shape[0] == y.shape[0]
assert Y.shape[1] == len(labels) ```
Example #3
```def test_region_init():
region = Region(
name='test',
description='region description',
west_bound=0.,
east_bound=5,
south_bound=0,
north_bound=90.,
)
assert region.name == 'test'
assert region.description == 'region description'
(Longitude(0.), Longitude(5), 0, 90.))
Example #4
```def find_match(self, pred, gt):
'''
Match component to balls.
'''
batch_size, n_frames_input, n_components, _ = pred.shape
diff = pred.reshape(batch_size, n_frames_input, n_components, 1, 2) - \
gt.reshape(batch_size, n_frames_input, 1, n_components, 2)
diff = np.sum(np.sum(diff ** 2, axis=-1), axis=1)
# Direct indices
indices = np.argmin(diff, axis=2)
ambiguous = np.zeros(batch_size, dtype=np.int8)
for i in range(batch_size):
_, counts = np.unique(indices[i], return_counts=True)
if not np.all(counts == 1):
ambiguous[i] = 1
return indices, ambiguous ```
Example #5
```def test_bounds(self):
"""
Test that out-of-bounds coordinates return NaN reddening, and that
in-bounds coordinates do not return NaN reddening.
"""

for mode in (['random_sample', 'random_sample_per_pix',
'median', 'samples', 'mean']):
# Draw random coordinates, both above and below dec = -30 degree line
n_pix = 1000
ra = -180. + 360.*np.random.random(n_pix)
dec = -75. + 90.*np.random.random(n_pix)    # 45 degrees above/below
c = coords.SkyCoord(ra, dec, frame='icrs', unit='deg')

ebv_calc = self._bayestar(c, mode=mode)

nan_below = np.isnan(ebv_calc[dec < -35.])
nan_above = np.isnan(ebv_calc[dec > -25.])
pct_nan_above = np.sum(nan_above) / float(nan_above.size)

# print r'{:s}: {:.5f}% nan above dec=-25 deg.'.format(mode, 100.*pct_nan_above)

self.assertTrue(np.all(nan_below))
self.assertTrue(pct_nan_above < 0.05) ```
Example #6
```def map_values(values, pos, target_pos, dtype=None, nan=dat.CPG_NAN):
"""Maps `values` array at positions `pos` to `target_pos`.

Inserts `nan` for uncovered positions.
"""
assert len(values) == len(pos)
assert np.all(pos == np.sort(pos))
assert np.all(target_pos == np.sort(target_pos))

values = values.ravel()
pos = pos.ravel()
target_pos = target_pos.ravel()
idx = np.in1d(pos, target_pos)
pos = pos[idx]
values = values[idx]
if not dtype:
dtype = values.dtype
target_values = np.empty(len(target_pos), dtype=dtype)
target_values.fill(nan)
idx = np.in1d(target_pos, pos).nonzero()[0]
assert len(idx) == len(values)
assert np.all(target_pos[idx] == pos)
target_values[idx] = values
return target_values ```
Example #7
```def image_to_cortex(self, image,
surface='midgray', hemi=None, affine=Ellipsis, method=None, fill=0,
dtype=None, weights=None):
'''
sub.image_to_cortex(image) is equivalent to the tuple
(sub.lh.from_image(image), sub.rh.from_image(image)).
'''
if hemi is None: hemi = 'both'
hemi = hemi.lower()
if hemi in ['both', 'lr', 'all', 'auto']:
return tuple(
[self.image_to_cortex(image, surface=surface, hemi=h, affine=affine,
method=method, fill=fill, dtype=dtype, weights=weights)
for h in ['lh', 'rh']])
else:
hemi = getattr(self, hemi)
return hemi.from_image(image, surface=surface, affine=affine,
method=method, fill=fill, dtype=dtype, weights=weights) ```
Example #8
```def get_constraint_value(self, applyMultiframePrior=True):
"""
Compute all partial Structure Factor (SQs).

:Parameters:
#. applyMultiframePrior (boolean): Whether to apply subframe weight
and prior to the total. This will only have an effect when used
frame is a subframe and in case subframe weight and prior is
defined.

:Returns:
#. SQs (dictionary): The SQs dictionnary, where keys are the
element wise intra and inter molecular SQs and values are
the computed SQs.
"""
if self.data is None:
LOGGER.warn("data must be computed first using 'compute_data' method.")
return {}
return self._get_constraint_value(self.data, applyMultiframePrior=applyMultiframePrior) ```
Example #9
```def getImgIds(self, imgIds=[], catIds=[]):
'''
Get img ids that satisfy given filter conditions.
:param imgIds (int array) : get imgs for given ids
:param catIds (int array) : get imgs with all given cats
:return: ids (int array)  : integer array of img ids
'''
imgIds = imgIds if type(imgIds) == list else [imgIds]
catIds = catIds if type(catIds) == list else [catIds]

if len(imgIds) == len(catIds) == 0:
ids = self.imgs.keys()
else:
ids = set(imgIds)
for i, catId in enumerate(catIds):
if i == 0 and len(ids) == 0:
ids = set(self.catToImgs[catId])
else:
ids &= set(self.catToImgs[catId])
return list(ids) ```
Example #10
```def annToRLE(self, ann):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
t = self.imgs[ann['image_id']]
h, w = t['height'], t['width']
segm = ann['segmentation']
if type(segm) == list:
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
# rles = maskUtils.frPyObjects(segm, h, w)
elif type(segm['counts']) == list:
# uncompressed RLE
# rle = maskUtils.frPyObjects(segm, h, w)
else:
# rle
rle = ann['segmentation']
return rle ```
Example #11
```def test_module_input_grads():
a = mx.sym.Variable('a', __layout__='NC')
b = mx.sym.Variable('b', __layout__='NC')
c = mx.sym.Variable('c', __layout__='NC')

c = a + 2 * b + 3 * c
net = mx.mod.Module(c, data_names=['b', 'c', 'a'], label_names=None,
context=[mx.cpu(0), mx.cpu(1)])
net.bind(data_shapes=[['b', (5, 5)], ['c', (5, 5)], ['a', (5, 5)]],
net.init_params()

net.forward(data_batch=mx.io.DataBatch(data=[nd.ones((5, 5)),
nd.ones((5, 5)),
nd.ones((5, 5))]))
Example #12
```def test_module_reshape():
data = mx.sym.Variable('data')
sym = mx.sym.FullyConnected(data, num_hidden=20, name='fc')

dshape = (7, 20)
mod = mx.mod.Module(sym, ('data',), None, context=[mx.cpu(0), mx.cpu(1)])
mod.bind(data_shapes=[('data', dshape)])
mod.init_params()
mod.init_optimizer(optimizer_params={'learning_rate': 1})

mod.forward(mx.io.DataBatch(data=[mx.nd.ones(dshape)],
label=None))
mod.backward([mx.nd.ones(dshape)])
mod.update()
assert mod.get_outputs()[0].shape == dshape
assert (mod.get_params()[0]['fc_bias'].asnumpy() == -1).all()

dshape = (14, 20)
mod.reshape(data_shapes=[('data', dshape)])
mod.forward(mx.io.DataBatch(data=[mx.nd.ones(dshape)],
label=None))
mod.backward([mx.nd.ones(dshape)])
mod.update()
assert mod.get_outputs()[0].shape == dshape
assert (mod.get_params()[0]['fc_bias'].asnumpy() == -3).all() ```
Example #13
```def test_sparse_nd_setitem():
def check_sparse_nd_setitem(stype, shape, dst):
x = mx.nd.zeros(shape=shape, stype=stype)
x[:] = dst
dst_nd = mx.nd.array(dst) if isinstance(dst, (np.ndarray, np.generic)) else dst
assert np.all(x.asnumpy() == dst_nd.asnumpy() if isinstance(dst_nd, NDArray) else dst)

shape = rand_shape_2d()
for stype in ['row_sparse', 'csr']:
# ndarray assignment
check_sparse_nd_setitem(stype, shape, rand_ndarray(shape, 'default'))
check_sparse_nd_setitem(stype, shape, rand_ndarray(shape, stype))
# numpy assignment
check_sparse_nd_setitem(stype, shape, np.ones(shape))
# scalar assigned to row_sparse NDArray
check_sparse_nd_setitem('row_sparse', shape, 2) ```
Example #14
```def _project_to_map(map, vertex, wt=None, ignore_points_outside_map=False):
"""Projects points to map, returns how many points are present at each
location."""
num_points = np.zeros((map.size[1], map.size[0]))
vertex_ = vertex[:, :2] - map.origin
vertex_ = np.round(vertex_ / map.resolution).astype(np.int)
if ignore_points_outside_map:
good_ind = np.all(np.array([vertex_[:,1] >= 0, vertex_[:,1] < map.size[1],
vertex_[:,0] >= 0, vertex_[:,0] < map.size[0]]),
axis=0)
vertex_ = vertex_[good_ind, :]
if wt is not None:
wt = wt[good_ind, :]
if wt is None:
np.add.at(num_points, (vertex_[:, 1], vertex_[:, 0]), 1)
else:
assert(wt.shape[0] == vertex.shape[0]), \
'number of weights should be same as vertices.'
np.add.at(num_points, (vertex_[:, 1], vertex_[:, 0]), wt)
return num_points ```
Example #15
```def raw_valid_fn_vec(self, xyt):
"""Returns if the given set of nodes is valid or not."""
height = self.traversible.shape[0]
width = self.traversible.shape[1]
x = np.round(xyt[:,[0]]).astype(np.int32)
y = np.round(xyt[:,[1]]).astype(np.int32)
is_inside = np.all(np.concatenate((x >= 0, y >= 0,
x < width, y < height), axis=1), axis=1)
x = np.minimum(np.maximum(x, 0), width-1)
y = np.minimum(np.maximum(y, 0), height-1)
ind = np.ravel_multi_index((y,x), self.traversible.shape)
is_traversible = self.traversible.ravel()[ind]

is_valid = np.all(np.concatenate((is_inside[:,np.newaxis], is_traversible),
axis=1), axis=1)
return is_valid ```
Example #16
```def valid_fn_vec(self, pqr):
"""Returns if the given set of nodes is valid or not."""
xyt = self.to_actual_xyt_vec(np.array(pqr))
height = self.traversible.shape[0]
width = self.traversible.shape[1]
x = np.round(xyt[:,[0]]).astype(np.int32)
y = np.round(xyt[:,[1]]).astype(np.int32)
is_inside = np.all(np.concatenate((x >= 0, y >= 0,
x < width, y < height), axis=1), axis=1)
x = np.minimum(np.maximum(x, 0), width-1)
y = np.minimum(np.maximum(y, 0), height-1)
ind = np.ravel_multi_index((y,x), self.traversible.shape)
is_traversible = self.traversible.ravel()[ind]

is_valid = np.all(np.concatenate((is_inside[:,np.newaxis], is_traversible),
axis=1), axis=1)
return is_valid ```
Example #17
```def is_pos_def(X):
"""Check for positive definiteness."""
return np.all(np.linalg.eigvals(X) > 0) ```
Example #18
```def remove_intercept(self, X):
"""Remove 1's from data as last features."""
# Data shape
N, D = X.shape

# Find which column contains the intercept
intercept_index = []
for d in range(D):
if np.all(X[:, d] == 0):
intercept_index.append(d)

# Remove intercept columns
X = X[:, np.setdiff1d(np.arange(D), intercept_index)]

return X, D-len(intercept_index) ```
Example #19
```def is_pos_def(self, A):
"""Check for positive definiteness."""
return np.all(np.real(np.linalg.eigvals(A)) > 0) ```
Example #20
```def find_medioid(self, X, Y):
"""
Find point with minimal distance to all other points.

Parameters
----------
X : array
data set, with N samples x D features.
Y : array
labels to select for which samples to compute distances.

Returns
-------
x : array
medioid
ix : int
index of medioid

"""
# Initiate an array with infinities
A = np.full((X.shape[0],), np.inf)

# Insert sum of distances to other points
A[Y] = np.sum(squareform(pdist(X[Y, :])), axis=1)

# Find the index of the point with the smallest distance
ix = np.argmin(A)

return X[ix, :], ix ```
Example #21
```def test_iwe_ratio_Gaussians():
"""Test for estimating ratio of Gaussians."""
X = rnd.randn(10, 2)
Z = rnd.randn(10, 2) + 1
clf = ImportanceWeightedClassifier()
iw = clf.iwe_ratio_gaussians(X, Z)
assert np.all(iw >= 0) ```
Example #22
```def test_iwe_logistic_discrimination():
"""Test for estimating through logistic classifier."""
X = rnd.randn(10, 2)
Z = rnd.randn(10, 2) + 1
clf = ImportanceWeightedClassifier()
iw = clf.iwe_logistic_discrimination(X, Z)
assert np.all(iw >= 0) ```
Example #23
```def test_iwe_kernel_densities():
"""Test for estimating through kernel density estimation."""
X = rnd.randn(10, 2)
Z = rnd.randn(10, 2) + 1
clf = ImportanceWeightedClassifier()
iw = clf.iwe_kernel_densities(X, Z)
assert np.all(iw >= 0) ```
Example #24
```def test_iwe_kernel_mean_matching():
"""Test for estimating through kernel mean matching."""
X = rnd.randn(10, 2)
Z = rnd.randn(10, 2) + 1
clf = ImportanceWeightedClassifier()
iw = clf.iwe_kernel_mean_matching(X, Z)
assert np.all(iw >= 0) ```
Example #25
```def does_coord_increase_w_index(arr):
"""Determine if the array values increase with the index.

Useful, e.g., for pressure, which sometimes is indexed surface to TOA and
sometimes the opposite.
"""
diff = np.diff(arr)
if not np.all(np.abs(np.sign(diff))):
raise ValueError("Array is not monotonic: {}".format(arr))
# Since we know its monotonic, just test the first value.
return bool(diff[0]) ```
Example #26
```def test_region_init_mult_rect():
bounds_in = [[1, 2, 3, 4], (-12, -30, 2.3, 9)]
for (w, e, s, n), bounds in zip(bounds_in, region.mask_bounds):
assert isinstance(bounds, tuple)
assert np.all(bounds == (Longitude(w), Longitude(e), s, n)) ```
Example #27
```def test_lon_eq(obj1, obj2):
assert np.all(obj1 == obj2)
assert np.all(obj2 == obj1) ```
Example #28
```def test_lon_lt(obj1, obj2):
```def test_lon_leq(obj1, obj2):
```def test_lon_geq(obj1, obj2):