【问题标题】:Python - Find all possible combination between two arraysPython - 查找两个数组之间的所有可能组合
【发布时间】:2021-10-06 14:06:45
【问题描述】:

我已经为我的长代码创建了一个小模型。 现在,让我们考虑以下示例:

  • x_interval(我们可以简单地称之为X)= [100,101,...,149] 50 个点的列表;
  • y_interval(我们可以简单地称之为Y)= [0.5,0.51,...,0.99] 50 个点的列表。

在下面的代码中,我能够针对 X 和 Y 的每个元素生成此函数的绘图,即关于 X 的第一个元素和 Y 的第一个元素 列表,然后相对于两个列表的第二个元素,直到两个列表的最后一个元素,如下图所示:

如何在这两个列表之间获取所有可能的组合?是否可以用 3 维图表示所有可能的组合?

这是我的代码的“模型”:

import numpy as np
from scipy.interpolate import barycentric_interpolate
import matplotlib.pyplot as plt
def func(x,y):
    return 2*np.array(x) - np.array(y)**2

lower_x, upper_x = 100,150
lower_y, upper_y = 0.5,1.
x_interval = np.arange(lower_x, upper_x,1)    #X
y_interval = np.arange(lower_y, upper_y,0.01) #Y

x1 = np.linspace(lower_x, upper_x,7)
x2 = np.linspace(lower_y, upper_y,7)


def function(x1,x2,x_interval,y_interval):
    res_tot = []
    res_1 = []

    index = 0
    for xi in x_interval:
        result = func(xi,x2)
        interpolation = barycentric_interpolate(x2,result,y_interval)
        res_1.append(interpolation[index])
        index += 1
    
    index = 0
    for xi in x1:
        result = func(xi,x2)
        res_tot.append(result[index])
        index += 1

    output = barycentric_interpolate(x1, res_tot, x_interval)
    return np.array(res_1) - np.array(output)*x_interval
print(function(x1,x2,x_interval,y_interval))

plt.plot(function(x1,x2,x_interval,y_interval))

提前致谢!!

【问题讨论】:

标签: python numpy matplotlib combinations


【解决方案1】:

如果您的目的是获得用于 3D 绘图的 x/y 组合,那么您正在寻找的是 numpy.meshgrid

# input arrays
x = np.arange(1, 10)
y = np.arange(10, 100, 20)

# x = array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# y = array([10, 30, 50, 70, 90])

# computing the meshgrid
X, Y = np.meshgrid(x,y)

输出:

# X
array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
       [1, 2, 3, 4, 5, 6, 7, 8, 9],
       [1, 2, 3, 4, 5, 6, 7, 8, 9],
       [1, 2, 3, 4, 5, 6, 7, 8, 9],
       [1, 2, 3, 4, 5, 6, 7, 8, 9]])

# Y
array([[10, 10, 10, 10, 10, 10, 10, 10, 10],
       [30, 30, 30, 30, 30, 30, 30, 30, 30],
       [50, 50, 50, 50, 50, 50, 50, 50, 50],
       [70, 70, 70, 70, 70, 70, 70, 70, 70],
       [90, 90, 90, 90, 90, 90, 90, 90, 90]])

【讨论】:

    【解决方案2】:

    对于三维绘图,您将需要第三个带有 z_interval 的列表。

    import matplotlib.pyplot as plt
    
    x_interval = [1,2,3,4,5]
    y_interval = [6,7,8,9,10]
    z_interval = [0,3,9,5,11]
    
    fig = plt.figure()
    ax = plt.axes(projection='3d')
    ax.plot3D(x_interval, y_interval, z_interval, 'gray')
    

    要交互所有 X 和 Y,您只需在 for 中使用 for:

    for x in x_interval:
        for y in y_interval:
            do_something(x,y)
    

    创建所有可能组合的一种简单方法是使用 Pandas。

    import pandas as pd
    
    x_interval = [1,2,3,4,5]
    y_interval = [6,7,8,9,10]
    
    x_interval_df = pd.DataFrame (x_interval, columns = ['column_x'])
    y_interval_df = pd.DataFrame (y_interval, columns = ['column_y'])
    
    x_interval_df['key'] = 1
    y_interval_df['key'] = 1
    
    possible_combinations = pd.merge(x_interval_df, y_interval_df, on ='key').drop(columns=['key'])
    

    然后您可以使用所有可能的组合与数据框进行交互。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 2011-08-10
      相关资源
      最近更新 更多