避免多重嵌套 for-loops
Avoiding multiples nested for-loops
请原谅我不能用我的标题准确地综合我的问题,但是我想通过解释它,事情会变得更清楚。
我的问题是我必须利用一组数组的每个组合执行计算,然后将每个循环的相应结果以及用于计算的参数存储在一个数组中。随后,我会将 NumPy 数组和相应的计算结果存储在 pandas dataframe
中
我尽量避免 for 循环,我不确定是否有其他方法可以利用 pandas 或 python 方法实现我的目标,我仍然不知道.我要解决的问题自然更复杂,会涉及更多的数组和更复杂的数据。所以,总而言之,我的问题是是否有其他聪明的方法可以避免我一直在做的事情。
我一直在编写的代码 - 以非常 matlab-ish 的方式编写 - 具有与以下类似的逻辑(这只是一个说明性的更简单的示例):
max_x = 5
min_x = 1
x_1 = np.linspace(min_x, max_x, 5)
x_2 = np.linspace(min_x, max_x, 5)
x_3 = np.linspace(min_x, max_x, 5)
x_4 = np.linspace(min_x, max_x, 5)
x_result = np.array([])
x1 = np.array([])
x2 = np.array([])
x3 = np.array([])
x4 = np.array([])
for i in range(0,len(x_1)):
for j in range(0,len(x_2)):
for k in range(0,len(x_3)):
for l in range(0, len(x_4)):
x_set = [x_1[i],x_2[j],x_3[k], x_4[l]]
x_calc = calculation_1(arg = x_set)
x1 = np.append(x1, x_1[i])
x2 = np.append(x2, x_2[j])
x3 = np.append(x3, x_3[k])
x4 = np.append(x4, x_4[l])
x_result = np.append(x_result, x_calc)
df_x = pd.DataFrame(np.array([x1, x2, x3, x4, x_result])).T
例如,您可以使用递归重写 n 个嵌套循环。
(最终你可以使用队列编写任何单循环的递归)
如果我理解正确的话,你想用数组实现某种笛卡尔积。我们可以使用 np.meshgrid
来做到这一点,例如:
def cartesian_product(*arrs):
return np.transpose(np.meshgrid(*arrs)).reshape(-1, len(arrs))
例如:
>>> x_1 = [1,2,3]
>>> x_2 = [3,4]
>>> x_3 = [5]
>>> cartesian_product(x_1, x_2, x_3)
array([[1, 3, 5],
[1, 4, 5],
[2, 3, 5],
[2, 4, 5],
[3, 3, 5],
[3, 4, 5]])
然后您可以通过 calculation_1
引导此叉积的项目,例如 np.apply_along_axis(..)
:
np.apply_axis(calculation_1, 1, c)
然后我们可以将该结果添加为新列,例如 sum
:
>>> c = cartesian_product(x_1, x_2, x_3)
>>> np.hstack((c, np.apply_axis(sum, 1, c)[:, None]))
array([[ 1, 3, 5, 9],
[ 1, 4, 5, 10],
[ 2, 3, 5, 10],
[ 2, 4, 5, 11],
[ 3, 3, 5, 11],
[ 3, 4, 5, 12]])
请原谅我不能用我的标题准确地综合我的问题,但是我想通过解释它,事情会变得更清楚。
我的问题是我必须利用一组数组的每个组合执行计算,然后将每个循环的相应结果以及用于计算的参数存储在一个数组中。随后,我会将 NumPy 数组和相应的计算结果存储在 pandas dataframe
中我尽量避免 for 循环,我不确定是否有其他方法可以利用 pandas 或 python 方法实现我的目标,我仍然不知道.我要解决的问题自然更复杂,会涉及更多的数组和更复杂的数据。所以,总而言之,我的问题是是否有其他聪明的方法可以避免我一直在做的事情。
我一直在编写的代码 - 以非常 matlab-ish 的方式编写 - 具有与以下类似的逻辑(这只是一个说明性的更简单的示例):
max_x = 5
min_x = 1
x_1 = np.linspace(min_x, max_x, 5)
x_2 = np.linspace(min_x, max_x, 5)
x_3 = np.linspace(min_x, max_x, 5)
x_4 = np.linspace(min_x, max_x, 5)
x_result = np.array([])
x1 = np.array([])
x2 = np.array([])
x3 = np.array([])
x4 = np.array([])
for i in range(0,len(x_1)):
for j in range(0,len(x_2)):
for k in range(0,len(x_3)):
for l in range(0, len(x_4)):
x_set = [x_1[i],x_2[j],x_3[k], x_4[l]]
x_calc = calculation_1(arg = x_set)
x1 = np.append(x1, x_1[i])
x2 = np.append(x2, x_2[j])
x3 = np.append(x3, x_3[k])
x4 = np.append(x4, x_4[l])
x_result = np.append(x_result, x_calc)
df_x = pd.DataFrame(np.array([x1, x2, x3, x4, x_result])).T
例如,您可以使用递归重写 n 个嵌套循环。
(最终你可以使用队列编写任何单循环的递归)
如果我理解正确的话,你想用数组实现某种笛卡尔积。我们可以使用 np.meshgrid
来做到这一点,例如:
def cartesian_product(*arrs):
return np.transpose(np.meshgrid(*arrs)).reshape(-1, len(arrs))
例如:
>>> x_1 = [1,2,3]
>>> x_2 = [3,4]
>>> x_3 = [5]
>>> cartesian_product(x_1, x_2, x_3)
array([[1, 3, 5],
[1, 4, 5],
[2, 3, 5],
[2, 4, 5],
[3, 3, 5],
[3, 4, 5]])
然后您可以通过 calculation_1
引导此叉积的项目,例如 np.apply_along_axis(..)
:
np.apply_axis(calculation_1, 1, c)
然后我们可以将该结果添加为新列,例如 sum
:
>>> c = cartesian_product(x_1, x_2, x_3)
>>> np.hstack((c, np.apply_axis(sum, 1, c)[:, None]))
array([[ 1, 3, 5, 9],
[ 1, 4, 5, 10],
[ 2, 3, 5, 10],
[ 2, 4, 5, 11],
[ 3, 3, 5, 11],
[ 3, 4, 5, 12]])