本文整理汇总了Python中numpy.unique方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.unique方法的具体用法?Python numpy.unique怎么用?Python numpy.unique使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.unique方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_one_hot
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
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)
示例2: find_match
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
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
示例3: prepro_pos_table
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def prepro_pos_table(pos_tables):
"""Extracts unique positions and sorts them."""
if not isinstance(pos_tables, list):
pos_tables = [pos_tables]
pos_table = None
for next_pos_table in pos_tables:
if pos_table is None:
pos_table = next_pos_table
else:
pos_table = pd.concat([pos_table, next_pos_table])
pos_table = pos_table.groupby('chromo').apply(
lambda df: pd.DataFrame({'pos': np.unique(df['pos'])}))
pos_table.reset_index(inplace=True)
pos_table = pos_table[['chromo', 'pos']]
pos_table.sort_values(['chromo', 'pos'], inplace=True)
return pos_table
示例4: init_W
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def init_W(self, mode='normal'):
self.W = {}
if (self.status != 'load_train_data') and (self.status != 'train'):
print("Please load train data first.")
return self.W
self.status = 'init'
self.data_num = len(self.train_Y)
self.data_demension = len(self.train_X[0])
self.class_list = list(itertools.combinations(np.unique(self.train_Y), 2))
for class_item in self.class_list:
self.W[class_item] = np.zeros(self.data_demension)
return self.W
示例5: visualize_sampling
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def visualize_sampling(self,permutations):
max_length = len(permutations[0])
grid = np.zeros([max_length,max_length]) # initialize heatmap grid to 0
transposed_permutations = np.transpose(permutations)
for t, cities_t in enumerate(transposed_permutations): # step t, cities chosen at step t
city_indices, counts = np.unique(cities_t,return_counts=True,axis=0)
for u,v in zip(city_indices, counts):
grid[t][u]+=v # update grid with counts from the batch of permutations
# plot heatmap
fig = plt.figure()
rcParams.update({'font.size': 22})
ax = fig.add_subplot(1,1,1)
ax.set_aspect('equal')
plt.imshow(grid, interpolation='nearest', cmap='gray')
plt.colorbar()
plt.title('Sampled permutations')
plt.ylabel('Time t')
plt.xlabel('City i')
plt.show()
# Heatmap of attention (x=cities; y=steps)
示例6: visualize_sampling
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def visualize_sampling(self, permutations):
max_length = len(permutations[0])
grid = np.zeros([max_length,max_length]) # initialize heatmap grid to 0
transposed_permutations = np.transpose(permutations)
for t, cities_t in enumerate(transposed_permutations): # step t, cities chosen at step t
city_indices, counts = np.unique(cities_t,return_counts=True,axis=0)
for u,v in zip(city_indices, counts):
grid[t][u]+=v # update grid with counts from the batch of permutations
# plot heatmap
fig = plt.figure()
rcParams.update({'font.size': 22})
ax = fig.add_subplot(1,1,1)
ax.set_aspect('equal')
plt.imshow(grid, interpolation='nearest', cmap='gray')
plt.colorbar()
plt.title('Sampled permutations')
plt.ylabel('Time t')
plt.xlabel('City i')
plt.show()
示例7: rand_indices
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def rand_indices(x, rand_attr):
"""
Function randomly selects features without replacement. It used with random forest. Selected features must have more
than one distinct value.
x: numpy array - dataset
rand_attr - parameter defines number of randomly selected features
"""
loop = True
indices = range(len(x[0]))
while loop:
loop = False
# randomly selected features without replacement
rand_list = random.sample(indices, rand_attr)
for i in rand_list:
if len(np.unique(x[:, i])) == 1:
loop = True
indices.remove(i)
if len(indices) == rand_attr - 1:
return -1 # all features in dataset have one distinct value
break
return rand_list
示例8: count_super
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def count_super(p, m, counters, preds, labels, label_to_ch):
for l in np.unique(labels):
preds_l = preds[labels == l]
# in -> known
if label_to_ch[l]:
acc = np.zeros_like(preds_l, dtype=bool)
for c in label_to_ch[l]:
if p == 0: counters['data'][m][c] += preds_l.shape[0]
acc |= (preds_l == c)
acc_sum = acc.sum()
for c in label_to_ch[l]:
counters['acc'][p,m][c] += acc_sum
# out -> novel
else:
if p == 0: counters['data'][m][-1] += preds_l.shape[0]
acc_sum = (preds_l < 0).sum()
counters['acc'][p,m][-1] += acc_sum
示例9: print_mutation
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def print_mutation(hyp, results, bucket=''):
# Print mutation results to evolve.txt (for use with train.py --evolve)
a = '%10s' * len(hyp) % tuple(hyp.keys()) # hyperparam keys
b = '%10.3g' * len(hyp) % tuple(hyp.values()) # hyperparam values
c = '%10.3g' * len(results) % results # results (P, R, mAP, F1, test_loss)
print('\n%s\n%s\nEvolved fitness: %s\n' % (a, b, c))
if bucket:
os.system('gsutil cp gs://%s/evolve.txt .' % bucket) # download evolve.txt
with open('evolve.txt', 'a') as f: # append result
f.write(c + b + '\n')
x = np.unique(np.loadtxt('evolve.txt', ndmin=2), axis=0) # load unique rows
np.savetxt('evolve.txt', x[np.argsort(-fitness(x))], '%10.3g') # save sort by fitness
if bucket:
os.system('gsutil cp evolve.txt gs://%s' % bucket) # upload evolve.txt
示例10: estimate_mu
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def estimate_mu(self, _X1, _Y1, _X2, _Y2):
adist_m = proxy_a_distance(_X1, _X2)
C = len(np.unique(_Y1))
epsilon = 1e-3
list_adist_c = []
for i in range(1, C + 1):
ind_i, ind_j = np.where(_Y1 == i), np.where(_Y2 == i)
Xsi = _X1[ind_i[0], :]
Xtj = _X2[ind_j[0], :]
adist_i = proxy_a_distance(Xsi, Xtj)
list_adist_c.append(adist_i)
adist_c = sum(list_adist_c) / C
mu = adist_c / (adist_c + adist_m)
if mu > 1:
mu = 1
if mu < epsilon:
mu = 0
return mu
示例11: BestMap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def BestMap(L1, L2):
L1 = L1.flatten(order='F').astype(float)
L2 = L2.flatten(order='F').astype(float)
if L1.size != L2.size:
sys.exit('size(L1) must == size(L2)')
Label1 = np.unique(L1)
nClass1 = Label1.size
Label2 = np.unique(L2)
nClass2 = Label2.size
nClass = max(nClass1, nClass2)
# For Hungarian - Label2 are Workers, Label1 are Tasks.
G = np.zeros([nClass, nClass]).astype(float)
for i in range(0, nClass2):
for j in range(0, nClass1):
G[i, j] = np.sum(np.logical_and(L2 == Label2[i], L1 == Label1[j]))
c = Hungarian(-G)
newL2 = np.zeros(L2.shape)
for i in range(0, nClass2):
newL2[L2 == Label2[i]] = Label1[c[i]]
return newL2
示例12: _add_choices
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def _add_choices(self, nchoices):
if isinstance(nchoices, int):
self.nchoices = nchoices
self.choice_names = None
elif isinstance(nchoices, list) or nchoices.__class__.__name__ == "Series" or nchoices.__class__.__name__ == "DataFrame":
self.choice_names = np.array(nchoices).reshape(-1)
self.nchoices = self.choice_names.shape[0]
if np.unique(self.choice_names).shape[0] != self.choice_names.shape[0]:
raise ValueError("Arm/choice names contain duplicates.")
elif isinstance(nchoices, np.ndarray):
self.choice_names = nchoices.reshape(-1)
self.nchoices = self.choice_names.shape[0]
if np.unique(self.choice_names).shape[0] != self.choice_names.shape[0]:
raise ValueError("Arm/choice names contain duplicates.")
else:
raise ValueError("'nchoices' must be an integer or list with named arms.")
示例13: _partial_fit_single
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def _partial_fit_single(self, choice, X, a, r):
yclass, this_choice = self._filter_arm_data(X, a, r, choice)
if self.smooth is not None:
self.counters[0, choice] += yclass.shape[0]
xclass = X[this_choice, :]
do_full_refit = False
if self.buffer is not None:
do_full_refit = self.buffer[choice].do_full_refit()
xclass, yclass = self.buffer[choice].get_batch(xclass, yclass)
if (xclass.shape[0] > 0) or self.force_fit:
if (do_full_refit) and (np.unique(yclass).shape[0] >= 2):
self.algos[choice].fit(xclass, yclass)
else:
self.algos[choice].partial_fit(xclass, yclass, classes = [0, 1])
## update the beta counters if needed
if (self.force_counters):
self._update_beta_counters(yclass, choice)
示例14: fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def fit(self, X, y, *args, **kwargs):
if X.shape[0] == 0:
return self
elif np.unique(y).shape[0] <= 1:
return self
self.model.fit(X, y)
var = self.model.predict_proba(X)[:,1]
var = var * (1 - var)
n = X.shape[1]
self.Sigma = np.zeros((n+self.fit_intercept, n+self.fit_intercept), dtype=ctypes.c_double)
X, Xcsr = self._process_X(X)
_wrapper_double.update_matrices_noinv(
X,
np.empty(0, dtype=ctypes.c_double),
var,
self.Sigma,
np.empty(0, dtype=ctypes.c_double),
Xcsr = Xcsr,
add_bias=self.fit_intercept,
overwrite=1
)
_matrix_inv_symm(self.Sigma, self.lambda_)
self.is_fitted = True
示例15: one_hot
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import unique [as 别名]
def one_hot(y, fill_k=False, one_not=False):
"""Map to one-hot encoding."""
# Check labels
labels = np.unique(y)
# Number of classes
K = len(labels)
# Number of samples
N = y.shape[0]
# Preallocate array
if one_not:
Y = -np.ones((N, K))
else:
Y = np.zeros((N, K))
# Set k-th column to 1 for n-th sample
for n in range(N):
# Map current class to index label
y_n = (y[n] == labels)
if fill_k:
Y[n, y_n] = y_n
else:
Y[n, y_n] = 1
return Y, labels