本文整理汇总了Python中numpy.setxor1d方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.setxor1d方法的具体用法?Python numpy.setxor1d怎么用?Python numpy.setxor1d使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.setxor1d方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setxor1d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def setxor1d(ar1, ar2, assume_unique=False):
"""
Find the set exclusive-or of two arrays.
Return the sorted, unique values that are in only one (not both) of the
input arrays.
Parameters
----------
ar1, ar2 : array_like
Input arrays.
assume_unique : bool
If True, the input arrays are both assumed to be unique, which
can speed up the calculation. Default is False.
Returns
-------
setxor1d : ndarray
Sorted 1D array of unique values that are in only one of the input
arrays.
Examples
--------
>>> a = np.array([1, 2, 3, 2, 4])
>>> b = np.array([2, 3, 5, 7, 5])
>>> np.setxor1d(a,b)
array([1, 4, 5, 7])
"""
if not assume_unique:
ar1 = unique(ar1)
ar2 = unique(ar2)
aux = np.concatenate((ar1, ar2))
if aux.size == 0:
return aux
aux.sort()
flag = np.concatenate(([True], aux[1:] != aux[:-1], [True]))
return aux[flag[1:] & flag[:-1]]
示例2: within_index
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def within_index(self, *args, **kwargs):
A = self.A.within_index(*args, **kwargs)
B = self.B.within_index(*args, **kwargs)
return setxor1d(A, B, assume_unique=True)
示例3: __xor__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def __xor__(self, other):
if not isinstance(other, Fingerprint):
raise E3FPInvalidFingerprintError(
"variable is %s not Fingerprint" % (other.__class__.__name__)
)
if self.bits != other.bits:
raise E3FPBitsValueError(
"cannot compare fingerprints of different sizes"
)
return Fingerprint(
np.setxor1d(self.indices, other.indices, assume_unique=True),
bits=self.bits,
)
示例4: run_one_step
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def run_one_step(self):
is_drainage_node = self._channel_mask
is_drainage_node[self._grid.open_boundary_nodes] = 1
# check for pits
self_draining_nodes = np.where(
self._receivers == np.arange(self._grid.number_of_nodes)
)
pits = np.setxor1d(self_draining_nodes, self._grid.boundary_nodes)
if pits.any():
warn(
"Pits detected in the flow directions supplied. "
"Pits will be treated as drainage nodes."
)
is_drainage_node[pits] = 1
# iterate downstream through stack to find nearest drainage elevation
nearest_drainage_elev = np.empty(self._elev.shape)
for n in self._node_order:
r = self._receivers[n]
# if not drainage node set drainage elevation to downstream.
if not is_drainage_node[n]:
nearest_drainage_elev[n] = nearest_drainage_elev[r]
else: # set elevation of drainage to self.
nearest_drainage_elev[n] = self._elev[n]
self._hand[:] = self._elev - nearest_drainage_elev
示例5: get_train_datasets
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def get_train_datasets(flags):
mini_imagenet = _load_mini_imagenet(data_dir=flags.data_dir, split='sources')
few_shot_data_train = Dataset(mini_imagenet)
pretrain_data_train, pretrain_data_test = None, None
if flags.feat_extract_pretrain:
train_idx = np.random.choice(range(len(mini_imagenet[0])), size=int(0.9 * len(mini_imagenet[0])),
replace=False)
test_idx = np.setxor1d(range(len(mini_imagenet[0])), train_idx)
new_labels = mini_imagenet[1]
for i, old_class in enumerate(set(mini_imagenet[1])):
new_labels[mini_imagenet[1] == old_class] = i
pretrain_data_train = Dataset((mini_imagenet[0][train_idx], new_labels[train_idx]))
pretrain_data_test = Dataset((mini_imagenet[0][test_idx], new_labels[test_idx]))
return few_shot_data_train, pretrain_data_train, pretrain_data_test
示例6: get_few_shot_idxs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def get_few_shot_idxs(labels, classes, num_shots):
train_idxs, test_idxs = [], []
idxs = np.arange(len(labels))
for cl in classes:
class_idxs = idxs[labels == cl]
class_idxs_train = np.random.choice(class_idxs, size=num_shots, replace=False)
class_idxs_test = np.setxor1d(class_idxs, class_idxs_train)
train_idxs.extend(class_idxs_train)
test_idxs.extend(class_idxs_test)
assert set(class_idxs_train).isdisjoint(test_idxs)
return np.array(train_idxs), np.array(test_idxs)
示例7: get_few_shot_idxs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def get_few_shot_idxs(self, labels, classes, num_shots):
train_idxs, test_idxs = [], []
idxs = np.arange(len(labels))
for cl in classes:
class_idxs = idxs[labels == cl]
class_idxs_train = np.random.choice(class_idxs, size=num_shots, replace=False)
class_idxs_test = np.setxor1d(class_idxs, class_idxs_train)
train_idxs.extend(class_idxs_train)
test_idxs.extend(class_idxs_test)
assert set(class_idxs_train).isdisjoint(test_idxs)
return np.array(train_idxs), np.array(test_idxs)
示例8: test_setxor1d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def test_setxor1d(self):
self.check2(np.setxor1d)
示例9: setxor1d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def setxor1d(ar1, ar2, assume_unique=False):
"""
Find the set exclusive-or of two arrays.
Return the sorted, unique values that are in only one (not both) of the
input arrays.
Parameters
----------
ar1, ar2 : array_like
Input arrays.
assume_unique : bool
If True, the input arrays are both assumed to be unique, which
can speed up the calculation. Default is False.
Returns
-------
setxor1d : ndarray
Sorted 1D array of unique values that are in only one of the input
arrays.
Examples
--------
>>> a = np.array([1, 2, 3, 2, 4])
>>> b = np.array([2, 3, 5, 7, 5])
>>> np.setxor1d(a,b)
array([1, 4, 5, 7])
"""
if not assume_unique:
ar1 = unique(ar1)
ar2 = unique(ar2)
aux = np.concatenate((ar1, ar2))
if aux.size == 0:
return aux
aux.sort()
# flag = ediff1d( aux, to_end = 1, to_begin = 1 ) == 0
flag = np.concatenate(([True], aux[1:] != aux[:-1], [True]))
# flag2 = ediff1d( flag ) == 0
flag2 = flag[1:] == flag[:-1]
return aux[flag2]
示例10: setxor1d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def setxor1d(ar1, ar2, assume_unique=False):
"""
Find the set exclusive-or of two arrays.
Return the sorted, unique values that are in only one (not both) of the
input arrays.
Parameters
----------
ar1, ar2 : array_like
Input arrays.
assume_unique : bool
If True, the input arrays are both assumed to be unique, which
can speed up the calculation. Default is False.
Returns
-------
setxor1d : ndarray
Sorted 1D array of unique values that are in only one of the input
arrays.
Examples
--------
>>> a = np.array([1, 2, 3, 2, 4])
>>> b = np.array([2, 3, 5, 7, 5])
>>> np.setxor1d(a,b)
array([1, 4, 5, 7])
"""
if not assume_unique:
ar1 = unique(ar1)
ar2 = unique(ar2)
aux = np.concatenate( (ar1, ar2) )
if aux.size == 0:
return aux
aux.sort()
# flag = ediff1d( aux, to_end = 1, to_begin = 1 ) == 0
flag = np.concatenate( ([True], aux[1:] != aux[:-1], [True] ) )
# flag2 = ediff1d( flag ) == 0
flag2 = flag[1:] == flag[:-1]
return aux[flag2]
示例11: _train__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import setxor1d [as 别名]
def _train__(self):
# Init pop and calculate fitness
pop = [self._create_solution__(minmax=0) for _ in range(self.pop_size)]
# Find the pathfinder
pop = sorted(pop, key=lambda temp: temp[self.ID_FIT])
g_best = deepcopy(pop[0])
gbest_present = deepcopy(g_best)
for i in range(self.epoch):
alpha, beta = np.random.uniform(1, 2, 2)
A = np.random.uniform(self.domain_range[0], self.domain_range[1]) * np.exp(-2 * (i + 1) / self.epoch)
## Update the position of pathfinder and check the bound
temp = gbest_present[self.ID_POS] + 2 * np.random.uniform() * (gbest_present[self.ID_POS] - g_best[self.ID_POS]) + A
temp = self._amend_solution_and_return__(temp)
fit = self._fitness_model__(temp)
g_best = deepcopy(gbest_present)
if fit < gbest_present[self.ID_FIT]:
gbest_present = [temp, fit]
pop[0] = deepcopy(gbest_present)
## Update positions of members, check the bound and calculate new fitness
for j in range(1, self.pop_size):
temp1 = deepcopy(pop[j][self.ID_POS])
t1 = beta * np.random.uniform() * (gbest_present[self.ID_POS] - temp1)
my_list_idx = np.setxor1d( np.array(range(1, self.pop_size)) , np.array([j]) )
idx = np.random.choice(my_list_idx)
dist = np.linalg.norm(pop[idx][self.ID_POS] - temp1)
t2 = alpha * np.random.uniform() * (pop[idx][self.ID_POS] - temp1)
t3 = np.random.uniform(self.domain_range[0], self.domain_range[1], self.problem_size) * (1 - (i + 1) * 1.0 / self.epoch) * dist
temp1 += t1 + t2 + t3
## Update members
temp1 = self._amend_solution_and_return__(temp1)
fit = self._fitness_model__(temp1)
if fit < pop[j][self.ID_FIT]:
pop[j] = [temp1, fit]
## Update the best solution found so far (current pathfinder)
pop = sorted(pop, key=lambda temp: temp[self.ID_FIT])
current_best = deepcopy(pop[self.ID_MIN_PROBLEM])
if current_best[self.ID_FIT] < gbest_present[self.ID_FIT]:
gbest_present = deepcopy(current_best)
self.loss_train.append(gbest_present[self.ID_FIT])
if self.print_train:
print("Generation : {0}, best result so far: {1}".format(i + 1, gbest_present[self.ID_FIT]))
return gbest_present[self.ID_FIT], self.loss_train