# Python numpy.union1d() Examples

The following are 30 code examples for showing how to use numpy.union1d(). 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 _optimize_2D(nodes, triangles, stay=[]):
''' Optimize the locations of the points by moving them towards the center
of their patch. This is done iterativally for all points for a number of
iterations and using a .05 step length'''
boundary = edges[adjacency_list[:, 1] == -1].reshape(-1)
stay = np.union1d(boundary, stay)
stay = stay.astype(int)
n_iter = 5
step_length = .05
mean_bar = np.zeros_like(nodes)
new_nodes = np.copy(nodes)
k = np.bincount(triangles.reshape(-1), minlength=len(nodes))
for n in range(n_iter):
bar = np.mean(new_nodes[triangles], axis=1)
for i in range(2):
mean_bar[:, i] = np.bincount(triangles.reshape(-1),
weights=np.repeat(bar[:, i], 3),
minlength=len(nodes))
mean_bar /= k[:, None]
new_nodes += step_length * (mean_bar - new_nodes)
new_nodes[stay] = nodes[stay]
return new_nodes ```
Example 2
```def testUnion1dExecution(self):
rs = np.random.RandomState(0)
raw1 = rs.random(10)
raw2 = rs.random(9)

t1 = tensor(raw1, chunk_size=3)
t2 = tensor(raw2, chunk_size=4)

t = union1d(t1, t2, aggregate_size=1)
res = self.executor.execute_tensor(t, concat=True)[0]
expected = np.union1d(raw1, raw2)
np.testing.assert_array_equal(res, expected)

t = union1d(t1, t2)
res = self.executor.execute_tensor(t, concat=True)[0]
expected = np.union1d(raw1, raw2)
np.testing.assert_array_equal(res, expected) ```
Example 3
```def compute_miou(coords, preds, targets, weights):
coords, preds, targets, weights = filter_points(coords, preds, targets, weights)
seen_classes = np.unique(targets)

pointmiou = np.zeros(CONF.NUM_CLASSES)
voxmiou = np.zeros(CONF.NUM_CLASSES)

uvidx, uvlabel, _ = point_cloud_label_to_surface_voxel_label_fast(coords, np.concatenate((np.expand_dims(targets,1),np.expand_dims(preds,1)),axis=1), res=0.02)
for l in seen_classes:
target_label = np.arange(targets.shape[0])[targets==l]
pred_label = np.arange(preds.shape[0])[preds==l]
num_intersection_label = np.intersect1d(pred_label, target_label).shape[0]
num_union_label = np.union1d(pred_label, target_label).shape[0]
pointmiou[l] = num_intersection_label / (num_union_label + 1e-8)

target_label_vox = uvidx[(uvlabel[:, 0] == l)]
pred_label_vox = uvidx[(uvlabel[:, 1] == l)]
num_intersection_label_vox = np.intersect1d(pred_label_vox, target_label_vox).shape[0]
num_union_label_vox = np.union1d(pred_label_vox, target_label_vox).shape[0]
voxmiou[l] = num_intersection_label_vox / (num_union_label_vox + 1e-8)

Example 4
```def cov_params_wo_det(self):
# rows & cols to be dropped (related to deterministic terms inside the
# cointegration relation)
start_i = self.neqs**2  # first elements belong to alpha @ beta.T
end_i = start_i + self.neqs * self.det_coef_coint.shape[0]
to_drop_i = np.arange(start_i, end_i)

# rows & cols to be dropped (related to deterministic terms outside of
# the cointegration relation)
cov = self.cov_params_default
cov_size = len(cov)
to_drop_o = np.arange(cov_size-self.det_coef.size, cov_size)

to_drop = np.union1d(to_drop_i, to_drop_o)

# standard errors: ```
Example 5
```def test_stratified_shuffle_split_overlap_train_test_bug():
# See https://github.com/scikit-learn/scikit-learn/issues/6121 for
# the original bug report
y = [0, 1, 2, 3] * 3 + [4, 5] * 5
X = np.ones_like(y)

sss = StratifiedShuffleSplit(n_splits=1,
test_size=0.5, random_state=0)

train, test = next(sss.split(X=X, y=y))

# no overlap
assert_array_equal(np.intersect1d(train, test), [])

# complete partition
assert_array_equal(np.union1d(train, test), np.arange(len(y))) ```
Example 6
```def test_stratified_shuffle_split_multilabel():
# fix for issue 9037
for y in [np.array([[0, 1], [1, 0], [1, 0], [0, 1]]),
np.array([[0, 1], [1, 1], [1, 1], [0, 1]])]:
X = np.ones_like(y)
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.5, random_state=0)
train, test = next(sss.split(X=X, y=y))
y_train = y[train]
y_test = y[test]

# no overlap
assert_array_equal(np.intersect1d(train, test), [])

# complete partition
assert_array_equal(np.union1d(train, test), np.arange(len(y)))

# correct stratification of entire rows
# (by design, here y[:, 0] uniquely determines the entire row of y)
expected_ratio = np.mean(y[:, 0])
assert_equal(expected_ratio, np.mean(y_train[:, 0]))
assert_equal(expected_ratio, np.mean(y_test[:, 0])) ```
Example 7
```def test_evaluate_performance_too_many_entities_warning():
model = TransE(batches_count=200, seed=0, epochs=1, k=5, eta=1, verbose=True)
model.fit(X['train'])

# no entity list declared
with pytest.warns(UserWarning):
evaluate_performance(X['test'][::100], model, verbose=True, corrupt_side='o')

# with larger than threshold entity list
with pytest.warns(UserWarning):
# TOO_MANY_ENT_TH threshold is set to 50,000 entities. Using explicit value to comply with linting
# and thus avoiding exporting unused global variable.
entities_subset = np.union1d(np.unique(X["train"][:, 0]), np.unique(X["train"][:, 2]))[:50000]
evaluate_performance(X['test'][::100], model, verbose=True, corrupt_side='o', entities_subset=entities_subset)

# with small entity list (no exception expected)
evaluate_performance(X['test'][::100], model, verbose=True, corrupt_side='o', entities_subset=entities_subset[:10])

# with smaller dataset, no entity list declared (no exception expected)
model_wn18 = TransE(batches_count=200, seed=0, epochs=1, k=5, eta=1, verbose=True)
model_wn18.fit(X_wn18rr['train'])
evaluate_performance(X_wn18rr['test'][::100], model_wn18, verbose=True, corrupt_side='o') ```
Example 8
```def test_yago_3_10():
assert len(yago_3_10['train']) == 1079040
assert len(yago_3_10['valid']) == 5000 - 22
assert len(yago_3_10['test']) == 5000 - 18

# ent_train = np.union1d(np.unique(yago_3_10["train"][:,0]), np.unique(yago_3_10["train"][:,2]))
# ent_valid = np.union1d(np.unique(yago_3_10["valid"][:,0]), np.unique(yago_3_10["valid"][:,2]))
# ent_test = np.union1d(np.unique(yago_3_10["test"][:,0]), np.unique(yago_3_10["test"][:,2]))

# assert len(set(ent_valid) - set(ent_train)) == 22
# assert len (set(ent_test) - ((set(ent_valid) & set(ent_train)) | set(ent_train))) == 18

# distinct_ent = np.union1d(np.union1d(ent_train, ent_valid), ent_test)
# distinct_rel = np.union1d(np.union1d(np.unique(yago_3_10["train"][:,1]), np.unique(yago_3_10["train"][:,1])),
#                           np.unique(yago_3_10["train"][:,1]))

# assert len(distinct_ent) == 123182
# assert len(distinct_rel) == 37 ```
Example 9
```def test_wn18rr():

ent_train = np.union1d(np.unique(wn18rr["train"][:, 0]), np.unique(wn18rr["train"][:, 2]))
ent_valid = np.union1d(np.unique(wn18rr["valid"][:, 0]), np.unique(wn18rr["valid"][:, 2]))
ent_test = np.union1d(np.unique(wn18rr["test"][:, 0]), np.unique(wn18rr["test"][:, 2]))
distinct_ent = np.union1d(np.union1d(ent_train, ent_valid), ent_test)
distinct_rel = np.union1d(np.union1d(np.unique(wn18rr["train"][:, 1]), np.unique(wn18rr["train"][:, 1])),
np.unique(wn18rr["train"][:, 1]))

assert len(wn18rr['train']) == 86835

# - 210 because 210 triples containing unseen entities are removed
assert len(wn18rr['valid']) == 3034 - 210

# - 210 because 210 triples containing unseen entities are removed
assert len(wn18rr['test']) == 3134 - 210 ```
Example 10
```def compute_recall(self,n_points,nnn,sr):
sample_indices=np.random.choice(self.numsamples,n_points)
recalls=[]
elapsed=[]
numpredicted=[]
for qidx in sample_indices:
start=time.time()
#preds=np.array([m.query_bins(qidx,sr) for m in self.models])
predicted=self.firstmodel.query_bins(qidx,sr)#reduce(np.union1d,preds)
if len(predicted)<nnn:
raise ValueError('Not a good search radius')
numpredicted.append(len(predicted))
l1distances=np.array([np.sum((m.hashes[predicted,:]^m.hashes[qidx,:]),axis=1) for m in self.models])
rankings=l1distances.mean(axis=0).argsort()
#trusted_model=self.models[np.argmax([len(p) for p in preds])]
#rankings=np.sum((trusted_model.hashes[predicted,:]^trusted_model.hashes[qidx,:]),axis=1).argsort()
predicted=predicted[rankings][:nnn]
elapsed.append(time.time()-start)
trueNNs=self.firstmodel.true_nns(qidx,nnn)
recalls.append(len(set(predicted)&set(trueNNs))/nnn)
return [np.mean(recalls),np.std(recalls),np.mean(elapsed),np.std(elapsed),np.mean(numpredicted),np.std(numpredicted)] ```
Example 11
```def test_list_batch_source(self):
# Make sure that with enough epochs we sample everything.
stream = RandomFixedSizeCrop(self.batch_stream, (5, 4),
which_sources=('source2',))
seen_indices = numpy.array([], dtype='uint8')
for i in range(30):
for batch in stream.get_epoch_iterator():
for example in batch[1]:
assert example.shape == (2, 5, 4)
seen_indices = numpy.union1d(seen_indices,
example.flatten())
assert len(batch[1]) in (1, 2)
if self.source2_biggest == len(seen_indices):
break
else:
assert False ```
Example 12
```def uunion1d(arr1, arr2):
"""Find the union of two arrays.

A wrapper around numpy.intersect1d that preserves units.  All input arrays
must have the same units.  See the documentation of numpy.intersect1d for
full details.

Examples
--------
>>> from unyt import cm
>>> A = [1, 2, 3]*cm
>>> B = [2, 3, 4]*cm
>>> uunion1d(A, B)
unyt_array([1, 2, 3, 4], 'cm')

"""
v = np.union1d(arr1, arr2)
v = _validate_numpy_wrapper_units(v, [arr1, arr2])
return v ```
Example 13
```def transform_event(self, event):

for peak in event.peaks:
# check that there is a position
if not len(peak.reconstructed_positions):
continue
try:
# Get x,y position from peak
xy = peak.get_position_from_preferred_algorithm(self.config['xy_posrec_preference'])
except ValueError:
self.log.debug("Could not find any position from the chosen algorithms")
continue
try:
peak.s2_saturation_correction *= saturation_correction(
peak=peak,
channels_in_pattern=self.config['channels_top'],
expected_pattern=self.s2_patterns.expected_pattern((xy.x, xy.y)),
confused_channels=np.union1d(peak.saturated_channels, self.zombie_pmts_s2),
log=self.log)
except exceptions.CoordinateOutOfRangeException:
self.log.debug("Expected light pattern at coordinates "
"(%f, %f) consists of only zeros!" % (xy.x, xy.y))

return event ```
Example 14
```def intersect_sim(array_1, array_2):
"""Calculate the simiarity of two arrays
by using intersection / union
"""
sim = float(np.intersect1d(array_1, array_2).size) / \
float(np.union1d(array_1, array_2).size)
return sim ```
Example 15
```def get_fixed_nodes(self):
# Return a list of fixed nodes for the problem
dofs = np.arange(2 * (self.nelx + 1) * (self.nely + 1))
fixed = np.union1d(dofs[0:2 * (self.nely + 1):2],
np.array([2 * (self.nelx + 1) * (self.nely + 1) - 1]))
return fixed ```
Example 16
```def get_fixed_nodes(self):
# Return a list of fixed nodes for the problem
dofs = np.arange(2 * (self.nelx + 1) * (self.nely + 1))
fixed = np.union1d(dofs[0:2 * (self.nely + 1):2],
np.array([2 * (self.nelx + 1) * (self.nely + 1) - 1]))
return fixed ```
Example 17
```def get_fixed_nodes(self):
""" Return a list of fixed nodes for the problem. """
x = np.arange(self.passive_min_x)
topx_to_id = np.vectorize(
lambda x: xy_to_id(x, 0, self.nelx, self.nely))
ids = topx_to_id(x)
fixed = np.union1d(2 * ids, 2 * ids + 1)
return fixed ```
Example 18
```def find_known_plans(self):
"""
Find and return list of known RV stars and list of stars with earthlike planets
"""
TL = self.TargetList
SU = self.SimulatedUniverse

c = 28.4 *u.m/u.s
Mj = 317.8 * u.earthMass
Mpj = SU.Mp/Mj                     # planet masses in jupiter mass units
Ms = TL.MsTrue[SU.plan2star]
Teff = TL.stellarTeff(SU.plan2star)
mu = const.G*(SU.Mp + Ms)
T = (2.*np.pi*np.sqrt(SU.a**3/mu)).to(u.yr)
e = SU.e

t_filt = np.where((Teff.value > 3000) & (Teff.value < 6800))[0]    # planets in correct temp range

K = (c / np.sqrt(1 - e[t_filt])) * Mpj[t_filt] * np.sin(SU.I[t_filt]) * Ms[t_filt]**(-2/3) * T[t_filt]**(-1/3)

K_filter = (T[t_filt].to(u.d)/10**4).value
K_filter[np.where(K_filter < 0.03)[0]] = 0.03
k_filt = t_filt[np.where(K.value > K_filter)[0]]               # planets in the correct K range

a_filt = k_filt[np.where((SU.a[k_filt] > .95*u.AU) & (SU.a[k_filt] < 1.67*u.AU))[0]]   # planets in habitable zone
r_filt = a_filt[np.where(SU.Rp.value[a_filt] < 1.75)[0]]                               # rocky planets
self.earth_candidates = np.union1d(self.earth_candidates, r_filt).astype(int)

known_stars = np.unique(SU.plan2star[k_filt])
known_rocky = np.unique(SU.plan2star[r_filt])
return known_stars.astype(int), known_rocky.astype(int) ```
Example 19
```def check_volumes_meshed(m2m_folder, threshold=0.5):
''' Checks if the volumes have been correctly meshed

Parameters
-------------
m2m_folder: str
Path to m2m_{subID}
threshold: float
How much smaller is the meshed volume allowed to be
'''
from .. import file_finder
files = file_finder.SubjectFiles(subpath=m2m_folder)
tissues = np.union1d(
tissues = tissues[tissues != 0]
for i in tissues:
return False
return True

# =============================================================================
# SURFACE MESH UTILITIES
# ============================================================================= ```
Example 20
```def _remove_useless_states(self):
"""Check for states that don't do anything, and remove them.

Scan the A, B, and C matrices for rows or columns of zeros.  If the
zeros are such that a particular state has no effect on the input-output
dynamics, then remove that state from the A, B, and C matrices.

"""

# Search for useless states and get indices of these states.
#
# Note: shape from np.where depends on whether we are storing state
# space objects as np.matrix or np.array.  Code below will work
# correctly in either case.
ax1_A = np.where(~self.A.any(axis=1))[0]
ax1_B = np.where(~self.B.any(axis=1))[0]
ax0_A = np.where(~self.A.any(axis=0))[-1]
ax0_C = np.where(~self.C.any(axis=0))[-1]
useless_1 = np.intersect1d(ax1_A, ax1_B, assume_unique=True)
useless_2 = np.intersect1d(ax0_A, ax0_C, assume_unique=True)
useless = np.union1d(useless_1, useless_2)

# Remove the useless states.
self.A = delete(self.A, useless, 0)
self.A = delete(self.A, useless, 1)
self.B = delete(self.B, useless, 0)
self.C = delete(self.C, useless, 1)

self.states = self.A.shape[0]
self.inputs = self.B.shape[1]
self.outputs = self.C.shape[0] ```
Example 21
```def union1d(ar1, ar2):
"""
Find the union of two arrays.

Return the unique, sorted array of values that are in either of the two
input arrays.

Parameters
----------
ar1, ar2 : array_like
Input arrays. They are flattened if they are not already 1D.

Returns
-------
union1d : ndarray
Unique, sorted union of the input arrays.

--------
numpy.lib.arraysetops : Module with a number of other functions for
performing set operations on arrays.

Examples
--------
>>> np.union1d([-1, 0, 1], [-2, 0, 2])
array([-2, -1,  0,  1,  2])

To find the union of more than two arrays, use functools.reduce:

>>> from functools import reduce
>>> reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
array([1, 2, 3, 4, 6])
"""
return unique(np.concatenate((ar1, ar2), axis=None)) ```
Example 22
```def jaccard_index(x, y):
nom = np.intersect1d(x, y).size
denom = np.union1d(x, y).size

return nom/denom ```
Example 23
```def _remove_from_index_operations(self, which, transforms):
"""
Helper preventing copy code.
Remove given what (transform prior etc) from which param index ops.
"""
if len(transforms) == 0:
transforms = which.properties()
removed = np.empty((0,), dtype=int)
for t in list(transforms):
unconstrained = which.remove(t, self._raveled_index())
removed = np.union1d(removed, unconstrained)
if t is __fixed__:
self._highest_parent_._set_unfixed(self, unconstrained)

return removed ```
Example 24
```def combine_indices(arr1, arr2):
return numpy.union1d(arr1, arr2) ```
Example 25
```def union1d(ar1, ar2):
"""
Find the union of two arrays.

Return the unique, sorted array of values that are in either of the two
input arrays.

Parameters
----------
ar1, ar2 : array_like
Input arrays. They are flattened if they are not already 1D.

Returns
-------
union1d : ndarray
Unique, sorted union of the input arrays.

--------
numpy.lib.arraysetops : Module with a number of other functions for
performing set operations on arrays.

Examples
--------
>>> np.union1d([-1, 0, 1], [-2, 0, 2])
array([-2, -1,  0,  1,  2])

To find the union of more than two arrays, use functools.reduce:

>>> from functools import reduce
>>> reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
array([1, 2, 3, 4, 6])
"""
return unique(np.concatenate((ar1, ar2), axis=None)) ```
Example 26
```def union1d(ar1, ar2):
"""
Find the union of two arrays.

Return the unique, sorted array of values that are in either of the two
input arrays.

Parameters
----------
ar1, ar2 : array_like
Input arrays. They are flattened if they are not already 1D.

Returns
-------
union1d : ndarray
Unique, sorted union of the input arrays.

--------
numpy.lib.arraysetops : Module with a number of other functions for
performing set operations on arrays.

Examples
--------
>>> np.union1d([-1, 0, 1], [-2, 0, 2])
array([-2, -1,  0,  1,  2])

To find the union of more than two arrays, use functools.reduce:

>>> from functools import reduce
>>> reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
array([1, 2, 3, 4, 6])
"""
return unique(np.concatenate((ar1, ar2))) ```
Example 27
```def union1d(ar1, ar2):
"""
Find the union of two arrays.

Return the unique, sorted array of values that are in either of the two
input arrays.

Parameters
----------
ar1, ar2 : array_like
Input arrays. They are flattened if they are not already 1D.

Returns
-------
union1d : ndarray
Unique, sorted union of the input arrays.

--------
numpy.lib.arraysetops : Module with a number of other functions for
performing set operations on arrays.

Examples
--------
>>> np.union1d([-1, 0, 1], [-2, 0, 2])
array([-2, -1,  0,  1,  2])

To find the union of more than two arrays, use functools.reduce:

>>> from functools import reduce
>>> reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
array([1, 2, 3, 4, 6])
"""
return unique(np.concatenate((ar1, ar2))) ```
Example 28
```def union1d(ar1, ar2):
"""
Find the union of two arrays.

Return the unique, sorted array of values that are in either of the two
input arrays.

Parameters
----------
ar1, ar2 : array_like
Input arrays. They are flattened if they are not already 1D.

Returns
-------
union1d : ndarray
Unique, sorted union of the input arrays.

--------
numpy.lib.arraysetops : Module with a number of other functions for
performing set operations on arrays.

Examples
--------
>>> np.union1d([-1, 0, 1], [-2, 0, 2])
array([-2, -1,  0,  1,  2])

To find the union of more than two arrays, use functools.reduce:

>>> from functools import reduce
>>> reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
array([1, 2, 3, 4, 6])
"""
return unique(np.concatenate((ar1, ar2), axis=None)) ```
Example 29
```def union_classes(eval_segm, gt_segm):
eval_cl, _ = extract_classes(eval_segm)
gt_cl, _   = extract_classes(gt_segm)

cl = np.union1d(eval_cl, gt_cl)
n_cl = len(cl)

return cl, n_cl ```
Example 30
```def union1d(ar1, ar2):
"""
Find the union of two arrays.

Return the unique, sorted array of values that are in either of the two
input arrays.

Parameters
----------
ar1, ar2 : array_like
Input arrays. They are flattened if they are not already 1D.

Returns
-------
union1d : ndarray
Unique, sorted union of the input arrays.