Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 异质化numpy阵列,使相邻的两个单元不相等_Python 3.x_Numpy - Fatal编程技术网

Python 3.x 异质化numpy阵列,使相邻的两个单元不相等

Python 3.x 异质化numpy阵列,使相邻的两个单元不相等,python-3.x,numpy,Python 3.x,Numpy,我有一个大型numpy数组,我希望使用以下条件填充该数组: 仅使用设定范围为0到9的数字,例如 填充以使相邻单元格不相等。这意味着每个单元格将被不同的值包围。不允许有任何例外。 我目前正在使用以下功能 def uniqify(in_array): #warning: array is mutable and gets modified outside this scope out_array = np.arange(in_array.size).reshape(in_array.

我有一个大型numpy数组,我希望使用以下条件填充该数组:

仅使用设定范围为0到9的数字,例如 填充以使相邻单元格不相等。这意味着每个单元格将被不同的值包围。不允许有任何例外。 我目前正在使用以下功能

def uniqify(in_array):
    #warning: array is mutable and gets modified outside this scope
    out_array = np.arange(in_array.size).reshape(in_array.shape)
    return out_array.astype(np.float32)
但是,这违反了第一个标准,因为数组值会上升到非常高的数字。由于在输入此函数之前我不知道这些数组的大小,因此我宁愿限制每个条目中可以包含的数字的大小

有什么优雅的解决方案可以做到这一点吗

编辑

我目前正在研究numpy中的一些随机化功能,例如

下面是我用np.random.choice编写的一个例子


这会打印各种数字范围从0到1的列表,这很好,但是对于大型数据集,我可以想象可能的样本之间的差异非常小,以至于它们几乎相等。因此,我不能随机化,但需要驱动一个函数,明确目标是避免相邻单元相等。我想象数字可以从1到9…正如Paul Panzer评论的那样,可以构建一个规则的棋盘状图案:

def uniq(shape):
    f = lambda *idx: np.mod(np.sum(idx, axis=0), 10)
    return np.fromfunction(f, shape)
例如,uniq5,17是

没有相邻的元素是相等的。模甚至可以取为mod 2,从而得到一个0-1数组

如果您希望数组是随机的,或者至少看起来是随机的,分步可以提供帮助

def uniq(shape):
    steps = 1 + np.mod(np.random.randint(1, 100, size=len(shape))*(np.sqrt(5)+1)/2, 8)
    f = lambda *idx: np.mod(np.floor(np.random.uniform(0, 10) + np.moveaxis(idx, 0, -1).dot(steps)), 10)
    return np.fromfunction(f, shape)
现在uniq5,17是一个随机的样子

[[ 4.  8.  3.  7.  2.  7.  1.  6.  0.  5.  0.  4.  9.  3.  8.  3.  7.]
 [ 0.  4.  9.  4.  8.  3.  7.  2.  7.  1.  6.  0.  5.  0.  4.  9.  3.]
 [ 6.  1.  5.  0.  4.  9.  4.  8.  3.  7.  2.  7.  1.  6.  0.  5.  0.]
 [ 2.  7.  1.  6.  1.  5.  0.  4.  9.  4.  8.  3.  7.  2.  7.  1.  6.]
 [ 8.  3.  8.  2.  7.  1.  6.  1.  5.  0.  4.  9.  4.  8.  3.  7.  2.]]

第一个版本的所有步骤都是1。步长的计算基于黄金比率np.sqrt5+1/2,因为其倍数产生均匀分布但看起来随机的数字。步骤保证在1到9之间,因此在铺设地板和采用mod 10后,每一步都有一个不同的数字。

如果replace=False,您将永远无法获得相等的相邻值,因为所有值都是唯一的。因此,假设您的目标是获得一个比可能值的数量更长的随机_列表,为什么不将其填充到for循环中,以便跟踪最后添加的元素。如果下一个选项等于最后一个元素,请选择另一个。您希望列表的随机性如何?很容易找到符合您标准的常规棋盘格式。
def uniq(shape):
    steps = 1 + np.mod(np.random.randint(1, 100, size=len(shape))*(np.sqrt(5)+1)/2, 8)
    f = lambda *idx: np.mod(np.floor(np.random.uniform(0, 10) + np.moveaxis(idx, 0, -1).dot(steps)), 10)
    return np.fromfunction(f, shape)
[[ 4.  8.  3.  7.  2.  7.  1.  6.  0.  5.  0.  4.  9.  3.  8.  3.  7.]
 [ 0.  4.  9.  4.  8.  3.  7.  2.  7.  1.  6.  0.  5.  0.  4.  9.  3.]
 [ 6.  1.  5.  0.  4.  9.  4.  8.  3.  7.  2.  7.  1.  6.  0.  5.  0.]
 [ 2.  7.  1.  6.  1.  5.  0.  4.  9.  4.  8.  3.  7.  2.  7.  1.  6.]
 [ 8.  3.  8.  2.  7.  1.  6.  1.  5.  0.  4.  9.  4.  8.  3.  7.  2.]]