Home > Blockchain >  Python - Find all possible combination between two arrays
Python - Find all possible combination between two arrays

Time:10-06

I have created a little model of my long code. Now, let's consider the following example, with:

  • x_interval (we can simply call it X) = [100,101,...,149] a list of 50 points;
  • y_interval (we can simply call it Y) = [0.5,0.51,...,0.99] alist of 50 points.

In the code below I am able to generate the plot of this function, with respect each element of X and Y, i.e. with respect the first element of X and first element of Y list, after that with respect to the second element of both list, till the last element of both list, as we can see from the picture below:

enter image description here

How can I take all the possible combinations, between these 2 lists? It is possible represent all possible combination, with a 3-dimensional plot?

This is the "model" of my code:

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))

Thanks in advance!!

CodePudding user response:

If your purpose is to get x/y combinations for 3D plotting, what you're looking for is 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)

output:

# 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]])

CodePudding user response:

For a Three-Dimensional plot you will need the third list with the 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')

To interact all Xs and Ys you can just use a for inside a for:

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

And one simple way to create all possible combinations is using 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'])

Then you can interact the the dataframe with all possible combinations.

  • Related