gpt4 book ai didi

python - 增长 numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:09 26 4
gpt4 key购买 nike

我有一个 3x256 规则的规则集。每个规则映射到一个 3x3 的值网格,这些值本身就是规则。

规则示例:

0 -> [[0,0,0],[0,1,0],[0,0,0]]  
1 -> [[1,1,1],[0,0,0],[1,1,1]]

种子:

[[0]]

1 次迭代后:

[[0,0,0],
[0,1,0],
[0,0,0]]

2次迭代后:

[[0,0,0,0,0,0,0,0,0],
[0,1,0,0,1,0,0,1,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,1,1,1,0,0,0],
[0,1,0,0,0,0,0,1,0],
[0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,1,0,0,1,0,0,1,0],
[0,0,0,0,0,0,0,0,0]]

现在我有了一个有效的实现,但是,它是我的脚本中最慢的函数。我想知道是否有一种更Pythonic和更有效的方法来重写这个函数。

def decode(rules,fractal_iterations,seed):
final_seed_matrix = np.zeros((3,3**fractal_iterations,3**fractal_iterations))

for i in range(dimensions):
seed_matrix = np.array([[seed]])
for j in range(fractal_iterations):
size_y = seed_matrix.shape[0]
size_x = seed_matrix.shape[1]
new_matrix = np.zeros((size_y*rule_size_sqrt,size_x*rule_size_sqrt))

for y in range(size_y):
for x in range(size_x):
seed_value = seed_matrix[y,x]
new_matrix[y*rule_size_sqrt : y*rule_size_sqrt+rule_size_sqrt, x*rule_size_sqrt : x*rule_size_sqrt+rule_size_sqrt] = rules[int(seed_value),i]

seed_matrix = new_matrix
final_seed_matrix[i] = seed_matrix

return np.moveaxis(final_seed_matrix,0,-1)

最佳答案

这是一个优化版本,它使用高级索引在一个索引步骤中选择和修补所有规则。这将创建一个 4D 数组,在其替换的像素位置处具有适当的规则。将其展平为 2D 就是交换中轴并 reshape 形状的问题。它似乎给出了与您相同的结果,但速度明显更快(到目前为止仅测试了整数规则):

results equal: True
OP : 24.883304461836815 ms
optimized: 1.093490980565548 ms

代码:

import numpy as np

dimensions = 3
rule_size_sqrt = 3

def decode(rules,fractal_iterations,seed):
final_seed_matrix = np.zeros((3,3**fractal_iterations,3**fractal_iterations))

for i in range(dimensions):
seed_matrix = np.array([[seed]])
for j in range(fractal_iterations):
size_y = seed_matrix.shape[0]
size_x = seed_matrix.shape[1]
new_matrix = np.zeros((size_y*rule_size_sqrt,size_x*rule_size_sqrt))

for y in range(size_y):
for x in range(size_x):
seed_value = seed_matrix[y,x]
new_matrix[y*rule_size_sqrt : y*rule_size_sqrt+rule_size_sqrt, x*rule_size_sqrt : x*rule_size_sqrt+rule_size_sqrt] = rules[int(seed_value),i]

seed_matrix = new_matrix
final_seed_matrix[i] = seed_matrix

return np.moveaxis(final_seed_matrix,0,-1)

def decode_fast(rules, fractal_iterations, seed):
rules_int = rules.astype(int)
seed = np.array([[seed]], dtype=int)
res = np.empty((3**fractal_iterations, 3**fractal_iterations, dimensions),
dtype=rules.dtype)
for i in range(dimensions):
grow = seed
for j in range(1, fractal_iterations):
grow = rules_int[grow, i].swapaxes(1, 2).reshape(3**j, -1)
grow = rules[grow, i].swapaxes(1, 2).reshape(3**fractal_iterations, -1)
res[..., i] = grow
return res

rules = np.random.randint(0, 4, (4, dimensions, 3, 3))
seed = 1
fractal_iterations = 5
print('results equal:', np.all(decode(rules, fractal_iterations, seed) == decode_fast(rules, fractal_iterations, seed)))

from timeit import repeat
print('OP :', min(repeat('decode(rules, fractal_iterations, seed)', globals=globals(), number=50))*20, 'ms')
print('optimized:', min(repeat('decode_fast(rules, fractal_iterations, seed)', globals=globals(), number=50))*20, 'ms')

关于python - 增长 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48420760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com