我的数据样本每个都是一个整齐的形状,例如: (100,100,9),我将其中的10个连接成一个形状为 login.Click += delegate
{
activityIndicator.Visibility = Android.Views.ViewStates.Visible;
new UsersAuthentication().UsersAuthenTask(email.Text,password.Text, StartActivity, new Intent(this, typeof(IndMassActivity)));
};
}
public void AuthorizationFailed()
{
Toast.MakeText(this, "Authorization failed", ToastLength.Short).Show();
}
的阵列(10,100,100,9)。在10个数据样本中,我想找到重复值的索引。例如,如果foo
和foo[0, 42, 42, 3] = 0.72
,我想要一个反映这一点的输出。这样做的有效方法是什么?
我正在考虑一个形状为布尔输出的数组(100,100,9),但是有一个比循环更好的方法来比较每个数据样本(数据样本数量的二次运行时间(10))?
答案 0 :(得分:0)
在下面的代码段中,id_rsa.pub
是所需的结果:一个布尔数组,显示哪些索引是重复的。还有dups
阈值,因此值< =此阈值的任何差异都是重复的。
delta
答案 1 :(得分:-1)
以下是针对每个样本使用argsort
的解决方案。不漂亮,不快但完成工作。
import numpy as np
from timeit import timeit
def dupl(a, axis=0, make_dict=True):
a = np.moveaxis(a, axis, -1)
i = np.argsort(a, axis=-1, kind='mergesort')
ai = a[tuple(np.ogrid[tuple(map(slice, a.shape))][:-1]) + (i,)]
same = np.zeros(a.shape[:-1] + (a.shape[-1]+1,), bool)
same[..., 1:-1] = np.diff(ai, axis=-1) == 0
uniqs = np.where((same[..., 1:] & ~same[..., :-1]).ravel())[0]
same = (same[...,1:]|same[...,:-1]).ravel()
reps = np.split(i.ravel()[same], np.cumsum(same)[uniqs[1:]-1])
grps = np.searchsorted(uniqs, np.arange(0, same.size, a.shape[-1]))
keys = ai.ravel()[uniqs]
if make_dict:
result = np.empty(a.shape[:-1], object)
result.ravel()[:] = [dict(zip(*p)) for p in np.split(
np.array([keys, reps], object), grps[1:], axis=-1)]
return result
else:
return keys, reps, grps
a = np.random.randint(0,10,(10,100,100,9))
axis = 0
result = dupl(a, axis)
print('shape, axis, time (sec) for 10 trials:',
a.shape, axis, timeit(lambda: dupl(a, axis=axis), number=10))
print('same without creating dict:',
a.shape, axis, timeit(lambda: dupl(a, axis=axis, make_dict=False),
number=10))
#check
print("checking result")
am = np.moveaxis(a, axis, -1)
for af, df in zip(am.reshape(-1, am.shape[-1]), result.ravel()):
assert len(set(af)) + sum(map(len, df.values())) == len(df) + am.shape[-1]
for k, v in df.items():
assert np.all(np.where(af == k)[0] == v)
print("no errors")
打印:
shape, axis, time (sec) for 10 trials: (10, 100, 100, 9) 0 5.328339613042772
same without creating dict: (10, 100, 100, 9) 0 2.568383438978344
checking result
no errors